Skip to content

fix: restore static typing of inject.attr(Cls)#136

Merged
ivankorobkov merged 2 commits into
ivankorobkov:masterfrom
trin94:fix-attr-overload-order
Jun 27, 2026
Merged

fix: restore static typing of inject.attr(Cls)#136
ivankorobkov merged 2 commits into
ivankorobkov:masterfrom
trin94:fix-attr-overload-order

Conversation

@trin94

@trin94 trin94 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Hi, I recently opened issue #135 to report the static typing regression. Today, I looked into this myself using AI (Claude Opus 4.8).

The actual fix is tiny:

before:

@t.overload
def attr(cls: t.Hashable) -> Injectable: ...


@t.overload
def attr(cls: type[T]) -> T: ...

after:

@t.overload
def attr(cls: type[T]) -> T: ...


@t.overload
def attr(cls: t.Hashable) -> Injectable: ...
Verified locally via `uvx --with tox-uv tox -e lint-types`
python-inject on  fix-attr-overload-order [?] via 🐍 v3.10.0 (python-inject) uvx --with tox-uv tox -e lint-types
.pkg: _optional_hooks> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: get_requires_for_build_editable> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: build_editable> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
lint-types: install_package> /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/bin/uv pip install --reinstall --no-deps inject@/home/elias/PycharmProjects/python-inject/.tox/.tmp/package/24/inject-5.4.1.dev2+ge54ad9e6b.d20260626-py3-none-any.whl
lint-types: commands[0]> mypy typing_checks
typing_checks/attr.py:16: error: Expression is of type "object | Any", not "_Service"  [assert-type]
typing_checks/attr.py:17: error: Expression is of type "object | Any", not "_Repository"  [assert-type]
Found 2 errors in 1 file (checked 2 source files)
lint-types: exit 1 (0.10 seconds) /home/elias/PycharmProjects/python-inject> mypy typing_checks pid=39578
.pkg: _exit> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
  lint-types: FAIL code 1 (0.30=setup[0.20]+cmd[0.10] seconds)
  evaluation failed :( (0.34 seconds)

python-inject on  fix-attr-overload-order [!?] via 🐍 v3.10.0 (python-inject) uvx --with tox-uv tox -e lint-types
.pkg: _optional_hooks> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: get_requires_for_build_editable> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
.pkg: build_editable> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
lint-types: install_package> /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/bin/uv pip install --reinstall --no-deps inject@/home/elias/PycharmProjects/python-inject/.tox/.tmp/package/25/inject-5.4.1.dev2+ge54ad9e6b-py3-none-any.whl
lint-types: commands[0]> mypy typing_checks
Success: no issues found in 2 source files
.pkg: _exit> python /home/elias/.cache/uv/archive-v0/x3Z---987shE3V0B/lib/python3.10/site-packages/pyproject_api/_backend.py True hatchling.build
  lint-types: OK (0.30=setup[0.19]+cmd[0.11] seconds)
  congratulations :) (0.34 seconds)

We've basically swapping the order so that the typed one matches first.

The rest of the PR is adding a test + wiring it into the tox setup. I created a separate job as
tox -e lint-py (Lint formatting) is failing and the new public API type checks would never run.

Best

Fixes #135

@ivankorobkov

Copy link
Copy Markdown
Owner

Cool, thank you for the fix. I will make the next release now.

@ivankorobkov ivankorobkov merged commit 7d1a1d9 into ivankorobkov:master Jun 27, 2026
8 of 9 checks passed
@ivankorobkov

Copy link
Copy Markdown
Owner

Done https://pypi.org/project/inject/5.4.1/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inject 5.4.0 breaks static typing of inject.attr(Cls)

2 participants