A series of typing improvements I made while working on the cursor changes. No logic or behavior changes have been made, so should perform identically with the existing deployed code.
Changes:
Add mypy-protobuf extension, which enables the --mypy_out= flag for protoc. This generates a typing stub for the grpc protobuf output, which can be used by different python type checking tools/extensions. (does not have to be mypy)
Generate and commit the pyi files, and update the readme / script to help generate them in the future.
Move helper functions in models.py to their own file (model_helpers.py)
Add explicit type definitions to models in models.py
These types are not technically "correct", but are the types that will get returned by datastore. This allows us to more easily label the responses from ndb queries.
Added an exclusion to the assignment type check of models.py to the pyproject.toml for pyright (used by vscode and pylance). If you use a different type checker, you might want to look into ignoring the errors in models.py in your own config.
Sadly yield still causes problems typing wise, so turning on "strict" typing is still not possible, details for anyone interested in why:
Using the yield anywhere in a function turns the function into a generator,
The left hand side output of yield is received when the caller calls send() on the returned generator.
Python assumes that the sent type will always be the same throughout a single function, so the send type is defined at the top of the function.
ndb uses yield as its own custom async implementation, the left hand side output of yield is the result of the future returned from the right hand side of yield.
This means the output of a yield call will depend on the right hand side of the yield, which is impossible to represent in python's type hinting system.
A series of typing improvements I made while working on the cursor changes. No logic or behavior changes have been made, so should perform identically with the existing deployed code.
Changes:
--mypy_out=
flag for protoc. This generates a typing stub for the grpc protobuf output, which can be used by different python type checking tools/extensions. (does not have to be mypy)models.py
Sadly yield still causes problems typing wise, so turning on "strict" typing is still not possible, details for anyone interested in why: