8000 Fix #5758 -- plain import didn't work with namespace package by gvanrossum · Pull Request #5762 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
/ mypy Public

Fix #5758 -- plain import didn't work with namespace package #5762

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

Merged
merged 7 commits into from
Oct 11, 2018

Conversation

gvanrossum
Copy link
Member

No description provided.

@gvanrossum
Copy link
Member Author
gvanrossum commented Oct 10, 2018

So here's a fix for part two of #5758. The way it works is, first, when find_module() encounters an installed non-stub package with a py.typed marker underneath a namespace parent module, it remembers the namespace ancestor(s) in a special cache dict. Then if a later request for one of those ancestors is received and it is not found in the usual way, the cached ancestor's directory is produced.

But that's not all that's needed! @macbeth322 -- you were spot on with your suggestion that this had something to do with the ancestor dependencies in all_imported_modules_in_file()! I ended up moving the "main" dependency to the top so it precedes the ancestors; that way, the first call to find_module() is e.g. find_module('foo.bar.baz'), which squirrels away the namespace ancestor as I described above.

This order dependency is ugly and caused a bunch of tests to fail (since the errors about missing packages come out in a different order) but it's the best I could come up with.

(As I write this there are some more failures, I have to look into those later.)

@chrisphilip322
Copy link
Contributor
chrisphilip322 commented Oct 10, 2018

I was able to test 0fa27de a569fb2 and it looks like it works for all the edge cases I found.

Edit: I copied the wrong commit

Copy link
Collaborator
@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with one comment request and one test request.
If the test request is super painful then it is probably OK to skip for now if we need to cherry-pick this, but it seems like the sort of thing that is super easy to break later, so.

@@ -572,12 +572,12 @@ def correct_rel_imp(imp: Union[ImportFrom, ImportAll]) -> str:
pri = import_priority(imp, PRI_MED)
ancestor_pri = import_priority(imp, PRI_LOW)
for id, _ in imp.ids:
res.append((pri, id, imp.line))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This somewhat subtle order dependency might deserve some comment.

# installed package with a py.typed marker that is a
# subpackage of a namespace package. We only fess up to these
# if we would otherwise return "not found".
return self.ns_ancestors.get(id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test for this case?

@gvanrossum
Copy link
Member Author

Added a comment. I believe adding a test for this situation would require adding a big chunk of code to testpep561.py. I'd rather focus on other aspects of the release, but I'll file a bug to remind myself.

Copy link
Member
@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think it is fine to add the test later.

@gvanrossum gvanrossum merged commit 725247b into python:master Oct 11, 2018
@gvanrossum gvanrossum deleted the ns-bug branch October 11, 2018 17:21
gvanrossum added a commit that referenced this pull request Oct 11, 2018
Fixes #5758. (Sadly there's no test for the second half of that issue; I've opened #5767 to track adding one.)
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

Successfully merging this pull request may close these issues.

4 participants
0