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 class type parameter variance violations in inheritance #736

Closed
JukkaL opened this issue Aug 9, 2015 · 4 comments · Fixed by #13714
Closed

Check class type parameter variance violations in inheritance #736

JukkaL opened this issue Aug 9, 2015 · 4 comments · Fixed by #13714
Labels
bug mypy got something wrong priority-1-normal topic-inheritance Inheritance and incompatible overrides topic-type-variables

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 9, 2015

In generic inheritance, we can't change the variance of a type from invariant to covariant/contravariant. This should probably be rejected:

IV = TypeVar('IV')
CV = TypeVar('CV', covariant=True)

class A(Generic[IV]): ... # Bunch of methods
class B(A[CV], Generic[CV]): pass  # Error, type variable should be invariant

Going from covariant to invariant is fine (for example, List has to do this since it subclasses Sequence, which is covariant).

@jhance
Copy link
Collaborator

jhance commented Oct 10, 2015

Seems like we break this in our own tests but fixing the lib-stub to make Sequence covariant on T should fix that.

@jhance
Copy link
Collaborator

jhance commented Oct 10, 2015

I have this done on a branch, will submit PR later when my other stuff related to variance is in (since fixing this depends on fixing variance in general)

@jhance
Copy link
Collaborator

jhance commented Oct 11, 2015

It just struck me that we should reject things like List[Sequence[T]] where T is covariant. This is going to be fun...

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 12, 2015

Yeah, List[Sequence[T]] would also be invalid.

I'm sure there's a research paper that goes through all the special cases. Some of these might be relevant, in case you want to dig deeper (warning greek alert):

http://research.microsoft.com/en-us/um/people/akenn/generics/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-inheritance Inheritance and incompatible overrides topic-type-variables
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants