[New-bugs-announce] [issue40897] Inheriting from Generic causes inspect.signature to always return (*args, **kwargs) for constructor (and all subclasses)

Edward Yang report at bugs.python.org
Sat Jun 6 23:37:38 EDT 2020


New submission from Edward Yang <ezyang at mit.edu>:

Consider the following program:

```
import inspect
from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
    def __init__(self) -> None:
        pass

print(inspect.signature(A))
```

I expect inspect.signature to return () as the signature of the constructor of this function. However, I get this:

```
$ python3 foo.py
(*args, **kwds)
```

Although it is true that one cannot generally rely on inspect.signature to always give the most accurate signature (because there may always be decorator or metaclass shenanigans getting in the way), in this particular case it seems especially undesirable because Python type annotations are supposed to be erased at runtime, and yet here inheriting from Generic (simply to add type annotations) causes a very clear change in runtime behavior.

----------
components: Library (Lib)
messages: 370870
nosy: ezyang
priority: normal
severity: normal
status: open
title: Inheriting from Generic causes inspect.signature to always return (*args, **kwargs) for constructor (and all subclasses)
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40897>
_______________________________________


More information about the New-bugs-announce mailing list