python / python/mypy

(🐞) Artificially literal bools not considered when performing reachability checks where branch terminates

Open
#12,365 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-reachability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

There is already some disparity between naturally literal bools(annotated with Literal or narrowed) and artificially literal bools (platform, python-version, always-true, TYPE_CHECKING etc):

# mypy: always-true=foo
from typing import Literal

foo: bool

if not foo:
    print(1)  # no error ✅

bar: Literal[True]

if not bar:
    print(1)  # error: Statement is unreachable ✅

But this is not currently being applied to this situation:

# mypy: always-true=foo
from typing import Literal

foo: bool

def f1() -> None:
    if foo:
        return
    print(1)  # error: Statement is unreachable ❌

bar: Literal[True]

def f2() -> None:
    if bar:
        return
    print(1)  # error: Statement is unreachable ✅
Key:

✅ = correct behavior
❌ = sussy imposter behavior

Careful of #12325
Related: #11364

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.

Research direction

Start by reproducing the two reachability examples in the issue and compare the handling of artificially literal and naturally literal booleans. Read the context of #12325 and #11364 before changing behavior; done means the foo branch reports the same unreachable statement diagnostic as the bar branch without regressing the existing checks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.