Skip to content

variance inferred incorrectly when class has both a covariant and contravariant generic and instance attribute is assigned without a type annotationΒ #18147

Open
@DetachHead

Description

@DetachHead
from typing import Never


class Foo[in_T, out_T]:
    def __init__(self, *, value: out_T):
        a: Foo[Never, object] = self # error (incorrect)
        self.__a = value
        reveal_type(self.__a) # out_T`2

    def asdf(self, value: in_T) -> out_T: ...


class Bar[in_T, out_T]:
    def __init__(self, *, value: out_T):
        a: Bar[Never, object] = self # no error (correct)
        self.__a: out_T = value
        reveal_type(self.__a) # out_T`2

    def asdf(self, value: in_T) -> out_T: ...

playground

(originally raised in #18118 but i messed up the minimal repro)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions