Skip to content

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

Open
@Akuli

Description

@Akuli

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions