-
Notifications
You must be signed in to change notification settings - Fork 986
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
lint: narrow types to ensure correct usage #17519
base: main
Are you sure you want to change the base?
Conversation
Since `object_session(...)` can return `None`, we need to help mypy have confidence that the session is indeed there. One way to narrow the type is to assert that it's not `None`, and therefore should not alert about `union-attr` problems. Includes a couple of renames, and variable extraction where relevant. Signed-off-by: Mike Fiedler <[email protected]>
Refs: https://mypy.readthedocs.io/en/stable/type_narrowing.html Signed-off-by: Mike Fiedler <[email protected]>
Signed-off-by: Mike Fiedler <[email protected]>
Signed-off-by: Mike Fiedler <[email protected]>
@@ -31,6 +31,7 @@ def run_migrations_offline(): | |||
script output. | |||
""" | |||
options = context.config.get_section(context.config.config_ini_section) | |||
assert options is not None # type narrowing |
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.
We should probably better handle the case where options
could be None
here (raise an exception?) instead of assuming it will always be non-None
.
@@ -237,6 +237,7 @@ def has_primary_verified_email(self): | |||
@property | |||
def recent_events(self): | |||
session = orm.object_session(self) | |||
assert session is not None |
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 (and the many other assert session is not None
s makes me think we probably need a helper function that is responsible for narrowing the type and explicitly handling the case where the session might be None
rather than assuming it always is and throwing an AssertionError
if it's not. AFAICT, this probably should just raise our own exception with more details.
TL,DR: use type narrowing on objects of ambiguous nature.
These commits push us closer to being able to run
mypy
with--check-untyped-defs
, we're not fully there yet, but let's get these out the door.See each commit message for specifics on why the particular action was taken.
With these changes in place, we're down to: