Skip to content

Check class type parameter variance violations in inheritance #736

Closed

Description

@JukkaL

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).

Activity

  1. jhance commented on Oct 10, 2015

    @jhance
    Collaborator

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

  2. jhance commented on Oct 10, 2015

    @jhance
    Collaborator

    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)

  3. jhance commented on Oct 11, 2015

    @jhance
    Collaborator

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

  4. JukkaL commented on Oct 12, 2015

    @JukkaL
    CollaboratorAuthor

    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/

  5. added a commit that references this issue on Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions