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

MPP-3698: Enable mypy --check-untyped-def --no-implicit-reexport across code #4347

Merged
merged 27 commits into from
Jan 29, 2024

Conversation

jwhitlock
Copy link
Member

@jwhitlock jwhitlock commented Jan 26, 2024

For MPP-3698, I updated the code so both --check-untyped-def and --no-implicit-reexport is used across our code. This means that code without type annotations is checked by mypy rather than skipped, and finds errors like not considering None or other alternate return types.

This is a big PR, but the changes themselves are small.

Bugs Fixed

  • api/authentication.py
    • introspect_token - Error handler referred to fxa_resp, was not initialized (Sentry RELAY-J0, RELAY-J2)
  • api/permissions.py
    • HasPremium called a bool (unused in code)
  • emails/management/commands/process_emails_from_sqs.py
    • Command.handle - Use different name for exception inside of an exception handler

Code changes

  • Moved app config helper functions to app file. For example emails_config() is now in emails.app.py.
  • Lots of new annotations
  • Add assert to ensure a value is not None when mypy complained
  • Add isinstance to ensure request.user is not an anonymous user in API code
  • Add hasattr checks as needed for dynamically added fields
  • Comments and long code lines wrapped to 88 chars (in changed files). Long function names have a # noqa: E501 comment.
  • Used walrus operator (:=) to assign and assert (usually not None) in one line
  • Changes Optional[X] to Python 3.9 format X | None
  • More use of from __future__ import annotations for PEP-563, Postponed Evaluation of Annotations, to allow more forward annotations without quotes.
  • api/tests/authentication_tests.py
    • Used instance of FxaTokenAuthentication instead of class in FxaTokenAuthenticationTest
    • Used Django 4.2 header-setting style in FxaTokenAuthenticationTest
  • privaterelay/settings.py
    • RELAY_CHANNEL is now restricted to "local", "dev", "stage", or "prod"
    • Added many annotations to privaterelay/settings.py for "circular import" issues.
    • Removed cast=str when it is the default.
    • Set AUTO_RELOAD_BUNDLES = False to avoid django-ftl warning

@jwhitlock
Copy link
Member Author

PR #4321 is merged, this is ready for review.

@jwhitlock jwhitlock marked this pull request as ready for review January 26, 2024 21:58
Copy link
Member

@groovecoder groovecoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocking question about returning 401 status code that's very important to get right because Firefox clients change their behavior based on that status code.

api/authentication.py Show resolved Hide resolved
api/serializers/__init__.py Show resolved Hide resolved
api/tests/authentication_tests.py Show resolved Hide resolved
api/tests/authentication_tests.py Show resolved Hide resolved
privaterelay/settings.py Outdated Show resolved Hide resolved
privaterelay/settings.py Show resolved Hide resolved
privaterelay/tests/fxa_utils_tests.py Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
@jwhitlock jwhitlock force-pushed the typing-models-mpp-3698 branch from d69f791 to f38a5ee Compare January 29, 2024 16:34
Copy link
Member Author

@jwhitlock jwhitlock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick review @groovecoder! I've answered some questions, and made two suggested changes:

  • Removed Optional from privaterelay/settings.py
  • Added create_free_phones_flag_for_user helper

api/authentication.py Show resolved Hide resolved
api/tests/authentication_tests.py Show resolved Hide resolved
privaterelay/settings.py Outdated Show resolved Hide resolved
privaterelay/settings.py Show resolved Hide resolved
@jwhitlock jwhitlock requested a review from groovecoder January 29, 2024 20:07
@groovecoder groovecoder added this pull request to the merge queue Jan 29, 2024
@groovecoder groovecoder removed this pull request from the merge queue due to a manual request Jan 29, 2024
@jwhitlock jwhitlock added this pull request to the merge queue Jan 29, 2024
Merged via the queue into main with commit d25fe53 Jan 29, 2024
27 checks passed
@jwhitlock jwhitlock deleted the typing-models-mpp-3698 branch January 29, 2024 23:03
@jwhitlock jwhitlock changed the title MPP-3698: Enable mypy --check-untyped-def --no-implicit-reeexport across code MPP-3698: Enable mypy --check-untyped-def --no-implicit-reexport across code Feb 5, 2024
Copy link

sentry-io bot commented Feb 7, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ UnicodeEncodeError: 'utf-8' codec can't encode characters in position 404-406: surrogates not allowed emails.utils in ses_send_raw_email View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants