Skip to content

False negative: "Name not defined" error should be reported when variable is declared with a type annotation but not assigned to before use #11342

Closed
@waleedmebane

Description

@waleedmebane

Bug Report

x: int
print(x)

Mypy does not report an error for the code above or for this code:

print(x)
x = 0

But reports:
error: Name "x" is not defined

for:

print(x)

At runtime Python reports:
NameError: name 'x' is not defined

in all three cases.

Expected Behavior

Mypy should report Name "x" is not defined in all three cases.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.9.5 (default, Jul 19 2021, 01:21:38)
  • Operating system and version: Ubuntu 16.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions