Skip to content

Check attributes for variance violations #735

Open

Description

@JukkaL

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.

Activity

  1. jhance commented on Oct 11, 2015

    @jhance
    Collaborator

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

  2. JukkaL commented on Oct 12, 2015

    @JukkaL
    CollaboratorAuthor

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

  3. JukkaL commented on Oct 12, 2015

    @JukkaL
    CollaboratorAuthor

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

  4. KotlinIsland commented on Feb 2, 2023

    @KotlinIsland
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions