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
$ mypy t.py
t_typ.py:2: error: "__new__" must return a class instance (got "Union[B, C]")
t_typ.py:7: error: Incompatible return value type (got "A", expected "Union[B, C]")
Found 2 errors in 1 file (checked 1 source file)
The first error is not new and was already discussed in #1020, I'm okay with it being marked as "won't fix" even though my opinion diverges.
The second error however (error: Incompatible return value type (got "A", expected "Union[B, C]")) appeared at some point after mypy 0.910 and mypy 0.920, and I believe that it is not correct.
Indeed the return type of the method is detected as A, but it is guarded against by the if/else clause above.
For reference, a pattern similar to the one I'm usin described can be found in CPython's standard library, in pathlib.Path.__new__:
def__new__(cls, *args, **kwargs):
ifclsisPath:
cls=WindowsPathifos.name=='nt'elsePosixPathself=cls._from_parts(args)
ifnotself._flavour.is_supported:
raiseNotImplementedError("cannot instantiate %r on your system"% (cls.__name__,))
returnself
Expected Behavior
Only the first error should show up. None at all would be ideal in my opinion, but that seems out of line here.
Your Environment
Mypy version used: 0.910 and 0.930
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Python version used: 3.10.0
Operating system and version: MacOS 12.0.1
The text was updated successfully, but these errors were encountered:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in A
TypeError: unsupported operand type(s) for |: 'str' and 'str'
oh sorry, I got excited and tried to use Python 3.10 syntax, but the actual code I'm checking is using Python 3.7 compatible idioms, I'll edit the OP. Thanks !
Bug Report
new versions of mypy seem to misinterpret the return type of the
__new__
method defined belowTo Reproduce
checking this file gives
The first error is not new and was already discussed in #1020, I'm okay with it being marked as "won't fix" even though my opinion diverges.
The second error however (
error: Incompatible return value type (got "A", expected "Union[B, C]")
) appeared at some point after mypy 0.910 and mypy 0.920, and I believe that it is not correct.Indeed the return type of the method is detected as
A
, but it is guarded against by the if/else clause above.For reference, a pattern similar to the one I'm usin described can be found in CPython's standard library, in
pathlib.Path.__new__
:Expected Behavior
Only the first error should show up. None at all would be ideal in my opinion, but that seems out of line here.
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: