-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
PEP561: Clarify stub-only namespace package behavior #2083
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79f69c5
Clarify stub-only namespace package behavior in PEP561
nipunn1313 538d8ee
Clarify single file modules inside namespace packages
nipunn1313 9267be7
Apply suggestions from code review
nipunn1313 7141d80
Add note to changelog about handling of single file modules
nipunn1313 b3accc1
Clarify partial packages for namespace packages
nipunn1313 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clarification was inspired by googleapis/python-firestore#447 - around the module google/cloud/firestore.py which exists within a package distribution in a way that can't be
py.typed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think is quite right. In the above issue, the
py.typed
file belongs inside thegoogle/cloud
folder, asgoogle
is the namespace package (unless I am mistaken), so thepy.typed
file lives in each sub-package of the namespace package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
google.cloud is also a namespace package (sorry for confusion)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nested namespace package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you should still list the sub-package of the outermost package as typed (even if partial).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure we're on the same page - I don't think the preexisting PEP unambiguously specifies this case (single-file modules in possibly nested namespace packages) - we are discussing/reasoning about what makes the most sense to make as the specification here.
The sub-namespace-package (
google.cloud
in this case) will be populated by several different pip distributions (https://pypi.org/search/?q=google-cloud-). I don't think it works - or makes sense - for each of those pip distributions to place apy.typed partial
ingoogle.cloud
.As a concrete example - for something like
google/cloud/firestore.py
which is fully typed - It doesn't make sense to have agoogle/cloud/py.typed
indicating full typed-ness - sincegoogle/cloud
could have other entries from other distributions. Rather we must require it to begoogle/cloud/firestore/__init__.py
withgoogle/cloud/firestore/py.typed
so that typecheckers can know thatfirestore
is fully typed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that seems reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nipunn1313 I ran into something related to this while working on typing for https://pypi.org/project/googleapis-common-protos/ (using the excellent mypy-protobuf, thanks for your work!) and was wondering if you could confirm if my understanding of the updated spec is correct.
Like most Google distributions this distribution contains a namespace
google
package, which contains multiple subpackages. Here's the tree showing one particular subpackage:Here both
google
andrpc
are namespace packages (usingdeclare_namespace
/extend_path
), whilecontext
is a regular package. As you can seegoogle.rpc
actually contains some modules. Presumably some other Google distribution could put other modules in this namespace. If I understand this discussion correctly, there's no way to distribute typing information forcode_pb2.py
,error_details_pb2.py
andstatus_pb2.py
and for this to work Google would have to refactor these into packages? Is the correct approach when making a stub-only package for this to simply leave them out, like this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually wrote this spec modification with a case in mind - single-file-modules where types are included in the original package (googleapis-common-protos in your eg) - because in that case - a py.typed is required.
For a stub-only package - By the spec clarification - all namespace packages within stub-packages are to be considered partially typed by type checkers.
Thus I think you can opt to do
You may also add stubs for the remaining files (eg
status_pb2.pyi
), but be sure to omit the__init__.pyi
for namespace packages. Typechecker should considergoogle-stubs
andgoogle-stubs/rpc
as incomplete because they are namespace packages.I haven't tested whether mypy/pyright obey this spec clarification yet - but I believe pyright recently made a change to make this work. I believe in my past testing mypy needed the
--namespace-packages
flag to treat these properly, but I didn't test this specific case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the very quick clarification! That's my bad for not properly distinguishing between typed "real" packages and stub-only packages. I guess the restriction on single-file modules is Google's problem if they ever decide to type this in the package itself. I'll just do this for my stub-only package then: