Skip to content

Commit

Permalink
Add test cases for python#14209
Browse files Browse the repository at this point in the history
  • Loading branch information
NMertsch committed Sep 9, 2024
1 parent 0c10367 commit b0d8e35
Showing 1 changed file with 227 additions and 0 deletions.
227 changes: 227 additions & 0 deletions test-data/unit/check-possibly-undefined.test
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,230 @@ def foo(x: Union[int, str]) -> None:
assert_never(x)
f # OK
[builtins fixtures/tuple.pyi]

[case testForLoopIndexMayBeUndefined]
# flags: --enable-error-code possibly-undefined
class Iterable:
def __iter__(self): ...

for i in Iterable():
pass

i # E: Name "i" may be undefined # if iterable was empty

[case testForLoopBodyMayNotBeExecuted]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
class Iterable:
def __iter__(self): ...

for _ in Iterable():
a = 1

a # E: Name "a" may be undefined # if iterable was empty

[case testWhileLoopBodyMayNotBeExecuted]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

while condition():
a = 1

a # E: Name "a" may be undefined # if `condition()` was `False` on first call

[case testForLoopBodyOrElseIsExecuted]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
class Iterable:
def __iter__(self): ...

for _ in Iterable():
a = 1
else:
a = 1 # if iterable is empty

a # OK

[case testWhileLoopBodyOrElseIsExecuted]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

while condition():
a = 1
else:
a = 1 # if `condition()` was `False` on first call

a # OK

[case testForLoopBreakAsLastStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
class Iterable:
def __iter__(self): ...

for i in Iterable():
a = 1
break
else:
a = 1 # if iterable is empty

a # OK

[case testForLoopBreakAsOnlyStatementOfCondition]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...
class Iterable:
def __iter__(self): ...

for i in Iterable():
a = 1
if condition():
break
else:
a = 1 # if iterable is empty

a # OK

[case testForLoopBreakAfterConditionalDefinition]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...
class Iterable:
def __iter__(self): ...

for i in Iterable():
if condition():
a = 1
break
else:
a = 1 # if iterable is empty

a # OK
[case testWhileLoopBreakAsLastStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

while condition():
a = 1
break
else:
a = 1 # if `condition()` was `False` on first call

a # OK

[case testWhileLoopBreakAsOnlyStatementOfCondition]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

while condition():
a = 1
if condition():
break
else:
a = 1 # if `condition()` was `False` on first call

a # OK

[case testWhileLoopBreakAfterConditionalDefinition]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

while condition():
if condition():
a = 1
break
else:
a = 1 # if `condition()` was `False` on first call, and if not `break`

a # OK

[case testForLoopElseRaiseAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
class Iterable:
def __iter__(self): ...

for _ in Iterable():
a = 1
else:
raise Exception() # if iterable is empty

a # OK
[builtins fixtures/exception.pyi]

[case testForLoopElseNoReturnAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
from typing import NoReturn
def fail() -> NoReturn: ...
class Iterable:
def __iter__(self): ...

for _ in Iterable():
a = 1
else:
fail() # if iterable is empty

a # OK

[case testForLoopElseReturnAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
class Iterable:
def __iter__(self): ...

def f() -> int:
for _ in Iterable():
a = 1
else:
return 1 # if iterable is empty

return a # OK

[case testWhileLoopElseRaiseAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...
class Iterable:
def __iter__(self): ...

while condition():
a = 1
else:
raise Exception() # if `condition()` was `False` on first call

a # OK
[builtins fixtures/exception.pyi]

[case testWhileLoopElseNoReturnAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
from typing import NoReturn
def fail() -> NoReturn: ...
def condition() -> bool: ...

while condition():
a = 1
else:
fail() # if `condition()` was `False` on first call

a # OK

[case testWhileLoopElseReturnAsOnlyStatement]
# flags: --enable-error-code possibly-undefined
# Regression test for https://github.com/python/mypy/issues/14209
def condition() -> bool: ...

def f() -> int:
while condition():
a = 1
else:
return 1 # if `condition()` was `False` on first call

return a # OK

0 comments on commit b0d8e35

Please sign in to comment.