python / python/mypy

possibly-undefined not catching unbound variable in try/finally block

Open
#17,387 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-possibly-undefined
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The possibly-undefined check is not catching a possibly unbound variable when it's used within a finally block

To Reproduce

The following code raises NameError: name 'val' is not defined at runtime but passes he possibly-undefined check:

def foo() -> int:
    raise Exception()

try:
    val = foo()
except ValueError:
    val = 1
finally:
    print(val)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
enable_error_code = ["possibly-undefined", "redundant-expr", "truthy-bool"]
incremental = false
strict = true
warn_unreachable = true
  • Python version used: 3.9.17

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

No repository files or tests are named. Start by running the provided Python snippet with mypy 1.10.0 and the shown configuration, then trace the possibly-undefined analysis for try/finally; done means the unbound val use is reported and a regression test covers the case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.