-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
false report of multiple definitions #649
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
Comments
This is a hack...but, whenever I encounter stuff like this, I do: try:
advance_iterator = next
except NameError:
globals['advance_iterator'] = lambda it: it.next()
next = advance_iterator |
This is a known, long-standing issue. Conditional definitions are often rejected because mypy is too picky. A potential fix would be to treat |
Another, slightly different example:
|
Merging with #212, which is similar. |
Work towards #649. Still only addresses a subset of issues.
Now some more conditional definitions are accepted by mypy. These are all okay:
There's still a lot more work to do before mypy can deal with all interesting cases, but this should cover a good fraction of cases that used to fail. |
Thanks, it's a bit better. There's still this case that doesn't seem to
I get an error on each line mentioning os:
On Fri, Nov 27, 2015 at 1:58 PM, Jukka Lehtosalo [email protected]
--Guido van Rossum (python.org/~guido) |
That should be pretty easy to support as well. |
OK, good to know, but right now I think there are higher priority items.
|
OK, another case found in the wild: if ignore_case:
correct_case = None
else:
def correct_case(args): # E: error: Name 'correct_case' already defined
... |
I also have a long, unprocessed list of related issues from the Python 2.7 std library that I should analyze one of these days. |
These now work:
These still don't work:
|
Here's another one that still doesn't work:
Since it's the exact opposite of one that does work, how hard would it be (In general I am beginning to become more interested in more symmetrical |
I don't expect that supporting the [Here's some background for why these are hard. Mypy treats function definitions (and modules and classes) as special and different from variables. This is important for modules as it allows expressions like |
Maybe it could give up on that special treatment whenever it sees one of
these idioms, and treat the variable more as something of a given (maybe
optional) type?
|
Treating a function definition like an assignment if it's already defined as a variable would work. Consider code like this:
We would treat it like this code:
|
These now work:
The |
Okay, getting back to the original reported issue -- the fixes above don't actually solve it, as it also has the feature of both using a builtin in a module and redefining it. However, now there is fairly simple (though perhaps quite non-obvious) workaround: try:
advance_iterator = __builtins__.next # Note __builtins__!
except NameError:
def advance_iterator(it):
return it.next()
next = advance_iterator As we've pretty much implemented all the low hanging fruit improvements, I'm downgrading priority. Let me know if there are still common issues and I'll bump priority again. We could also close this issue and create separate issues for the remaining unsupported cases. |
Closing this now, even though this is not fully fixed. The above issues cover some remaining missing features. |
The following code in six fails to type check:
.../lib/python3.4/site-packages/six.py, line 501: Name 'advance_iterator' already defined
But advance_iterator can't be already defined: for NameError to be thrown, the lookup of next must have failed.
Whats the right way to handle this? Just mask it?
The text was updated successfully, but these errors were encountered: