Skip to content

Commit 0d14d3a

Browse files
authored
Merge pull request #68 from per1234/mypy
Add infrastructure for type checking in Python code
2 parents 19f5e91 + ba7c42a commit 0d14d3a

File tree

6 files changed

+114
-11
lines changed

6 files changed

+114
-11
lines changed

.github/workflows/check-python-task.yml

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ on:
1515
- "**/poetry.lock"
1616
- "**/pyproject.toml"
1717
- "**/setup.cfg"
18+
- ".mypy.ini"
19+
- "mypy.ini"
1820
- "Taskfile.ya?ml"
1921
- "**/tox.ini"
2022
- "**.py"
@@ -25,6 +27,8 @@ on:
2527
- "**/poetry.lock"
2628
- "**/pyproject.toml"
2729
- "**/setup.cfg"
30+
- ".mypy.ini"
31+
- "mypy.ini"
2832
- "Taskfile.ya?ml"
2933
- "**/tox.ini"
3034
- "**.py"
@@ -120,3 +124,34 @@ jobs:
120124

121125
- name: Check formatting
122126
run: git diff --color --exit-code
127+
128+
types:
129+
needs: run-determination
130+
if: needs.run-determination.outputs.result == 'true'
131+
runs-on: ubuntu-latest
132+
permissions:
133+
contents: read
134+
135+
steps:
136+
- name: Checkout repository
137+
uses: actions/checkout@v4
138+
139+
- name: Install Python
140+
uses: actions/setup-python@v5
141+
with:
142+
python-version: ${{ env.PYTHON_VERSION }}
143+
144+
- name: Install Poetry
145+
run: pip install poetry
146+
147+
- name: Install Task
148+
uses: arduino/setup-task@v1
149+
with:
150+
repo-token: ${{ secrets.GITHUB_TOKEN }}
151+
version: 3.x
152+
153+
- name: Check types
154+
uses: liskin/gh-problem-matcher-wrap@v3
155+
with:
156+
linters: mypy
157+
run: task python:check-types

Taskfile.yml

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tasks:
1616
- task: markdown:lint
1717
- task: npm:validate
1818
- task: poetry:validate
19+
- task: python:check-types
1920
- task: python:lint
2021
- task: python:test
2122

@@ -233,6 +234,16 @@ tasks:
233234
poetry run \
234235
coverage report
235236
237+
python:check-types:
238+
desc: Check types in Python files
239+
deps:
240+
- task: poetry:install-deps
241+
cmds:
242+
- |
243+
poetry run \
244+
mypy \
245+
.
246+
236247
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
237248
python:format:
238249
desc: Format Python files

poetry.lock

+58-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ flake8 = "6.1.0"
1818
pep8-naming = "0.13.3"
1919
pytest = "7.4.3"
2020
pytest-mock = "3.12.0"
21+
mypy = "1.7.1"
2122

2223
[build-system]
2324
requires = ["poetry-core"]

reportsizedeltas/reportsizedeltas.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,7 @@ def comment_report(self, pr_number: int, report_markdown: str) -> None:
516516
report_markdown -- Markdown formatted report
517517
"""
518518
print("::debug::Adding deltas report comment to pull request")
519-
report_data = {"body": report_markdown}
520-
report_data = json.dumps(obj=report_data)
521-
report_data = report_data.encode(encoding="utf-8")
519+
report_data = json.dumps(obj={"body": report_markdown}).encode(encoding="utf-8")
522520
url = "https://api.github.com/repos/" + self.repository_name + "/issues/" + str(pr_number) + "/comments"
523521

524522
self.http_request(url=url, data=report_data)
@@ -581,7 +579,7 @@ def get_json_response(self, url: str):
581579
except Exception as exception:
582580
raise exception
583581

584-
def http_request(self, url: str, data: str | None = None) -> dict[str]:
582+
def http_request(self, url: str, data: bytes | None = None):
585583
"""Make a request and return a dictionary:
586584
read -- the response
587585
info -- headers
@@ -599,7 +597,7 @@ def http_request(self, url: str, data: str | None = None) -> dict[str]:
599597
"url": response_object.geturl(),
600598
}
601599

602-
def raw_http_request(self, url: str, data: str | None = None):
600+
def raw_http_request(self, url: str, data: bytes | None = None):
603601
"""Make a request and return an object containing the response.
604602
605603
Keyword arguments:
@@ -710,8 +708,7 @@ def get_page_count(link_header: str | None) -> int:
710708
# Get the pagination data
711709
for link in link_header.split(","):
712710
if link[-13:] == '>; rel="last"':
713-
link = re.split("[?&>]", link)
714-
for parameter in link:
711+
for parameter in re.split("[?&>]", link):
715712
if parameter[:5] == "page=":
716713
page_count = int(parameter.split("=")[1])
717714
break

reportsizedeltas/tests/test_reportsizedeltas.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020

2121
def get_reportsizedeltas_object(
22-
repository_name="FooOwner/BarRepository", sketches_reports_source="foo-artifact-name", token="foo token"
23-
):
22+
repository_name: str = "FooOwner/BarRepository",
23+
sketches_reports_source: str = "foo-artifact-name",
24+
token: str = "foo token",
25+
) -> reportsizedeltas.ReportSizeDeltas:
2426
"""Return a reportsizedeltas.ReportSizeDeltas object to use in tests.
2527
2628
Keyword arguments:
@@ -33,7 +35,7 @@ def get_reportsizedeltas_object(
3335
)
3436

3537

36-
def directories_are_same(left_directory, right_directory):
38+
def directories_are_same(left_directory, right_directory) -> bool:
3739
"""Check recursively whether two directories contain the same files.
3840
Based on https://stackoverflow.com/a/6681395
3941

0 commit comments

Comments
 (0)