From 698e5b6892f220bf0cafaa4743d52b32d2b6a3bf Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 7 Oct 2025 21:04:19 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Replace=20`distlib`=20type=20ign?= =?UTF-8?q?ores=20with=20stubs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stubs are taken from the unmerged PR [[1]] upstream. MyPy is set up to rely on them instead of the missing package type annotations. [1]: https://github.com/pypa/distlib/pull/195 --- MANIFEST.in | 2 +- lib/ansible/galaxy/collection/__init__.py | 4 ++-- .../code-smell/mypy/_type_stubs/distlib/__init__.pyi | 5 +++++ .../code-smell/mypy/_type_stubs/distlib/manifest.pyi | 8 ++++++++ test/sanity/code-smell/mypy/ansible-core.ini | 2 ++ 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 test/sanity/code-smell/mypy/_type_stubs/distlib/__init__.pyi create mode 100644 test/sanity/code-smell/mypy/_type_stubs/distlib/manifest.pyi diff --git a/MANIFEST.in b/MANIFEST.in index fa609f52e9a..7b55f2e515c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -5,6 +5,6 @@ include licenses/*.txt include requirements.txt recursive-include packaging *.py *.j2 recursive-include test/integration * -recursive-include test/sanity *.in *.json *.py *.txt *.ini +recursive-include test/sanity *.in *.json *.py *.pyi *.txt *.ini recursive-include test/support *.py *.ps1 *.psm1 *.cs *.md recursive-include test/units * diff --git a/lib/ansible/galaxy/collection/__init__.py b/lib/ansible/galaxy/collection/__init__.py index 5656227e7e1..8a496f79f96 100644 --- a/lib/ansible/galaxy/collection/__init__.py +++ b/lib/ansible/galaxy/collection/__init__.py @@ -44,8 +44,8 @@ else: HAS_PACKAGING = True try: - from distlib.manifest import Manifest # type: ignore[import] - from distlib import DistlibException # type: ignore[import] + from distlib.manifest import Manifest + from distlib import DistlibException except ImportError: HAS_DISTLIB = False else: diff --git a/test/sanity/code-smell/mypy/_type_stubs/distlib/__init__.pyi b/test/sanity/code-smell/mypy/_type_stubs/distlib/__init__.pyi new file mode 100644 index 00000000000..04a64762b4d --- /dev/null +++ b/test/sanity/code-smell/mypy/_type_stubs/distlib/__init__.pyi @@ -0,0 +1,5 @@ +# NOTE: `distlib` is untyped as of 10 Oct 2025. +# NOTE: This should be removed once types are upstream. +# Ref: https://github.com/pypa/distlib/pull/195 + +class DistlibException(Exception): ... diff --git a/test/sanity/code-smell/mypy/_type_stubs/distlib/manifest.pyi b/test/sanity/code-smell/mypy/_type_stubs/distlib/manifest.pyi new file mode 100644 index 00000000000..4adaa5785c2 --- /dev/null +++ b/test/sanity/code-smell/mypy/_type_stubs/distlib/manifest.pyi @@ -0,0 +1,8 @@ +# NOTE: `distlib` is untyped as of 10 Oct 2025. +# NOTE: This should be removed once types are upstream. +# Ref: https://github.com/pypa/distlib/pull/195 + +class Manifest: + def __init__(self, base: str | None = ...) -> None: ... + def process_directive(self, directive: str) -> None: ... + def sorted(self, wantdirs: bool = ...) -> list[str]: ... diff --git a/test/sanity/code-smell/mypy/ansible-core.ini b/test/sanity/code-smell/mypy/ansible-core.ini index 24414954c69..9a965e80798 100644 --- a/test/sanity/code-smell/mypy/ansible-core.ini +++ b/test/sanity/code-smell/mypy/ansible-core.ini @@ -13,6 +13,8 @@ strict_optional = False # See: https://github.com/python/mypy/issues/4717 disable_error_code = attr-defined,safe-super,type-abstract +mypy_path = ${MYPY_CONFIG_FILE_DIR}/_type_stubs + # Some controller unit tests use ansible_collections imports, both real and test-specific. # The real imports are not currently visible to mypy. # There's little point in exposing the test-specific imports.