From 53bf1649dfb14beb3a02d66886196164a504b6fc Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 15 Aug 2024 15:02:44 +0100 Subject: [PATCH] Enable mypy with minimal checks (#260) Partial-Fix: #258 --- .pre-commit-config.yaml | 33 ++++++++++++-- .../eda/plugins/event_source/aws_sqs_queue.py | 2 +- .../eda/plugins/event_source/webhook.py | 2 +- pyproject.toml | 45 +++++++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ace19b40..c3c44f34 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,7 +24,7 @@ repos: hooks: - id: shellcheck - repo: https://github.com/streetsidesoftware/cspell-cli - rev: v8.13.1 + rev: v8.13.3 hooks: - id: cspell - repo: https://github.com/pycqa/isort @@ -36,8 +36,33 @@ repos: hooks: - id: black language_version: python3 + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.11.1 + hooks: + - id: mypy + # empty args needed in order to match mypy cli behavior + args: [] + additional_dependencies: + - aiohttp + - aiokafka + - ansible-core>=2.15 + - asyncmock + - azure-servicebus + - dpath + - kafka-python-ng; python_version >= "3.12" + - kafka-python; python_version < "3.12" + - psycopg[binary,pool] # extras needed to avoid install failure on macos-aarch64 + - pytest + - types-PyYAML + - types-aiobotocore[cloudtrail,sqs] + - types-botocore + - types-mock + - types-requests + - watchdog + - xxhash + - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.5.6" + rev: "v0.5.7" hooks: - id: ruff args: [ @@ -63,17 +88,19 @@ repos: - aiobotocore - aiohttp - aiokafka + - ansible-core - asyncmock - azure-servicebus + - botocore - dpath - kafka-python - psycopg - pytest - pyyaml - requests + - types-aiobotocore - watchdog - xxhash - - ansible-core - repo: local hooks: - id: ansible-test-sanity diff --git a/extensions/eda/plugins/event_source/aws_sqs_queue.py b/extensions/eda/plugins/event_source/aws_sqs_queue.py index 7ee5ba95..0fc2c749 100644 --- a/extensions/eda/plugins/event_source/aws_sqs_queue.py +++ b/extensions/eda/plugins/event_source/aws_sqs_queue.py @@ -64,7 +64,7 @@ async def main(queue: asyncio.Queue, args: dict[str, Any]) -> None: ) if "Messages" in response: - for msg in response["Messages"]: + for msg in response["Messages"]: # type: ignore[typeddict-item] meta = {"MessageId": msg["MessageId"]} try: msg_body = json.loads(msg["Body"]) diff --git a/extensions/eda/plugins/event_source/webhook.py b/extensions/eda/plugins/event_source/webhook.py index 4e3478e1..9bc02a08 100644 --- a/extensions/eda/plugins/event_source/webhook.py +++ b/extensions/eda/plugins/event_source/webhook.py @@ -74,7 +74,7 @@ async def webhook(request: web.Request) -> web.Response: return web.Response(text=endpoint) -def _parse_token(request: web.Request) -> (str, str): +def _parse_token(request: web.Request) -> tuple[str, str]: scheme, token = request.headers["Authorization"].strip().split(" ") if scheme != "Bearer": raise web.HTTPUnauthorized(text="Only Bearer type is accepted") diff --git a/pyproject.toml b/pyproject.toml index b5d00fa4..00ff6ca1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,51 @@ use_parentheses = true ensure_newline_before_comments = true line_length = 120 +[tool.mypy] +python_version = "3.9" +color_output = true +error_summary = true + +# TODO: Remove temporary skips and close https://github.com/ansible/event-driven-ansible/issues/258 +disable_error_code = [ + "arg-type", + "assignment", + "attr-defined", + "index", + "misc", + "override", + "return", + "return-value", + "union-attr", + "var-annotated", +] +# strict = true +# disallow_untyped_calls = true +# disallow_untyped_defs = true +# disallow_any_generics = true +# disallow_any_unimported = True +# warn_redundant_casts = True +# warn_return_any = True +# warn_unused_configs = True + +# site-packages is here to help vscode mypy integration getting confused +exclude = "(build|dist|test/local-content|site-packages|~/.pyenv|examples/playbooks/collections|plugins/modules)" +# https://github.com/python/mypy/issues/12664 +incremental = false +namespace_packages = true +explicit_package_bases = true + +[[tool.mypy.overrides]] +module = [ + # Dependencies not following pep-561 yet: + "aiokafka.*", # https://github.com/aio-libs/aiokafka/issues/980 + "ansible.*", # https://github.com/ansible/ansible/issues/83801 + "asyncmock", # https://github.com/timsavage/asyncmock/issues/8 + # "botocore.*", # https://github.com/boto/botocore/issues/2297 + "kafka.*", # https://github.com/dpkp/kafka-python/issues/2446 +] +ignore_missing_imports = true + [tool.pylint.MASTER] # Temporary ignore until we are able to address issues on these: ignore-paths = "^(demos/dynatrace-demo/fake_app.py|tests/|plugins/modules).*$"