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

Namespace packages (PEP 420) #5691

Merged
merged 14 commits into from
Oct 3, 2018
Merged

Conversation

gvanrossum
Copy link
Member

@gvanrossum gvanrossum commented Sep 29, 2018

Tentative implementation of PEP 420. Fixes #1645.

Clarification of the implementation:

  • candidate_base_dirs is a list of (dir, verify) tuples, laboriously pre-computed.
  • The old code just looped over this list, looking for dir/<module>, and if found, it would verify that there were __init__.py files in all the right places (except if verify is false); the first success would be the hit;
  • In PEP 420 mode, if the above approach finds no hits, we do something different for those paths that failed due to __init__ verification; essentially we narrow down the list of candidate paths by checking for __init__ files from the top down. Hopefully the last test added clarifies this.

@gvanrossum gvanrossum force-pushed the namespaces branch 2 times, most recently from f2c87ff to ecac1b5 Compare September 29, 2018 21:07
@gvanrossum gvanrossum closed this Sep 29, 2018
@gvanrossum gvanrossum deleted the namespaces branch September 29, 2018 21:08
@gvanrossum gvanrossum restored the namespaces branch September 29, 2018 21:08
@gvanrossum gvanrossum reopened this Sep 29, 2018
@gvanrossum
Copy link
Member Author

(Whoops, accidentally deleted the branch which caused the PR to be closed. I'm not done yet.)

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.

I think this is right. At least after reading the PEP and playing a bit, this coincides with the runtime behaviour. I have two more suggestions for tests (apart from nesting normal in namespace and vice versa as you proposed, I just checked, both of these work at runtime).

test-data/unit/check-modules.test Show resolved Hide resolved
test-data/unit/check-modules.test Show resolved Hide resolved
@ilevkivskyi
Copy link
Member

As I understand this fixes #1645. I would add it to the PR description, so that we will not forget to close the issue :)

@gvanrossum
Copy link
Member Author

Alas, tests for nesting one type of package inside the other fail (both ways -- it always prefers the decoy file). It's late so I'll just leave it broken, I'll fix it tomorrow.

@gvanrossum
Copy link
Member Author

Hey @carljm, IIRC you expressed interest in namespace packages eons ago. Perhaps you can test this PR with your use case?

@gvanrossum
Copy link
Member Author

(Hold on, I have an idea for a refinement of the final part of the algorithm.)

@gvanrossum
Copy link
Member Author

@ilevkivskyi I think I'm happy now. Are you?

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.

Yes, I am happy with this.

# indicates the highest level at which a __init__.py[i] file
# is found; if no __init__ was found it returns 0, if we find
# only foo/bar/__init__.py it returns 1, and if we have
# foo/__init__.py it returns 2 (regardless of what's un
Copy link
Member

Choose a reason for hiding this comment

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

typo: 'un' -> 'in'

@gvanrossum gvanrossum merged commit f59a871 into python:master Oct 3, 2018
@gvanrossum gvanrossum deleted the namespaces branch October 3, 2018 18:09
TV4Fun pushed a commit to TV4Fun/mypy that referenced this pull request Oct 4, 2018
Tentative implementation of PEP 420. Fixes python#1645.

Clarification of the implementation:

- `candidate_base_dirs` is a list of `(dir, verify)` tuples, laboriously pre-computed.
- The old code just looped over this list, looking for `dir/<module>`, and if found, it would verify that there were `__init__.py` files in all the right places (except if `verify` is false); the first success would be the hit;
- In PEP 420 mode, if the above approach finds no hits, we do something different for those paths that failed due to `__init__` verification; essentially we narrow down the list of candidate paths by checking for `__init__` files from the top down. Hopefully the last test added clarifies this.
gvanrossum added a commit that referenced this pull request Oct 9, 2018
Follow-up for #5691. Should be cherry-picked into the release (#5741). Fixes #5757.
gvanrossum added a commit that referenced this pull request Oct 9, 2018
Follow-up for #5691. Should be cherry-picked into the release (#5741). Fixes #5757.
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.

2 participants