Skip to content

del followed by an assignment of an "incompatible" type shouldn't probably raise an error #2806

Closed

Description

@nodakai

I'm posting this to here because PEP 482--484 didn't seem to mention either "scope" or del and this is probably an implementation detail.

Consider this example:

x = 1
del x
x = "a"

Mypy 0.470 raises an error for the above code, saying "Incompatible types in assignment (expression has type "str", variable has type "int")". But, shouldn't it be allowed because the xs before and after del are essentially different variables?

Compare it with

x = 1
x = None
x = "a"

which clearly is a type error (and mypy judges as such.)

Allowing the 1st pattern will open up the way to conveniently rewrite legacy codes like

# path: # type str
path = path.split(os.pathsep)
# path: # type List[str]

(taken from shutil.py)

I mean, we will be able to write

path_ = path.split(os.pathsep)
del path
path = path_

Activity

  1. JukkaL commented on Feb 7, 2017

    @JukkaL
    Collaborator

    Mypy currently doesn't treat del as deleting a variable -- it rather treats it as deleting the value of a variable. We've discussed similar issues before -- this is basically a special case of #1174, so I'm closing this issue as duplicate. Feel free to add to the discussion in #1174 if you have new thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions