# optional_refine_and.py
# This should pass.
# This should terminate.
from typing import Optional
def expect_int(i: int) -> None:
return
def f(x: Optional[int]) -> None:
return x and expect_int(x)
assert f(None) is None
assert f(42) is None
What happened?
return x and expect_int(x)
^
compiler.errors.TypedSyntaxError: return type must be None, not Optional[int]
What should have happened?
The program should be accepted by the type checker.
# optional_refine_or.py
# This should fail.
from typing import Optional
def expect_int(i: int) -> int:
return 42
def f(x: Optional[int]) -> int:
return x or expect_int(x)
[root@0fcdc5077884 vol]# ./python -m compiler --static Playground/conformance_suite/optional_refine_or.py
Traceback (most recent call last):
....
File "Playground/conformance_suite/optional_refine_or.py", line 10
return x or expect_int(x)
^
compiler.errors.TypedSyntaxError: type mismatch: None received for positional arg 'i', expected int
[root@0fcdc5077884 vol]#
a4a71ae 2021-12-29
What program did you run?
What happened?
What should have happened?
The program should be accepted by the type checker.
Note that a dual test fails as expected.