From 9e143b2e7255755e531ab708facb1f76b216f689 Mon Sep 17 00:00:00 2001 From: Nick Gaya Date: Thu, 21 Dec 2017 10:14:55 -0800 Subject: [PATCH 1/4] Add enum iteration test cases Test cases for #2305 --- test-data/unit/pythoneval.test | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 67374962afc3..19281800c327 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -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] +from enum import Enum +class E(Enum): + A = 'a' +l = [e for e in E] +reveal_type(l[0]) +for e in E: + reveal_type(e) +[out] +_testEnumIteration.py:5: error: Revealed type is '_testEnumIteration.E*' +_testEnumIteration.py:7: error: Revealed type is '_testEnumIteration.E*' + +[case testEnumIterable] +from enum import Enum +from typing import Iterable +class E(Enum): + a = 'a' +def f(ie:Iterable[E]): + pass +f(E) + +[case testIntEnumIterable] +from enum import IntEnum +from typing import Iterable +class N(IntEnum): + x = 1 +def f(ni: Iterable[N]): + pass +def g(ii: Iterable[int]): + pass +f(N) +g(N) + +[case testDerivedEnumIterable] +from enum import Enum +from typing import Iterable +class E(str, Enum): + a = 'foo' +def f(ei: Iterable[E]): + pass +def g(si: Iterable[str]): + pass +f(E) +g(E) From 6e6894184134120e434c354babe8114cb53f5a76 Mon Sep 17 00:00:00 2001 From: Nick Gaya Date: Mon, 5 Feb 2018 11:59:12 -0800 Subject: [PATCH 2/4] Address review comments --- test-data/unit/pythoneval.test | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 19281800c327..590b515df427 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1463,20 +1463,19 @@ _testNoCrashOnGenericUnionUnpacking.py:16: error: Revealed type is 'Union[builti from enum import Enum class E(Enum): A = 'a' -l = [e for e in E] -reveal_type(l[0]) +(reveal_type(e) for e in E) for e in E: reveal_type(e) [out] -_testEnumIteration.py:5: error: Revealed type is '_testEnumIteration.E*' -_testEnumIteration.py:7: error: Revealed type is '_testEnumIteration.E*' +_testEnumIteration.py:4: error: Revealed type is '_testEnumIteration.E*' +_testEnumIteration.py:6: error: Revealed type is '_testEnumIteration.E*' [case testEnumIterable] from enum import Enum from typing import Iterable class E(Enum): - a = 'a' -def f(ie:Iterable[E]): + A = 'a' +def f(ie: Iterable[E]): pass f(E) @@ -1484,7 +1483,7 @@ f(E) from enum import IntEnum from typing import Iterable class N(IntEnum): - x = 1 + X = 1 def f(ni: Iterable[N]): pass def g(ii: Iterable[int]): @@ -1496,7 +1495,7 @@ g(N) from enum import Enum from typing import Iterable class E(str, Enum): - a = 'foo' + A = 'foo' def f(ei: Iterable[E]): pass def g(si: Iterable[str]): From 191918e879c97531e5a9fceef1beb118b2f896b6 Mon Sep 17 00:00:00 2001 From: Nick Gaya Date: Tue, 6 Feb 2018 13:56:43 -0800 Subject: [PATCH 3/4] Add comment to enum iteration test --- test-data/unit/pythoneval.test | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 590b515df427..c0b24488df26 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1460,6 +1460,10 @@ _testNoCrashOnGenericUnionUnpacking.py:15: error: Revealed type is 'Union[builti _testNoCrashOnGenericUnionUnpacking.py:16: error: Revealed type is 'Union[builtins.int*, builtins.str*]' [case testEnumIteration] +# Regression test for #2305 +# Tests that: +# - Mypy allows iteration over enum classes in comprehensions and for-loops +# - The inferred element type in such iteration contexts is the enum type from enum import Enum class E(Enum): A = 'a' @@ -1467,8 +1471,8 @@ class E(Enum): for e in E: reveal_type(e) [out] -_testEnumIteration.py:4: error: Revealed type is '_testEnumIteration.E*' -_testEnumIteration.py:6: error: Revealed type is '_testEnumIteration.E*' +_testEnumIteration.py:8: error: Revealed type is '_testEnumIteration.E*' +_testEnumIteration.py:10: error: Revealed type is '_testEnumIteration.E*' [case testEnumIterable] from enum import Enum From 0330038761d5cecc1b8fb4c4f7bb09389d71507f Mon Sep 17 00:00:00 2001 From: Nick Gaya Date: Wed, 7 Feb 2018 11:37:06 -0800 Subject: [PATCH 4/4] Update test name, remove comments --- test-data/unit/pythoneval.test | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index c0b24488df26..b35c2af5614c 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1459,11 +1459,8 @@ _testNoCrashOnGenericUnionUnpacking.py:11: error: Revealed type is 'Union[builti _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] +[case testEnumIterationAndPreciseElementType] # Regression test for #2305 -# Tests that: -# - Mypy allows iteration over enum classes in comprehensions and for-loops -# - The inferred element type in such iteration contexts is the enum type from enum import Enum class E(Enum): A = 'a' @@ -1471,8 +1468,8 @@ class E(Enum): for e in E: reveal_type(e) [out] -_testEnumIteration.py:8: error: Revealed type is '_testEnumIteration.E*' -_testEnumIteration.py:10: error: Revealed type is '_testEnumIteration.E*' +_testEnumIterationAndPreciseElementType.py:5: error: Revealed type is '_testEnumIterationAndPreciseElementType.E*' +_testEnumIterationAndPreciseElementType.py:7: error: Revealed type is '_testEnumIterationAndPreciseElementType.E*' [case testEnumIterable] from enum import Enum