python / python/mypy

Detect out-of-scope variable from a with-block

Open
#8,842 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Is it possible to have mypy detect the use of variables that have gone outside of their intended scope in a with-block?

with expr() as value:
    do_good_stuff(value)

do_bad_stuff(value)

value is still defined at this point, but has been cleaned up at the end of the with-block. I'd like to be able to detect such uses.

It feels like a typing error, as value isn't really the same type after the with-block (okay, I guess technically it is, but it isn't usable as the same type anymore).

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 issue names no files, tests, or entry points. Start by locating mypy's handling of with-block scopes and type checking, then define the diagnostic behavior for a variable used after cleanup and add coverage showing the use inside the block remains valid while the later use is reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.