You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python version-conditional import inside function does not respect test #2260
Interesting. It looks like other cases of sys.version_info checks (not involving import) work correctly, so it seems the sys.version_info logic is executed here (also verified in the debugger). So maybe the code for understanding alternative imports isn't triggered inside functions?
Yeah, it seems that inside functions the is_unreachable flag is not
respected by the import-handling code. I'll have to delve in deeper to
understand why that is -- if you replace the alternative imports with
conflicting assignments you'll see that the type checker does honor
reachability everywhere. And the import code respects it outside functions.
I debugged this a bit more, it's because FirstPass doesn't descend into
functions. I don't think this is a very important use case -- it should be
easy to work around by moving the conditional imports out of functions.
It's quite common for imports, conditional or otherwise, to be done within functions (to improve startup time, to not bloat the interpreter's footprint unless a module is needed, to prevent namespace pollution, etc.). It's also fairly surprising for it to not work as expected.
I've never looked at mypy's internals apart from some collections stuff, so if you already have an idea of how to fix it, that's less likely to break something than if I have to figure it out. If you're busy, I don't mind taking a look, but unless the fix is obvious, you could probably take care of it faster than I could.
I'm guessing the function and class visitors need to recurse into their
bodies. And we'll need tests. (Does the same problem occur with conditional
imports in classes? If not the class visitor might not need fixing. The
visitors for some other statements show you how to recurse.)
So I guess only the function and class visitors should need changes, and the import-related stuff as to whether it's a top-level import or not (maybe?)
I expect the import visitors to be fine, it's the function/class visitors
and the statement visitors that you noticed that need work. (visitors
containing "pass" are not sufficient, they have to do the work
themselves. There are plenty of examples.)
So I guess only the function and class visitors should need changes, and
the import-related stuff as to whether it's a top-level import or not
(maybe?)
I have a branch that works with all my test files (and real-world files where I wanted this) and passes all current tests. Would you be able to write tests for it? I've made a PR: #2304
Activity
gvanrossum commentedon Oct 16, 2016
Interesting. It looks like other cases of sys.version_info checks (not involving import) work correctly, so it seems the sys.version_info logic is executed here (also verified in the debugger). So maybe the code for understanding alternative imports isn't triggered inside functions?
drvink commentedon Oct 16, 2016
Even
typing.TYPE_CHECKINGseems to be ignored inside a function:gvanrossum commentedon Oct 16, 2016
gvanrossum commentedon Oct 16, 2016
drvink commentedon Oct 16, 2016
It's quite common for imports, conditional or otherwise, to be done within functions (to improve startup time, to not bloat the interpreter's footprint unless a module is needed, to prevent namespace pollution, etc.). It's also fairly surprising for it to not work as expected.
gvanrossum commentedon Oct 16, 2016
drvink commentedon Oct 16, 2016
I've never looked at mypy's internals apart from some collections stuff, so if you already have an idea of how to fix it, that's less likely to break something than if I have to figure it out. If you're busy, I don't mind taking a look, but unless the fix is obvious, you could probably take care of it faster than I could.
gvanrossum commentedon Oct 16, 2016
drvink commentedon Oct 16, 2016
Any tips on what needs to be changed and how? (I guess the where is
FirstPassofmypy/semanal.py.)gvanrossum commentedon Oct 16, 2016
drvink commentedon Oct 16, 2016
To traverse function and class bodies, I see these (from
NodeVisitor) are already present:So I guess only the function and class visitors should need changes, and the import-related stuff as to whether it's a top-level import or not (maybe?)
gvanrossum commentedon Oct 17, 2016
I expect the import visitors to be fine, it's the function/class visitors
and the statement visitors that you noticed that need work. (visitors
containing "pass" are not sufficient, they have to do the work
themselves. There are plenty of examples.)
On Sun, Oct 16, 2016 at 4:51 PM, Mark Laws notifications@github.com wrote:
--Guido van Rossum (python.org/~guido)
gvanrossum commentedon Oct 20, 2016
Are you still working on a PR for this?
drvink commentedon Oct 21, 2016
Yes, sorry--I got sidetracked but I should have some time to look at it today.
drvink commentedon Oct 23, 2016
I have a branch that works with all my test files (and real-world files where I wanted this) and passes all current tests. Would you be able to write tests for it? I've made a PR: #2304