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

@A5rocks

Description

@A5rocks

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]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0