8000 Use self-type in `__init__` to add extra generic types · Issue #16752 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Use self-type in __init__ to add extra generic types #16752

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 fo 8000 r 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

Closed
A5rocks opened this issue Jan 6, 2024 · 4 comments
Closed

Use self-type in __init__ to add extra generic types #16752

A5rocks opened this issue Jan 6, 2024 · 4 comments
Labels

Comments

@A5rocks
Copy link
Collaborator
A5rocks commented Jan 6, 2024

Feature

Allow using self-types to add intermediate generic types for __init__.

Pitch

This would make python-trio/trio#2898 possible to type well without the awful hacks we had to do. Specifically making sure RaisesGroup(RaisesGroup(ValueError)) is RaisesGroup[ExceptionGroup[ValueError]].

Perhaps a code sample would make more sense:

from typing import Generic, TypeVar, overload

E = TypeVar("E", bound=BaseException)
E2 = TypeVar("E2", bound=BaseException)

class RaisesGroup(Generic[E]):
    @overload
    def __init__(self, a: type[E]):
        ...
    
    @overload
    def __init__(self: "RaisesGroup[BaseExceptionGroup[E2]]", a: "RaisesGroup[E2]"):
        ...
    
    def __init__(self, a: object):
        ...


RaisesGroup(RaisesGroup(ValueError))  # currently: Argument 1 to "RaisesGroup" has incompatible type "RaisesGroup[ValueError]"; expected "RaisesGroup[Never]"
@A5rocks A5rocks added the feature label Jan 6, 2024
@A5rocks A5rocks changed the title Use self-type in __init__ to Use self-type in __init__ to add extra generic types Jan 6, 2024
@erictraut
Copy link
erictraut commented Jan 6, 2024

We should try to formally define the intended behavior for this construct. All of the major type checkers treat the self parameter in the __init__ method specially, but the behavior has never been formally spec'ed. It's therefore not surprising that we see different behaviors between type checkers. For example, pyright accepts the above code, and it works they way you'd like.

If you would like to see this become part of the official typing spec, I encourage you to write a mini spec and post it in the python/typing forum for feedback. My sense is that this probably doesn't require a full PEP. If the spec is relatively straightforward and uncontroversial, it's something that the recently-formed Typing Council could ratify and incorporate into the typing spec.

@A5rocks
Copy link
Collaborator Author
A5rocks commented Jan 6, 2024

I'll see if I can express this in a formal manner, though I'm not sure I'm able to. For reference, this was a 1 line (... ok maybe technically +2-1) change in #16753

@Viicos
Copy link
Contributor
Viicos commented Jan 8, 2024

I've opened a (draft) issue on the python/typing repository, as I'm also interested in having this feature formally specified so that I can make use of it in a "safe" way. This could also be forwarded to the typing forum if necessary.

@A5rocks
Copy link
Collaborator Author
A5rocks commented Jun 16, 2024

This works in mypy nowadays (or at least my initial example does), as of #17348

Thanks @ilevkivskyi!

It's possible there's other examples that don't work. I'm not sure.

@A5rocks A5rocks closed this as completed Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0