Skip to content

object.__new__(cls) fails type check inside generic classes #3120

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

Closed
pkch opened this issue Apr 3, 2017 · 2 comments
Closed

object.__new__(cls) fails type check inside generic classes #3120

pkch opened this issue Apr 3, 2017 · 2 comments

Comments

@pkch
Copy link
Contributor

pkch commented Apr 3, 2017

(Based on #3111)

This fails with Argument 1 to "__new__" of "object" has incompatible type C[T]; expected "object"

T = TypeVar('T')
class C(Generic[T]):
    def __new__(cls, foo: T) -> 'C[T]':
        obj = object.__new__(cls)
        return obj
    def set(self, x: T) -> None: pass

Edit: deleted my own incorrect interpretation.

@pkch
Copy link
Contributor Author

pkch commented Apr 20, 2017

This also happens when __init__ is present and has non-zero arguments in the signature:

class C:
    def __init__(self, x: int) -> None:
        pass
    def __new__(cls, *args) -> 'C':
        obj = object.__new__(cls)  # E:  Argument 1 to "__new__" of "object" has incompatible type "C"; expected "object"
        return obj

However, this passes:

class C:
    def __new__(cls) -> 'C':
        obj = object.__new__(cls)
        return obj

Somehow the presence of arguments in the signature prevents mypy from matching the class object to the expected object type.

@msullivan
Copy link
Collaborator

#4190 is a dupe of this but I'm closing this one because that one got categorized

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

No branches or pull requests

2 participants