-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Stubs for google.cloud.ndb the Google Cloud Datastore ndb client library #5821
Conversation
typeshed is the right place for this. A few things I noticed while briefly looking at it (not a full review):
|
One more thing: Untyped arguments and return values should just be left out, not annotated with |
Thank you so much for all the points raised! Will address them step by step. |
The `types-future` package is only for Python2, but this package supports only Python3. Also the versioning is only major minor, no patch.
One question:
Using pyright and pointing to my local typeshed checkout, with the |
Maybe @erictraut can help with your question. I am not sure how pyright handles third-party stubs at the moment. |
When you say "pointing to my local typeshed checkout", how are you configuring that in pyright? Are you specifying the "typeshedPath" in your pyrightconfig.json file? That's the best way to replace the bundled typeshed stubs with a private copy. There's another thing that might be happening here. Some versions of google's protobuf library shipped with a py.typed file in the |
This is almost done. Most type annotations were extracted from the ndb.model docstrings.
@erictraut thank you very much for your pointers. I can't see the import errors by pyright any more, yet I can't remember what I have changed. I basically picked the Re the google packages. I did check my virtual environment and the namespace packages which had
The Again, thank you very much for the pointer. If I run into trouble again, that's where I'll start investigating. |
@srittau could you clarify what that means for a big package like this? Would the better way be to remove any modules not having type hints instead of removing the |
The latest version of stubgen from mypy 0.910 doesn't generate |
I have to ask, because I'm out of ideas here and I'm not really sure where I could start looking. from google.cloud.ndb import Model, StringProperty
class Foo(Model):
name = StringProperty()
foo = Foo(name='frob')
reveal_type(foo.name)
reveal_type(foo.key) Running mypy against it with:
i get both types reported as Running
I get:
To an extend, I can understand why class MetaModel(type):
def __init__(cls, name: str, bases, classdict) -> None: ...
class Model(_NotEqualMixin, metaclass=MetaModel):
key: ModelKey = ...
def __init__(_self, **kwargs) -> None: ... If I'm not mistaken, the type of the @srittau or @erictraut if you do have any pointers where I could read up on figuring out this scenario I'd be very grateful. I browsed the mypy documentation and https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md, but nothing sticks out. |
The descriptor protocol ( |
Wow yes! That is totally it. Hm |
@romanofski, thanks for working on this PR. I checked out your branch to diagnose the problem you're encountering with pyright. It's due to a current limitation in pyright, which assumes that import module names will be unique across packages in typeshed. This PR introduces the first duplicate to typeshed, so it breaks that assumption. The Let me echo what @srittau said above. Please do not check in a set of stubs that contain a bunch of Did you reach out to the team that maintains the google-cloud-ndb package to see if they would be interested in accommodating inlined type annotations or packaged stubs? That approach is preferable to maintaining separate type stubs in typeshed. For additional guidance, you may find this documentation useful. |
Just out of curiosity: does the namespacing - I think almost all Google packages I've encountered are namespace packages - make it harder to traverse the imports?
Thank you so much for doing this. Since you had it mentioned earlier, I did remove the
Yes I fully agree.
Thanks again for the pointer. I'll have a more thorough read. Indeed I did check upstream first. The gist is, that the Google engineers would rather provide typing across all Python libraries (see googleapis/python-ndb#9) and prefer this going into typeshed for now. |
Pyright 1.1.160 is now released, and with this PR, typeshed's tests are now using the new version.
It adds a little bit of additional complexity to the import resolution mechanism (especially if that mechanism maintains a cache for performance reasons), but it's not a big problem. |
This is probably not correct. Will have to test.
This is currently going after what the documentation refers to their typical return values is.
Alright.. all tests pass. Most of my work went into the Questions:
Many thanks!! |
|
@@ -0,0 +1,5 @@ | |||
from typing_extensions import Literal | |||
|
|||
EVENTUAL: Literal["EVENTUAL"] |
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 installed it from pip and this value is 2
for me, not "EVENTUAL"
.
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 very much! Will update as soon as possible.
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.
Again thanks for spotting this! Should be addressed.
@@ -0,0 +1,12 @@ | |||
google.cloud.ndb.EVENTUAL |
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.
Could you add comments explaining why these need to be allowlisted?
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.
Updated. I must have generated this allowlist mistakenly in the earlier stages and never realised that it also hid some of the errors (e.g. google.cloud.ndb.EVENTUAL
). Now I only kept the three inconsistencies with the __new__
signatures which use self
in the library, but I kept it as cls
in the stub. Otherwise I run into errors from other type checkers in the tests.
This also addresses more inconsistencies with mistakenly added symbols to the allowlist
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.
One minor change then this is good to go. Sorry for the long delay.
stubs/google-cloud-ndb/METADATA.toml
Outdated
@@ -0,0 +1,2 @@ | |||
version = "1.9" |
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.
The syntax has changed:
version = "1.9" | |
version = "1.9.*" |
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.
No worries. Thanks for pointing it out.
I hope I did not make it hard than it already is when I merged in |
This adds stubs for https://github.com/googleapis/python-ndb, the Google Cloud Datastore ndb client library.
The stubs have been initially generated with
mypy-stubgen
. I have removed private modules and removed any unused imports. Most of the modules and functions are not typed yet, because these packages are quite big. I'm filing this as a draft in order to determine whether this would be an acceptable contribution. I would like to refine the types in subsequent pull requests.PS: I've checked with upstream which place would be better for these annotations and currently typeshed it is (see googleapis/python-ndb#9).