Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open
#63 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start with the reproducer in optional_refine_and.py and compare its behavior with the failing dual case in optional_refine_or.py. Run the static compiler against the examples to inspect the reported type refinement. Done means the and program is accepted while the or program continues to reject passing None to expect_int.

Written by the indexing model from the issue text.

Description

sp-correctness staticpython

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]# 
Dominant language
Python
Stars
3.8k
Forks
139
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebookincubator/MetaPython

All issues in facebookincubator/MetaPython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.