Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nodakai opened this issue Feb 4, 2017 · 1 comment

Comments

@nodakai
Copy link

nodakai commented Feb 4, 2017

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_
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2017

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants