-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add enum iteration test cases #4400
Conversation
|
@elazarg I added an iteration test that directly iterates over an enum type without checking |
@ngaya-ll see previous comment - the enum stub is incomplete, it does not have an PR 1755 is about the removal of explicit baseclasses, which will fix the other tests; you can try mimicking it in the stub. |
@elazarg If I understand correctly, you're saying the unit tests here are independent of the typeshed definitions. I observed a problem with mypy in the wild, so I thought it would make sense to add test cases here to demonstrate the problem and verify when it was fixed. But it sounds like these tests use dummy type definitions that will not reflect the real-world behavior of mypy. Is there a better way to test what I'm trying to test (i.e. that mypy + typeshed correctly handles enums in real life)? Or is this not helpful? |
This is what python-eval tests are for. |
1f3e08e
to
7b8b2d0
Compare
Test cases for python#2305
@elazarg Thanks! Moved the enum tests to pythoneval and rebased the branch onto master and the tests are now passing. Can this PR be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some suggestions.
test-data/unit/pythoneval.test
Outdated
from typing import Iterable | ||
class E(Enum): | ||
a = 'a' | ||
def f(ie:Iterable[E]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow PEP 8 (even in tests).
test-data/unit/pythoneval.test
Outdated
@@ -1458,3 +1458,48 @@ _testNoCrashOnGenericUnionUnpacking.py:10: error: Revealed type is 'Union[builti | |||
_testNoCrashOnGenericUnionUnpacking.py:11: error: Revealed type is 'Union[builtins.str, builtins.int]' | |||
_testNoCrashOnGenericUnionUnpacking.py:15: error: Revealed type is 'Union[builtins.int*, builtins.str*]' | |||
_testNoCrashOnGenericUnionUnpacking.py:16: error: Revealed type is 'Union[builtins.int*, builtins.str*]' | |||
|
|||
[case testEnumIteration] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use longer/more detailed names for test cases (there are lots of tests in pythoneval.test
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not sure what more detail would be helpful here. Did you have something in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like EnumIterationPreservesPreciseType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's only part of what this is aiming to test. I'll add a comment in the test with more details.
test-data/unit/pythoneval.test
Outdated
[case testEnumIteration] | ||
from enum import Enum | ||
class E(Enum): | ||
A = 'a' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use uppercase A
here and lowercase a
in other places?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change to uppercase for all enum constants to be consistent.
test-data/unit/pythoneval.test
Outdated
from enum import Enum | ||
class E(Enum): | ||
A = 'a' | ||
l = [e for e in E] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also reveal type of list(E)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is more concerned with the element type of the comprehension, not the resulting list. I'll edit the test to make that more explicit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is more concerned with the element type of the comprehension, not the resulting list.
It is (almost) never bad to test more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at it more in terms of trying to check the enum behavior with minimal reliance on other type-checking features. But I can add the additional check if you feel strongly that it would be valuable.
Thank you for PR! Please see some suggestions I left above. After you consider them, we can add these tests. |
test-data/unit/pythoneval.test
Outdated
@@ -1458,3 +1458,51 @@ _testNoCrashOnGenericUnionUnpacking.py:10: error: Revealed type is 'Union[builti | |||
_testNoCrashOnGenericUnionUnpacking.py:11: error: Revealed type is 'Union[builtins.str, builtins.int]' | |||
_testNoCrashOnGenericUnionUnpacking.py:15: error: Revealed type is 'Union[builtins.int*, builtins.str*]' | |||
_testNoCrashOnGenericUnionUnpacking.py:16: error: Revealed type is 'Union[builtins.int*, builtins.str*]' | |||
|
|||
[case testEnumIteration] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's only part of what this is aiming to test.
Then choose a better name. Also the comment below is too long, just leave # Regression test for #2305
I am not arguing for fun here, there is pytest -k
command that will run all test cases with a given pattern in names. This is why it is important to add some specific keyword like Precise
or Preserve
(or your choice). In addition, this avoids accidental test name collisions (we currently have thousands of test cases).
Test cases for #2305.