-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Type error due to repeated use of underscore as dummy assignment target #465
Comments
Technically, Python itself treats it as a variable:
See here. |
Hmm, this is a bit tricky. A conservative (but ad-hoc) option would be to give variables named So this would be accepted, since
Not sure if this generalization would be that useful, though. We could also give a warning if a local variable is only assigned to, unless this happens in a multiple assignment (or a similar context) where it's okay to ignore the values of all variables except at least one. |
I argue that only names that start with an underscore should be considered. I have some code like |
Further complication: what about people who use |
But the rule for ignoring only things are only assigned to would work with |
(Raised priority to high since this request keeps coming.) |
I propose the following implementation. In assignment statements (which excludes other defining contexts like This includes unpacking assignments like Note that this doesn't cover other similar cases like It also doesn't cover more general variable reuse -- that's #1174 and it's more complex. |
@gvanrossum I think it would still be safer if |
The use case for i18n is already taken care of since it is not an assignment -- the idiom is I have to think about how easy it is to detect whether |
I think I have seen some cases where this was implemented as an assignment (in situations with custom
I think this will require a flag on |
OK, I will try it. Though I also found some code like this: for foo, _ = bar():
<stuff>
_ = _ # Trying to defeat some other static analysis tool |
I ran out of time but here's the simpler version: https://github.com/gvanrossum/mypy/tree/underscore. This does not check that the variable is never used. Note that the version you propose would also suffer from action at a distance: _, x = foo() # Error here if the print() way below is uncommented
.
.
.
# print(_) |
Yes, this can be even in another module if I import |
@ilevkivskyi Perhaps if you have an idea you can leave a hint on where I could conveniently add a flag to I also worry that this might be complicated if you have e.g. class C:
_ = 0
def foo(self) -> None:
print(self._) since IIRC the |
My idea was second pass of semantic analysis. The idea is that |
Anyway, as I said before I am now not so sure this is a good idea because of probable problems with incremental mode. To illustrate this:
The problem is that |
Maybe we can apply the special casing only within functions. Most use cases are going to be within functions anyway. |
I'm not so sure -- the weird case I found ( Perhaps another more conservative approach would be to simply not infer a type if the variable name is def gettext(): ...
_ = gettext
x = _("") since the |
I have a further question of clarification (unrelated to the usage issue). Clearly |
I would say |
I'm sorry, apparently I am having a hard time parsing people's sentences today... Just to confirm, you mean that the entire I do think these two are rather marginal. |
OTOH my current code actually does work for I'm making it into a PR but we'll wait for Jukka. |
Yes, this is what I mean.
If this is easy to implement, I am fine with this. |
Doing this only at the local scope is indeed very safe, since we will not have any problems with external visibility of |
I did a grep on some of our large codebases and this usage is indeed (nearly) only used in function scopes. So I am now okay with allowing this only at function scope. I need to figure out how to test for that. |
Consider the following code:
mypy reports:
line 11: Incompatible types in assignment (expression has type "C", variable has type "B")
Seems like mypy treats
_
as just a variable. That prevents it from being used as a dummy in assignments like the one above.A solution might be to always give
_
the Any type.The text was updated successfully, but these errors were encountered: