Skip to content

Incorrect __init__ signature for classes inheriting from Generic #12655

@dhirschfeld

Description

@dhirschfeld

Correct __init__ signature:

In [21]: class C:
    ...:     """A docstring."""
    ...:     def __init__(self, a: float, b:int, c:str):
    ...:         pass
    ...:         

In [22]: C?
Init signature: C(a: float, b: int, c: str)
Docstring:      A docstring.
Type:           type
Subclasses:     

Incorrect signature when inheriting from Generic:

In [23]: from typing import Generic, TypeVar

In [24]: T = TypeVar('T')

In [25]: class C(Generic[T]):
    ...:     """A docstring."""
    ...:     def __init__(self, a: float, b:int, c:str):
    ...:         pass
    ...:         

In [26]: C?
Init signature: C(*args, **kwds)
Docstring:      A docstring.
Type:           type
Subclasses:     

help does work correctly in this case:

In [27]: help(C)
Help on class C in module __main__:

class C(typing.Generic)
 |  C(*args, **kwds)
 |  
 |  A docstring.
 |  
 |  Method resolution order:
 |      C
 |      typing.Generic
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, a: float, b: int, c: str)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
<SNIP>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions