python / python/mypy

Please just change something. Weird combination of dead code and inferred None

Open
#10,634 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Feature

foo = None

def bar() -> str:
    global foo
    if foo is not None:
        print("Hello")
    else:
        foo = "bar"
        return "lol"

This program type-checks, even though it's obviously wrong. It's not a bug, it's a feature:

  • Inferred type of foo is None, even though Optional[str] was intended.
  • Because foo has type None, the if foo is not None: part is eliminated as dead code, and therefore the missing return isn't detected.
  • After setting foo = "bar", reveal_type(foo) shows str. Perhaps the assignment should be an error?

Pitch

A friend submitted a PR like this and thought it was fine.

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

The payload names no files, tests, or entry points. Start by running the provided snippet through mypy and reviewing how inferred None, global reassignment, unreachable branches, and missing returns are handled. A maintainer decision on the intended semantics and regression coverage is needed before implementation can be scoped.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.