Skip to content

remove inferred bivariance #1728

@carljm

Description

@carljm

We infer bivariance when it is sound to do so; that is, when a type variable is unused in the body of a class:

class C[T]:
    pass

Since this type variable is unused, any specialization of C is equivalent to any other specialization -- it does not matter how you specialize C. In other words, C is bivariant in T.

So our behavior here is correct. But it is confusing, and not useful. We repeatedly run into issues in our test suite where we accidentally make a class bivariant in a typevar and then tests pass for the wrong reason, or fail unexpectedly, since bivariance is not intuitive (covariance is the intuitive expectation).

We should at least emit a diagnostic when we infer a typevar as bivariant (since it indicates the typevar is useless and could just be removed). And we should probably also go ahead and just infer the typevar as covariant in that case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    genericsBugs or features relating to ty's generics implementation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions