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

Check attributes for variance violations #735

Open
JukkaL opened this issue Aug 9, 2015 · 4 comments
Open

Check attributes for variance violations #735

JukkaL opened this issue Aug 9, 2015 · 4 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 9, 2015

Similar to #734, but for attributes. In a covariant class an instance attribute with type List[T] would not be valid, since List is invariant.

@jhance
Copy link
Collaborator

jhance commented Oct 11, 2015

I wonder if theres a way to word the error message here so it would be the same as #734?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 12, 2015

Hmm.. can't come up with a good common message right now. Why not just come up with a separate message for attributes?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 12, 2015

Also, assignment to a covariant attribute should be rejected. There doesn't seem to be issue for that.

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Feb 2, 2023

Am I correct in thinking that an example of this issue is:

from typing import TypeVar, Generic
T = TypeVar("T", covariant=True)
class Foo(Generic[T]):
    foo: T
a: Foo[int] = Foo()
b: Foo[object] = a
b.foo = "sus"
a.foo + 1  # no mypy error, runtime error

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

No branches or pull requests

3 participants