rodi
rodi copied to clipboard
Use `Self` type for `Container`
I've improved the self annotations for the Container type. So, here's how it works:
- There's a dedicated
Selftype intypingandtyping_extensionsthat we cannot use here, becausetyping_extensionsis not required forpython>=3.9andSelfwas only added totypingin 3.11 or 3.12 - Here's the difference between
-> "Container"and-> _ContainerSelfthat I am using now:
from typing import TypeVar
_S = TypeVar('_S', bound='My')
class My:
def with_s(self: _S) -> _S:
return self
def with_annotation(self) -> 'My':
return self
class Child(My): ...
reveal_type(Child().with_s())
# N: Revealed type is "__main__.Child"
reveal_type(Child().with_annotation())
# N: Revealed type is "__main__.My"
Link: https://mypy-play.net/?mypy=latest&python=3.12&gist=58161f0ca82a956a0f893c4f86a90786
And since I am using a Container subclass to specialize dep injection a bit - it does matter to me.