Skip to content

Fix mypy#140

Merged
ivankorobkov merged 13 commits into
ivankorobkov:masterfrom
trin94:fix/typing-issues
Jul 6, 2026
Merged

Fix mypy#140
ivankorobkov merged 13 commits into
ivankorobkov:masterfrom
trin94:fix/typing-issues

Conversation

@trin94

@trin94 trin94 commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Hi again,

This is a follow-up to #139. It makes the red Type checking (library code) job green. On top of that it enables mypy strict mode. Like in #136, I worked on this with AI assistance (Claude).

Python 3.10 baseline cleanup

After dropping Python 3.9 (#137), some compatibility code is not needed anymore: the PEP 604/560 version checks, the typing_extensions ParamSpec fallback, and the private typing._GenericAlias / typing._Union imports (replaced with the public t.get_origin() / t.get_args()).

Type hints now use the PEP 604 union syntax (X | None). I also removed the matching pyupgrade ignores from the ruff config, so ruff keeps checking this.

Small find on the way: an earlier formatting commit changed the test annotation C | None to t.Optional[C]. So the PEP 604 test did not test PEP 604 anymore. I restored it. Now both spellings have their own test.

Mypy: 16 errors to zero, then strict

Fixed in small commits so each one is easy to review:

  • incomplete signatures (bare t.Callable, the untyped attr() implementation)
  • Injector.get_instance now has the same type[T] overload as the module-level instance(), with a new typing_checks guard like we added in fix: restore static typing of inject.attr(Cls) #136
  • the autoparams fn-overload is positional-only now. mypy was right here: the old type hints allowed autoparams(fn=...), but that call crashes at runtime. This only changes the type hints: at runtime, autoparams(fn=...) always raised a TypeError, before and after
  • union unwrapping goes through t.get_args() instead of .__args__

After that, mypy was at zero errors. So I enabled strict = true (this resolves the old TODO) and removed the single flags that strict already includes. Strict found one more wrong annotation in a private helper, which is also fixed.

There are no runtime behavior changes. The test matrix stays green on 3.10-3.14.

README

One small fix: the "Keys" sample was a SyntaxError (a missing dot between the chained bind calls). I also ran all the other README samples. Some more are outdated (the context-managers sample and the classmethod + inject.attr examples). I might fix those in a separate PR as this one is big already.

Suggestion: Make CI jobs mandatory before merging

In the recent PRs we locked the dependencies and cleaned up the code base and the CI. So new tool releases cannot break the CI anymore, a red job now means a real problem. And with this PR all jobs are green. I think this is a good moment to make the CI jobs mandatory for merging.

Thanks and all the best 😄
Elias

trin94 added 13 commits July 4, 2026 12:38
Python 3 does not require inheriting from object anymore.
Incomplete signatures were behind four of the remaining mypy errors.
The first overload returned T without taking it as a parameter,
which mypy rejected and made the second overload unreachable.
The `fn` overload can only be passed positionally by the real
implementation, and the wrapper can return an awaitable, so the
declared types now match runtime behavior instead of fighting mypy.
mypy flags __args__ as private since type doesn't declare it, so use
the public get_args API instead.
Strict mode implies these flags so they can be removed, and it
exposed one real typing issue that needed fixing.
@trin94 trin94 changed the title Fix/typing issues Fix mypy Jul 4, 2026
@ivankorobkov

ivankorobkov commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Great fixes, thanks. Do you plan to change anything else? Otherwise, this can be a solid next version.

@ivankorobkov ivankorobkov merged commit ce0bbcc into ivankorobkov:master Jul 6, 2026
9 checks passed
@trin94 trin94 deleted the fix/typing-issues branch July 6, 2026 08:40
@trin94

trin94 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@ivankorobkov

Do you plan to change anything else? Otherwise, this can be a solid next version.

No other changes planned at the moment 😅 Thank you for merging these PRs 😃

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.

2 participants