Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

miketheman
Copy link
Member

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:

Found 114 errors in 30 files (checked 393 source files)

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]>
Signed-off-by: Mike Fiedler <[email protected]>
@miketheman miketheman added testing Test infrastructure and individual tests developer experience Anything that improves the experience for Warehouse devs labels Jan 29, 2025
@miketheman miketheman requested a review from a team as a code owner January 29, 2025 20:11
@@ -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
Copy link
Member

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
Copy link
Member

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 Nones 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer experience Anything that improves the experience for Warehouse devs testing Test infrastructure and individual tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants