Skip to content

FrozenSet does not behave covariantly with FrozenSet #3928

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

Closed
mbenbernard opened this issue Apr 16, 2020 · 3 comments
Closed

FrozenSet does not behave covariantly with FrozenSet #3928

mbenbernard opened this issue Apr 16, 2020 · 3 comments

Comments

@mbenbernard
Copy link

mypy 0.750 behaves strangely when run on the following code snippet:

from typing import FrozenSet

s1: FrozenSet[bool]
s2: FrozenSet[bool]
s3: FrozenSet[int]

reveal_type(s1)     # note: Revealed type is 'builtins.frozenset[builtins.bool]'
reveal_type(s2)     # note: Revealed type is 'builtins.frozenset[builtins.bool]'
reveal_type(s3)     # note: Revealed type is 'builtins.frozenset[builtins.int]'

s2 = s1     # OK
s3 = s1     # error: Incompatible types in assignment (expression has type "FrozenSet[bool]", variable has type "FrozenSet[int]")

As you can see, FrozenSet doesn't seem to behave covariantly, despite the fact that typing.py says otherwise:

FrozenSet = _alias(frozenset, T_co, inst=False)

The only real difference that I see between FrozenSet and the other type aliases defined in typing.py is that it specifies a built-in type as its origin (i.e. 'frozenset') instead of an ABC.

Is this the expected behavior of mypy or a bug?

@ilevkivskyi
Copy link
Member

I think this is a bug in typeshed, transferring there.

@ilevkivskyi ilevkivskyi transferred this issue from python/mypy Apr 18, 2020
@srittau
Copy link
Collaborator

srittau commented Apr 20, 2020

@ilevkivskyi FrozenSet is typed as FrozenSet = TypeAlias(object) in typeshed, like Set, Counter, etc. Isn't FrozenSet special cased by mypy?

@ilevkivskyi
Copy link
Member

But frozenset used to be invariant. I noticed it was already fixed few weeks ago in #3808, there however was no mypy release that has the fix (it just missed 0.770 by few days).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants