Skip to content

Use type context from previous definition when renaming #6231

Open
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions