From d9518d10fb4553a5e3e971ee785da8583484c4cc Mon Sep 17 00:00:00 2001 From: Elias Mueller Date: Fri, 26 Jun 2026 19:49:42 +0200 Subject: [PATCH 1/2] Fix attr overload ordering for type inference --- src/inject/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inject/__init__.py b/src/inject/__init__.py index be272db..4df24f9 100644 --- a/src/inject/__init__.py +++ b/src/inject/__init__.py @@ -614,11 +614,11 @@ def attr() -> Injectable: ... @t.overload -def attr(cls: t.Hashable) -> Injectable: ... +def attr(cls: type[T]) -> T: ... @t.overload -def attr(cls: type[T]) -> T: ... +def attr(cls: t.Hashable) -> Injectable: ... def attr(cls=_MISSING): From e54ad9e6bd3811dcff234fa39c8321a32da0f0cb Mon Sep 17 00:00:00 2001 From: Elias Mueller Date: Fri, 26 Jun 2026 20:07:15 +0200 Subject: [PATCH 2/2] Add typing guard for attr overload inference --- .github/workflows/tests.yaml | 15 +++++++++++++++ pyproject.toml | 16 +++++++++++++++- typing_checks/__init__.py | 0 typing_checks/attr.py | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 typing_checks/__init__.py create mode 100644 typing_checks/attr.py diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index c4b9cad..45e7ac5 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -53,6 +53,21 @@ jobs: # run: | # tox -e lint-mypy + types: + name: "Type checking (public API)" + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: "3.14" + - name: "Install tox" + run: | + pip install tox + - name: "Type checking (public API)" + run: | + tox -e lint-types + tests: name: "Run unit tests" runs-on: ubuntu-24.04 diff --git a/pyproject.toml b/pyproject.toml index f793b7a..86fa6c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,7 @@ defineConstant = { DEBUG = true } exclude = [] executionEnvironments = [] ignore = [] -include = ["src/inject", "tests"] +include = ["src/inject", "tests", "typing_checks"] pythonPlatform = "Linux" pythonVersion = "3.11" reportMissingImports = true @@ -275,6 +275,7 @@ env_list = [ "fmt-py", "fmt-toml", "lint-py", + "lint-types", #"lint-mypy", # TODO(pyctrl): make it green & uncomment "lint-toml", "lint-yaml", @@ -289,6 +290,7 @@ fmt = [ ] lint = [ "lint-py", + "lint-types", #"lint-mypy", # TODO(pyctrl): make it green & uncomment "lint-toml", "lint-yaml", @@ -325,6 +327,18 @@ commands = [ ], ] +[tool.tox.env.lint-types] +description = "Type-check the public-API typing guards" +deps = ["mypy"] +commands = [ + [ + "mypy", + { replace = "posargs", default = [ + "typing_checks", + ], extend = true }, + ], +] + [tool.tox.env.lint-mypy] description = "Type checking" deps = ["mypy"] diff --git a/typing_checks/__init__.py b/typing_checks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/typing_checks/attr.py b/typing_checks/attr.py new file mode 100644 index 0000000..250b250 --- /dev/null +++ b/typing_checks/attr.py @@ -0,0 +1,17 @@ +"""Static-typing regression guard for ``inject.attr``.""" + +from typing_extensions import assert_type + +import inject + + +class _Service: + pass + + +class _Repository: + pass + + +assert_type(inject.attr(_Service), _Service) +assert_type(inject.attr(_Repository), _Repository)