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

Using subclasses with __new__ #14426

Closed
Viicos opened this issue Jan 10, 2023 · 2 comments
Closed

Using subclasses with __new__ #14426

Viicos opened this issue Jan 10, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@Viicos
Copy link
Contributor

Viicos commented Jan 10, 2023

Bug Report

Noticed this behavior while working on typeddjango/djangorestframework-stubs#315, it seems that mypy isn't inferring the correct type in the following example:

from __future__ import annotations

class A:
    def __new__(cls) -> B:
        return B()

class B(A):
    pass

class C(A):
    pass

var1 = A()
reveal_type(var1) # Revealed type is "B", as excepted
var2 = C()
reveal_type(var2) # Revealed type is "C", should be "B" (pyright reports "B")

I suspect this is due to the fact that __new__ should return a subclass of the class being defined according to mypy. I think it is a reasonable idea (see #1020 (comment)) to allow arbitrary types to be allowed, altough this might not be considered a good practice. A use case can be found here (the linked PR above is trying to implement stubs for this).

@Viicos Viicos added the bug mypy got something wrong label Jan 10, 2023
@intgr
Copy link
Contributor

intgr commented Jan 12, 2023

I also found issue #8330 which seems to ask for the same thing.

@Viicos
Copy link
Contributor Author

Viicos commented Jan 12, 2023

Good catch, guess I missed it when searching for similar issues. Closing in favor of this one

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