Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Self type for Container #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sobolevn
Copy link

I've improved the self annotations for the Container type. So, here's how it works:

  1. There's a dedicated Self type in typing and typing_extensions that we cannot use here, because typing_extensions is not required for python>=3.9 and Self was only added to typing in 3.11 or 3.12
  2. Here's the difference between -> "Container" and -> _ContainerSelf that 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.

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.

1 participant