Skip to content

Commit

Permalink
feat: add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Nov 13, 2024
1 parent 8dbf57b commit 8d10426
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 69 deletions.
18 changes: 18 additions & 0 deletions .github/matchers/mypy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"problemMatcher": [
{
"owner": "mypy",
"pattern": [
{
"regexp": "^([^:]*):(\\d+):(?:(\\d+):)? ([^:]*): (.*?)(?: \\[(\\S+)\\])?$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5,
"code": 6
}
]
}
]
}
5 changes: 5 additions & 0 deletions .github/matchers/mypy.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright © Michal Čihař <[email protected]>

SPDX-License-Identifier: CC0-1.0

This file is maintained in https://github.com/WeblateOrg/meta/
17 changes: 17 additions & 0 deletions .github/matchers/pytest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"problemMatcher": [
{
"owner": "pytest",
"severity": "warning",
"pattern": [
{
"regexp": "^ *([^: ]*):(\\d+): ([^ :]*Warning): (.*)$",
"file": 1,
"line": 2,
"code": 3,
"message": 4
}
]
}
]
}
5 changes: 5 additions & 0 deletions .github/matchers/pytest.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright © Michal Čihař <[email protected]>

SPDX-License-Identifier: CC0-1.0

This file is maintained in https://github.com/WeblateOrg/meta/
43 changes: 43 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0

name: mypy

