You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
overloaded __new__ ignored when introducing __init__ #17251
Bug Report
I'm hitting a problem where the return type of an overloaded __new__ is ignored once an __init__ is added. I've searched the issue tracker thoroughly and while I've found a ton of issues related to __new__ return types I haven't found one that seems to be the same thing.
I've also found it ~impossible to type the signatures for the __new__ and __init__ implementation, but that's a separate issue.
Actual Behavior
$ mypy foo.py
foo.py:36: note: Revealed type is "foo.MyBaseClass[builtins.ValueError]"
foo.py:37: note: Revealed type is "foo.MyBaseClass[builtins.KeyboardInterrupt]"
pyright handles it without issue.
$ pyright foo.py
./foo.py:36:13 - information: Type of "MyBaseClass(ValueError)" is "MyClass[ValueError]"
./foo.py:37:13 - information: Type of "MyBaseClass(KeyboardInterrupt)" is "MyBaseClass[KeyboardInterrupt]"
and if I remove the __init__ from MyBaseClass then mypy handles the overloads correctly:
$ mypy foo.py
foo.py:36: note: Revealed type is "foo.MyClass[builtins.ValueError]"
foo.py:37: note: Revealed type is "foo.MyBaseClass[builtins.KeyboardInterrupt]"
Success: no issues found in 1 source file
Your Environment
Mypy version used: 1.10.0
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Python version used: 3.11.8
The source of the problem is trying to improve the typing of trio.testing.RaisesGroup: python-trio/trio#2989
I just ran into this while working on the stubs for scipy.sparse.linalg.LinearOperator (docs) in scipy-stubs (github).
For what it's worth; I worked around it by removing the __init__ stub altogether, and ignore the resulting stubtest errors. It's not pretty, but it's the only way I could find that didn't involve removing the overloads.
Activity
jakkdl commentedon Jan 28, 2025
The discussion in #15182 starting from #15182 (comment) is probably related; where they find a lot of confusing logic pertaining to
__new__+__init__jorenham commentedon Jul 5, 2025
I just ran into this while working on the stubs for
scipy.sparse.linalg.LinearOperator(docs) inscipy-stubs(github).For what it's worth; I worked around it by removing the
__init__stub altogether, and ignore the resulting stubtest errors. It's not pretty, but it's the only way I could find that didn't involve removing the overloads.