python / python/mypy

Use type context from previous definition when renaming

Open
#6,231 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive needs discussion priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Maybe we code like this shouldn't generate an error when using --allow-redefinition:

x: List[int] = []
# do something with x
x = []  # Error: need annotation

The reason for the error is that the second assignment creates a completely independent variable, so the type of the original definition has no effect.

There are still some open issues:

  • Using the context can generate false positives, in case the correct type would be something different for the second definition. I don't know how often this would be an issue.
  • Propagating the type context is somewhat tricky since the name of the previous definition is not trivial to calculate. The rules for renaming depend on the scope (local/class/global).
  • If/when we support more general redefinitions (not just within a single block), it may be unclear when we should propagate context. It may be best to first to make redefinitions more general and then reconsider this issue.

Follow-up to #6197.

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

Start by reading follow-up issue #6197 and the redefinition behavior described in this issue. Use the provided --allow-redefinition example to determine how type context should be handled across scopes, including the listed false-positive and renaming concerns. Done means the intended behavior and scope are settled before implementation.

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.