Skip to content

Add enum iteration test cases - #4400

Merged
ilevkivskyi merged 4 commits into
python:masterfrom
ngaya-ll:patch-1
Feb 13, 2018
Merged

ilevkivskyi merged 4 commits into
python:masterfrom
ngaya-ll:patch-1

Conversation

@ngaya-ll

@ngaya-ll ngaya-ll commented Dec 21, 2017 •

Copy link
Copy Markdown
Contributor

Test cases for #2305.

@elazarg

elazarg commented Dec 21, 2017 •

Copy link
Copy Markdown
Contributor

Enum should follow typeshed - __iter__ should be added to an EnumMeta metaclass.
(EDIT: the PR for typeshed: python/typeshed#1755)

@ngaya-ll

ngaya-ll commented Dec 21, 2017 •

Copy link
Copy Markdown
Contributor Author

@elazarg I added an iteration test that directly iterates over an enum type without checking Iterable, but for some reason that also fails even though equivalent code passes with mypy 0.560. Do you know what's going on there? Is that a regression, a problem with the test harness, or a problem with the test?

@elazarg

elazarg commented Dec 21, 2017

Copy link
Copy Markdown
Contributor

@ngaya-ll see previous comment - the enum stub is incomplete, it does not have an EnumMeta metaclass with an __iter__ method (which typeshed already has).

PR 1755 is about the removal of explicit baseclasses, which will fix the other tests; you can try mimicking it in the stub.

@ngaya-ll

ngaya-ll commented Feb 1, 2018

Copy link
Copy Markdown
Contributor Author

@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?

@elazarg

elazarg commented Feb 1, 2018

Copy link
Copy Markdown
Contributor

This is what python-eval tests are for.

@ngaya-ll
ngaya-ll force-pushed the patch-1 branch 3 times, most recently from 1f3e08e to 7b8b2d0 Compare February 2, 2018 00:53
@ngaya-ll

ngaya-ll commented Feb 2, 2018 •

Copy link
Copy Markdown
Contributor Author

@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?

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some suggestions.

Comment thread test-data/unit/pythoneval.test Outdated
from typing import Iterable
class E(Enum):
a = 'a'
def f(ie:Iterable[E]):

Copy link
Copy Markdown
Member

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).

Comment thread test-data/unit/pythoneval.test Outdated
_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]

Copy link
Copy Markdown
Member

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).

@ngaya-ll ngaya-ll Feb 5, 2018 •

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like EnumIterationPreservesPreciseType

Copy link
Copy Markdown
Contributor Author

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.

[case testEnumIteration]
from enum import Enum
class E(Enum):
A = 'a'

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread test-data/unit/pythoneval.test Outdated
from enum import Enum
class E(Enum):
A = 'a'
l = [e for e in E]

Copy link
Copy Markdown
Member

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)?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

@ilevkivskyi

Copy link
Copy Markdown
Member

@ngaya-ll

Can this PR be merged?

Thank you for PR! Please see some suggestions I left above. After you consider them, we can add these tests.

Comment thread test-data/unit/pythoneval.test Outdated
_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]

Copy link
Copy Markdown
Member

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).

@ilevkivskyi
ilevkivskyi merged commit bdd5de9 into python:master Feb 13, 2018
@ngaya-ll
ngaya-ll deleted the patch-1 branch May 23, 2019 20:19
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

Successfully merging this pull request may close these issues.

3 participants