-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: enable mypy session for ndb #16691
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
Open
chalmerlowe
wants to merge
9
commits into
main
Choose a base branch
from
feat-enable-mypy-ndb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
990f5a9
feat: enable mypy session for ndb
chalmerlowe d4f5310
feat(ndb): enable mypy and resolve type errors
chalmerlowe 9934515
fix(ndb): fix python 3.9 compatibility and __all__ test failures
chalmerlowe ec2312c
fix(ndb): fix mypy errors and undefined Any name
chalmerlowe a316b6f
fix(ndb): replace asserts in production code with explicit checks
chalmerlowe 51c6e18
adds comment about the transition from context to ctx
chalmerlowe 787e904
style: blacken ndb/model.py
chalmerlowe 4f9468d
style: blacken ndb/context.py
chalmerlowe 0ee10cc
Update temp variable name from st to parsed_time
chalmerlowe 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Replace
stwith a more meaningful nameThere 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.
Gemini suggested this refactor to clean up this code and reduce the number of if statements
Uh oh!
There was an error while loading. Please reload this page.
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.
@parthea
These two code blocks are fundamentally the same with the exception of item
# 2. Convert tuple to datetime.time using unpackingso my comments focus on that.In your comment we assert that Gemini recommends this refactor to reduce the number of
ifstatements.Funnily enough:
Those
ifstatements were also recommended by Gemini to enable greater type safety with mypy, because thedatetime.time(*t_tuple)expression caused mypy errors.Here is Gemini's rationale for the approach taken in this PR (this was the third change we made and Gemini felt it was the most important):
< BEGINNING of GEMINI's exposition >
This is the most important change for Mypy:
Old:
return datetime.time(*time_tuple)The Problem: The
*(splat) operator tells Python to unpack thetupleinto arguments. However,datetime.time()accepts specific arguments:hour,minute,second,microsecond.If
time_tuplehas a length of 1, it’s just thehour. If it's length 3, it'sH, M, S.Mypy cannot guarantee at compile-time that the length of the tuple matches what the function expects. It sees
*time_tupleand worries you might be passing 10 arguments to a function that takes 4.New:
The Benefit: This is called Exhaustive Checking or Manual Unrolling.
By checking the
len()explicitly, you are "narrowing" the type. Inside theif len(t_tuple) == 1block, Mypy knows for a fact thatt_tuple[0]exists and is the only argument.This removes all ambiguity. Mypy can now prove the code is safe without needing a
# type: ignore.< END of GEMINI's exposition >
I am happy to resolve this in whatever way feels best to you, but I suspect that if I revert back to using the splat operator, we will have to go through another cycle of trying to find a way to keep mypy happy OR add one or more ignore pragmas.