Skip to content

Add infrastructure for type checking in Python code #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/check-python-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ on:
- "**/poetry.lock"
- "**/pyproject.toml"
- "**/setup.cfg"
- ".mypy.ini"
- "mypy.ini"
- "Taskfile.ya?ml"
- "**/tox.ini"
- "**.py"
Expand All @@ -25,6 +27,8 @@ on:
- "**/poetry.lock"
- "**/pyproject.toml"
- "**/setup.cfg"
- ".mypy.ini"
- "mypy.ini"
- "Taskfile.ya?ml"
- "**/tox.ini"
- "**.py"
Expand Down Expand Up @@ -120,3 +124,34 @@ jobs:

- name: Check formatting
run: git diff --color --exit-code

types:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Poetry
run: pip install poetry

- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x

- name: Check types
uses: liskin/gh-problem-matcher-wrap@v3
with:
linters: mypy
run: task python:check-types
11 changes: 11 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tasks:
- task: markdown:lint
- task: npm:validate
- task: poetry:validate
- task: python:check-types
- task: python:lint
- task: python:test

Expand Down Expand Up @@ -233,6 +234,16 @@ tasks:
poetry run \
coverage report

python:check-types:
desc: Check types in Python files
deps:
- task: poetry:install-deps
cmds:
- |
poetry run \
mypy \
.

# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
python:format:
desc: Format Python files
Expand Down
59 changes: 58 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ flake8 = "6.1.0"
pep8-naming = "0.13.3"
pytest = "7.4.3"
pytest-mock = "3.12.0"
mypy = "1.7.1"

[build-system]
requires = ["poetry-core"]
Expand Down
11 changes: 4 additions & 7 deletions reportsizedeltas/reportsizedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ def comment_report(self, pr_number: int, report_markdown: str) -> None:
report_markdown -- Markdown formatted report
"""
print("::debug::Adding deltas report comment to pull request")
report_data = {"body": report_markdown}
report_data = json.dumps(obj=report_data)
report_data = report_data.encode(encoding="utf-8")
report_data = json.dumps(obj={"body": report_markdown}).encode(encoding="utf-8")
url = "https://api.github.com/repos/" + self.repository_name + "/issues/" + str(pr_number) + "/comments"

self.http_request(url=url, data=report_data)
Expand Down Expand Up @@ -581,7 +579,7 @@ def get_json_response(self, url: str):
except Exception as exception:
raise exception

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

def raw_http_request(self, url: str, data: str | None = None):
def raw_http_request(self, url: str, data: bytes | None = None):
"""Make a request and return an object containing the response.

Keyword arguments:
Expand Down Expand Up @@ -710,8 +708,7 @@ def get_page_count(link_header: str | None) -> int:
# Get the pagination data
for link in link_header.split(","):
if link[-13:] == '>; rel="last"':
link = re.split("[?&>]", link)
for parameter in link:
for parameter in re.split("[?&>]", link):
if parameter[:5] == "page=":
page_count = int(parameter.split("=")[1])
break
Expand Down
8 changes: 5 additions & 3 deletions reportsizedeltas/tests/test_reportsizedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@


def get_reportsizedeltas_object(
repository_name="FooOwner/BarRepository", sketches_reports_source="foo-artifact-name", token="foo token"
):
repository_name: str = "FooOwner/BarRepository",
sketches_reports_source: str = "foo-artifact-name",
token: str = "foo token",
) -> reportsizedeltas.ReportSizeDeltas:
"""Return a reportsizedeltas.ReportSizeDeltas object to use in tests.

Keyword arguments:
Expand All @@ -33,7 +35,7 @@ def get_reportsizedeltas_object(
)


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

Expand Down