on:
push:
branches-ignore:
- deepsource-fix-**
- renovate/**
- weblate
pull_request:

permissions:
contents: read

jobs:
mypy:
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2

- uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: ''
cache-suffix: '3.13'
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
- name: Install pip dependencies
run: uv pip install --system -e .[dev]

- name: Run mypy
run: |
echo "::add-matcher::.github/matchers/mypy.json"
mypy --show-column-numbers weblate_web
echo "::remove-matcher owner=mypy::"
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ text = "MIT"

[project.optional-dependencies]
dev = [
"weblate-language-data[lint,test]",
"weblate-language-data[lint,test,types]",
"translate-toolkit==3.14.1"
]
django = [
Expand All @@ -47,6 +47,10 @@ lint = [
test = [
"twine==5.1.1"
]
types = [
"mypy==1.3.0",
"django-stubs==5.1.1"
]

[project.readme]
content-type = "text/x-rst"
Expand Down
90 changes: 33 additions & 57 deletions scripts/generate-language-data
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ See https://github.com/WeblateOrg/language-data
import csv
import json
import re
import subprocess
from itertools import chain

SPLIT_RE = re.compile(
Expand All @@ -35,19 +34,25 @@ https://github.com/WeblateOrg/language-data
"""
# pylint: disable=line-too-long,too-many-lines
'''

TEMPLATE = """ (
'{0}',
"{0}",
# Translators: Language name for ISO code "{0}". The parenthesis clarifies
# variant of the language. It could contain a region, age (Old, Middle, ...)
# or other variant.
_('{1}'),
_("{1}"),
{2},
'{3}',
"{3}",
),
"""
TYPE_HINT = "tuple[tuple[str, str, int, str], ...]"


def escape(value: str) -> str:
"""Escape string for use in template."""
return value.replace('"', r"\"")


# Read languages
with open("languages.csv") as csvfile:
Expand Down Expand Up @@ -150,62 +155,54 @@ with open("weblate_language_data/languages.py", "w") as output:
output.write(HEADER)
output.write("from .utils import gettext_noop as _\n\n")
output.write("# Language definitions\n")
output.write("LANGUAGES = (\n")
output.write(f"LANGUAGES: {TYPE_HINT} = (\n")
for row in LANGUAGES:
output.write(
TEMPLATE.format(row[0], row[1].replace("'", "\\'"), row[2], row[3])
)
output.write(TEMPLATE.format(row[0], escape(row[1]), row[2], row[3]))
output.write(")\n")
with open("weblate_language_data/population.py", "w") as output:
output.write(HEADER)
output.write("# Language definitions\n")
output.write("POPULATION = {\n")
output.write("POPULATION: dict[str, int] = {\n")
for row in LANGUAGES:
code = row[0]
output.write(f" {code!r}: {get_population(code)},\n")
output.write(f' "{escape(code)}": {get_population(code)},\n')
output.write("}\n")
with open("weblate_language_data/plurals.py", "w") as output:
output.write(HEADER)
output.write("from .utils import gettext_noop as _\n\n")
output.write("# Additional plural rules definitions\n")
output.write("EXTRAPLURALS = (\n")
output.write(f"EXTRAPLURALS: {TYPE_HINT} = (\n")
for row in EXTRAPLURALS:
output.write(
TEMPLATE.format(row[0], row[1].replace("'", "\\'"), row[2], row[3])
)
output.write(TEMPLATE.format(row[0], escape(row[1]), row[2], row[3]))
output.write(")\n")
output.write("\n")
output.write("CLDRPLURALS = (\n")
output.write(f"CLDRPLURALS: {TYPE_HINT} = (\n")
for row in CLDRPLURALS:
output.write(
TEMPLATE.format(row[0], row[1].replace("'", "\\'"), row[2], row[3])
)
output.write(TEMPLATE.format(row[0], escape(row[1]), row[2], row[3]))
output.write(")\n")
output.write("\n")
output.write("QTPLURALS = (\n")
output.write(f"QTPLURALS: {TYPE_HINT} = (\n")
for row in QTPLURALS:
output.write(
TEMPLATE.format(row[0], row[1].replace("'", "\\'"), row[2], row[3])
)
output.write(TEMPLATE.format(row[0], escape(row[1]), row[2], row[3]))
output.write(")\n")
with open("weblate_language_data/aliases.py", "w") as output:
output.write(HEADER)
output.write("# Language aliases\n")
output.write("ALIASES = {\n")
output.write("ALIASES: dict[str, str] = {\n")
for row in ALIASES:
output.write(" '{}': '{}',\n".format(*row))
output.write(""" "{}": "{}",\n""".format(*row))
output.write("}\n")
with open("weblate_language_data/countries.py", "w") as output:
output.write(HEADER)
output.write("# List of defaul languages, omitting country code should be okay\n")
output.write("DEFAULT_LANGS = (\n")
output.write("DEFAULT_LANGS: tuple[str, ...] = (\n")
for row in DEFAULT_COUNTRIES:
output.write(" '{}',\n".format(*row))
output.write(f' "{escape(row[0])}",\n')
output.write(")\n")
with open("weblate_language_data/rtl.py", "w") as output:
output.write(HEADER)
output.write("# List of RTL languages\n")
output.write("RTL_LANGS = {\n")
output.write("RTL_LANGS: set[str] = {\n")
for code in sorted(RTL_CODES):
output.write(f' "{code}",\n')
output.write("}\n")
Expand Down Expand Up @@ -302,21 +299,19 @@ words.difference_update(
with open("weblate_language_data/check_languages.py", "w") as output:
output.write(HEADER)
output.write("# Language names to ignore in same check\n")
output.write("LANGUAGES = {\n")
content = ", ".join(
"'{}'".format(word.replace("'", "\\'"))
for word in sorted(words)
if len(word) > 2 # noqa: PLR2004
)
output.write(content)
output.write("\n}\n")
output.write("LANGUAGES: set[str] = {\n")
for word in sorted(words):
if len(word) <= 2: # noqa: PLR2004
continue
output.write(f' "{escape(word)}",\n')
output.write("}\n")


# Write language codes
with open("weblate_language_data/language_codes.py", "w") as output:
output.write(HEADER)
output.write("# Known language codes\n")
output.write("LANGUAGES = {\n")
output.write("LANGUAGES: set[str] = {\n")
for word in sorted(CODES):
output.write(' "{}",\n'.format(word.replace('"', '\\"')))
output.write("}\n")
Expand All @@ -325,26 +320,7 @@ with open("weblate_language_data/language_codes.py", "w") as output:
with open("weblate_language_data/country_codes.py", "w") as output:
output.write(HEADER)
output.write("# Known country codes\n")
output.write("COUNTRIES = {\n")
output.write("COUNTRIES: set[str] = {\n")
for word in sorted(COUNTRIES):
output.write(' "{}",\n'.format(word.replace('"', '\\"')))
output.write("}\n")

# Apply coding style
subprocess.run(
[
"pre-commit",
"run",
"--files",
"weblate_language_data/rtl.py",
"weblate_language_data/countries.py",
"weblate_language_data/aliases.py",
"weblate_language_data/plurals.py",
"weblate_language_data/languages.py",
"weblate_language_data/population.py",
"weblate_language_data/check_languages.py",
"weblate_language_data/language_codes.py",
"weblate_language_data/country_codes.py",
],
check=False,
)
2 changes: 1 addition & 1 deletion weblate_language_data/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# Language aliases
ALIASES = {
ALIASES: dict[str, str] = {
"braz_por": "pt_BR",
"chinese": "zh_Hans",
"chinese_chs": "zh_Hans",
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/check_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# Language names to ignore in same check
LANGUAGES = {
LANGUAGES: set[str] = {
"aak",
"aakkâr",
"aargau",
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# List of defaul languages, omitting country code should be okay
DEFAULT_LANGS = (
DEFAULT_LANGS: tuple[str, ...] = (
"af_za",
"am_et",
"ar_aa",
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/country_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# Known country codes
COUNTRIES = {
COUNTRIES: set[str] = {
"abw",
"ad",
"ae",
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/language_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# Known language codes
LANGUAGES = {
LANGUAGES: set[str] = {
"aa",
"aar",
"ab",
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .utils import gettext_noop as _

# Language definitions
LANGUAGES = (
LANGUAGES: tuple[tuple[str, str, int, str], ...] = (
(
"aa",
# Translators: Language name for ISO code "aa". The parenthesis clarifies
Expand Down
6 changes: 3 additions & 3 deletions weblate_language_data/plurals.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .utils import gettext_noop as _

# Additional plural rules definitions
EXTRAPLURALS = (
EXTRAPLURALS: tuple[tuple[str, str, int, str], ...] = (
(
"br",
# Translators: Language name for ISO code "br". The parenthesis clarifies
Expand Down Expand Up @@ -216,7 +216,7 @@
),
)

CLDRPLURALS = (
CLDRPLURALS: tuple[tuple[str, str, int, str], ...] = (
(
"ca",
# Translators: Language name for ISO code "ca". The parenthesis clarifies
Expand Down Expand Up @@ -336,7 +336,7 @@
),
)

QTPLURALS = (
QTPLURALS: tuple[tuple[str, str, int, str], ...] = (
(
"aa",
# Translators: Language name for ISO code "aa". The parenthesis clarifies
Expand Down
2 changes: 1 addition & 1 deletion weblate_language_data/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# Language definitions
POPULATION = {
POPULATION: dict[str, int] = {
"aa": 2305971,
"ab": 111858,
"abr": 1729455,
Expand Down
Empty file added weblate_language_data/py.typed
Empty file.
2 changes: 1 addition & 1 deletion weblate_language_data/rtl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable=line-too-long,too-many-lines

# List of RTL languages
RTL_LANGS = {
RTL_LANGS: set[str] = {
"ae",
"aii",
"ajp",
Expand Down

0 comments on commit 8d10426

Please sign in to comment.