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

no error when using covariant or contravariant TypeVar on class with writable attribute #14588

Closed
DetachHead opened this issue Feb 2, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Feb 2, 2023

from typing import Generic, TypeVar

T = TypeVar("T", covariant=True)


class Foo(Generic[T]):
    def __init__(self, value: T) -> None:
        self.foo = value

here, T is used in both an in and out position as the value can be modified as well.

jailbreak:

objects = Foo[object](None)

ints = Foo[int](1)


def do_thing(value: Foo[object]) -> None:
    value.foo = "asdf"


do_thing(ints)

if TYPE_CHECKING:
    reveal_type(ints.foo) # int
print(ints.foo) # "asdf" (hacked!!!!!!)

playground

@DetachHead DetachHead added the bug mypy got something wrong label Feb 2, 2023
@DetachHead DetachHead changed the title no error when using covariant or contravariant TypeVar on class with readable & writable attribute no error when using covariant or contravariant TypeVar on class with writable attribute Feb 2, 2023
@hauntsaninja
Copy link
Collaborator

Thanks, I think this is basically just a special case of #3208

@hauntsaninja
Copy link
Collaborator

Posted a comment in that issue, so if we ever do something about that we will also do something about this

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants