google / osv.dev

Open source vulnerability DB and triage service.
https://osv.dev
Apache License 2.0
1.49k stars 186 forks source link

refactor: python typing improvements #2537

Closed another-rex closed 3 weeks ago

another-rex commented 1 month ago

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:

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.