Skip to content

Commit

Permalink
chore(ci): introduce mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Oct 21, 2024
1 parent ccc031a commit 4d8886e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 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/
70 changes: 70 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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
- name: Get changed files
if: github.event_name == 'pull_request'
id: changed-files
uses: tj-actions/changed-files@v45
with:
files: '**.py'
- name: List all changed files
if: github.event_name == 'pull_request'
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
touch changed-files.txt
for file in ${ALL_CHANGED_FILES}; do
echo "$file" >> changed-files.txt
done
cat changed-files.txt
- uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: ''
cache-suffix: '3.12'
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install pip dependencies
run: uv pip install --system -r requirements-dev.txt

- name: Run mypy
run: mypy --show-column-numbers weblate_web > mypy.log
# Temporary hack until we have this fully working
continue-on-error: true
- name: Report mypy
if: always()
run: |
echo "::add-matcher::.github/matchers/mypy.json"
if [ -f changed-files.txt ] ; then
if grep --silent --fixed-strings --file=changed-files.txt mypy.log ; then
grep --fixed-strings --file=changed-files.txt mypy.log
fi
else
cat mypy.log
fi
echo "::remove-matcher owner=mypy::"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
cache-dependency-glob: ''
cache-suffix: ${{ matrix.python-version }}
- name: Install pip dependencies
run: uv pip install --system -r requirements.txt -r requirements-test.txt
run: uv pip install --system -r requirements-dev.txt
- name: Compile MO files
run: ./scripts/generate-locales
- name: Collect static files
Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
[tool.black]
target-version = ['py311']

[tool.django-stubs]
django_settings_module = "weblate_web.settings"

[tool.isort]
profile = "black"

[tool.mypy]
check_untyped_defs = true
plugins = [
"mypy_django_plugin.main",
"mypy_drf_plugin.main"
]

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "weblate_web.settings"
addopts = "--reuse-db --cov=weblate_web --cov-report="
Expand Down Expand Up @@ -74,3 +84,5 @@ max-complexity = 16
"scripts/*" = ["T201"]
"weblate_web/migrations/0031_fill_in_customer.py" = ["T201"]
"weblate_web/payments/backends.py" = ["T201"]

# strict_settings = false
8 changes: 8 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
-r requirements-lint.txt
-r requirements-test.txt
-r requirements.txt
django-stubs==5.1.0
django-stubs-ext==5.1.0
djangorestframework-stubs==3.15.1
mypy==1.12.1
PyICU
translate-toolkit
types-lxml==2024.9.16
types-paramiko==3.5.0.20240928
types-python-dateutil==2.9.0.20241003
types-requests==2.32.0.20241016
11 changes: 6 additions & 5 deletions weblate_web/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
def url_fetcher(url: str) -> dict[str, str | bytes]:
if not url.startswith(INVOICES_URL):
raise ValueError(f"Usupported URL: {url}")
filename = url.removeprefix(INVOICES_URL)
basename = url.removeprefix(INVOICES_URL)
filename = TEMPLATES_PATH / basename
result = {
"filename": filename,
"string": (TEMPLATES_PATH / filename).read_bytes(),
"filename": basename,
"string": filename.read_bytes(),
}
if filename.endswith("css"):
if basename.endswith("css"):
result["mime_type"] = "text/css"
result["encoding"] = "utf-8"
return result

Check failure on line 51 in weblate_web/invoices/models.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible return value type (got "dict[str, Sequence[object]]", expected "dict[str, str | bytes]")

Check failure on line 51 in weblate_web/invoices/models.py

View workflow job for this annotation

GitHub Actions / mypy

Incompatible return value type (got "dict[str, Sequence[object]]", expected "dict[str, str | bytes]")
Expand Down Expand Up @@ -269,7 +270,7 @@ def display_price(self):
def display_total_price(self):
return self.invoice.render_amount(self.unit_price * self.quantity)

def get_quantity_unit_display(self) -> str:
def get_quantity_unit_display(self) -> str: # types: ignore[no-redef]

Check failure on line 273 in weblate_web/invoices/models.py

View workflow job for this annotation

GitHub Actions / mypy

Name "get_quantity_unit_display" already defined on line 251

Check failure on line 273 in weblate_web/invoices/models.py

View workflow job for this annotation

GitHub Actions / mypy

Name "get_quantity_unit_display" already defined on line 251
# Correcly handle singulars
if self.quantity_unit == QuantityUnitChoices.HOURS and self.quantity == 1:
return "hour"
Expand Down

0 comments on commit 4d8886e

Please sign in to comment.