facebookincubator / cinder

Cinder is Meta's internal performance-oriented production version of CPython.

Home Page:https://trycinder.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`and` fails to refine `Optional[T]`

LuKuangChen opened this issue · comments

commented

a4a71ae
2021-12-29

What program did you run?

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

Note that a dual test fails as expected.

[root@0fcdc5077884 vol]# cat Playground/conformance_suite/optional_refine_or.py 
# 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]#