-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Make Enum Iterable (etc.) only structurally #1755
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
Conversation
Can't express the precise type nominally - see mypy#3210 for details
How should I add a test for this? |
There is one small warning, pytype and PyCharm may be not ready for this. (In principle it should be possible to fix this in mypy by modifying nominal constrain inference to make it more like structural, but I am not really proposing this since I don't have time to implement this in nearest future.) |
@elazarg typeshed doesn't currently have tests for specific stubs. There is an issue for adding it. In the meantime, could you share files you tested with mypy and their output? @matthiaskramm @vlasovskikh are you OK with this change (which depends on protocols) for mypy and pycharm? |
[updated with bad tests] from enum import Enum
from typing import Container, Iterable, Reversible, TypeVar
T = TypeVar('T')
class E(Enum):
one = 1
reveal_type(list(E)) # E: Revealed type is 'builtins.list[E*]'
reveal_type(iter(E)) # E: Revealed type is 'typing.Iterator[E*]'
reveal_type(reversed(E)) # E: Revealed type is 'typing.Iterator[E*]'
reveal_type(x for x in E) # E: Revealed type is 'typing.Iterator[E*]'
for a in E: reveal_type(a) # E: Revealed type is 'E*'
x: Container[E] = E
xx: Container[str] = E # doesn't fail - bad
y: Iterable[E] = E
yy: Iterable[str] = E # doesn't fail - bad
z: Reversible[E] = E
zz: Reversible[str] = E # doesn't fail - bad
def c(x: Container[T]) -> T: raise Exception
def it(x: Iterable[T]) -> T: raise Exception
def re(x: Reversible[T]) -> T: raise Exception
reveal_type(c(E)) # E: Revealed type is '<nothing>' # Bad; see below
reveal_type(it(E)) # E: Revealed type is 'E'
reveal_type(re(E)) # E: Revealed type is 'E'
(And maybe |
Situation with |
@JelleZijlstra given the bad tests in the updated tests, this PR should not be merged yet. I'd like to keep this open as a WIP though. I wonder why the assignments does not fail. |
@ilevkivskyi do you have any hint as to why |
This may be a problem in |
@elazarg Yes, it looks like the fix is very simple,
|
Hm, but then I get other spurious errors, so the actual fix will be a bit more complex, but I think this is the right direction. |
Thanks. I think I will open an issue about this. |
@JelleZijlstra PyCharm doesn't support protocols yet, but we will support them eventually, so this change is OK. |
Should fix python/mypy#3210 and python/mypy#3349
Remove
Iterable
,Reversible
andContainer
from being (nominal) baseclasses ofEnum
.