Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible regression: incorrectly ret type detected in of __new__ #11835

Open
neutrinoceros opened this issue Dec 23, 2021 · 2 comments
Open

Possible regression: incorrectly ret type detected in of __new__ #11835

neutrinoceros opened this issue Dec 23, 2021 · 2 comments
Labels
bug mypy got something wrong

Comments

@neutrinoceros
Copy link

neutrinoceros commented Dec 23, 2021

Bug Report

new versions of mypy seem to misinterpret the return type of the __new__ method defined below

To Reproduce

# t.py
class A:
    def __new__(cls, *args, **kwargs) -> "B | C":
        if cls is A:
            cls = B
        else:
            cls = C
        return object.__new__(cls)

class B(A):
    pass

class C(A):
    pass

checking this file gives

$ 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):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
                                      % (cls.__name__,))
        return self

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
@neutrinoceros neutrinoceros added the bug mypy got something wrong label Dec 23, 2021
@sobolevn
Copy link
Member

Moreover, this code is not even valid:

class A:
    def __new__(cls, *args, **kwargs) -> "B" | "C":
        ...

It is a runtime error:

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'

It should be: "B | C"

@neutrinoceros
Copy link
Author

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants