-
Notifications
You must be signed in to change notification settings - Fork 136
Feature/implement mypy type checking #394
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
base: main
Are you sure you want to change the base?
Changes from all commits
ebc2e88
a31f631
dfe7836
bac6cca
ae58e39
68732e6
3748cdf
97fe791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
name: Run mypy | ||
on: | ||
pull_request: | ||
push: | ||
branches: ["main", "v[0-9]*"] | ||
tags: ["v[0-9]*"] | ||
workflow_dispatch: | ||
jobs: | ||
mypy: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: apt update | ||
run: sudo apt update | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.12" | ||
- name: Install Python dependencies | ||
run: | | ||
python -m pip install --upgrade pip wheel | ||
pip install -e '.[amazon-ses,resend,postal]' -r requirements-dev.txt | ||
- name: MyPy | ||
run: mypy . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import uuid | ||
import warnings | ||
from collections.abc import Mapping | ||
from email.utils import quote as rfc822_quote | ||
|
||
from requests.structures import CaseInsensitiveDict | ||
|
||
from ..exceptions import AnymailConfigurationError, AnymailWarning | ||
from ..message import AnymailRecipientStatus | ||
from ..utils import BASIC_NUMERIC_TYPES, Mapping, get_anymail_setting, update_deep | ||
from ..utils import BASIC_NUMERIC_TYPES, get_anymail_setting, update_deep | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 it already found a bug! |
||
from .base_requests import AnymailRequestsBackend, RequestsPayload | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,9 +135,8 @@ def __init__( | |
http_headers["Content-Type"] = "application/json" | ||
http_headers["Accept"] = "application/json" | ||
http_headers["X-API-KEY"] = backend.api_key | ||
super().__init__( | ||
message, defaults, backend, headers=http_headers, *args, **kwargs | ||
) | ||
kwargs["headers"] = http_headers | ||
super().__init__(message, defaults, backend, *args, **kwargs) | ||
|
||
def get_api_endpoint(self) -> str: | ||
return "email/send.json" | ||
|
@@ -297,7 +296,9 @@ def set_send_at(self, send_at: datetime | str) -> None: | |
# "Date and time in the format “YYYY-MM-DD hh:mm:ss” in the UTC time zone." | ||
# If send_at is a datetime, it's guaranteed to be aware, but maybe not UTC. | ||
# Convert to UTC, then strip tzinfo to avoid isoformat "+00:00" at end. | ||
send_at_utc = send_at.astimezone(timezone.utc).replace(tzinfo=None) | ||
send_at_utc = send_at.astimezone( # type:ignore[union-attr] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option here would be to replace the try/catch with Question: Would you suggest first adding the type:ignore comments without changing code, and then going back to resolve each of them in later commits that change the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's an easy fix I'd recommend doing it now - I just didn't see the fix offhand and lean towards keeping MyPy from slowing things down vs. trying to figure out a type hint I can't grok right away, especially if I know the code is fine. |
||
timezone.utc | ||
).replace(tzinfo=None) | ||
send_at_formatted = send_at_utc.isoformat(sep=" ", timespec="seconds") | ||
assert len(send_at_formatted) == 19 | ||
except (AttributeError, TypeError): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
AnymailRecipientsType = list[dict[str, dict[str, str]]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? (It doesn't seem to be referenced anywhere.) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[mypy] | ||
plugins = mypy_django_plugin.main | ||
# TODO: Check all code in the future | ||
check_untyped_defs = False | ||
disallow_any_generics = False | ||
disallow_incomplete_defs = False | ||
disallow_subclassing_any = False | ||
disallow_untyped_calls = False | ||
disallow_untyped_defs = False | ||
ignore_missing_imports = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my experimenting, I was able to switch
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds great! |
||
no_implicit_optional = True | ||
no_implicit_reexport = True | ||
strict = True | ||
warn_return_any = False | ||
# TODO: Remove this exception in the future | ||
disable_error_code = var-annotated | ||
exclude = docs | ||
|
||
[mypy.plugins.django-stubs] | ||
django_settings_module = "tests.test_settings.settings_5_0" | ||
|
||
# TODO: Type check tests in the future | ||
[mypy-tests.*] | ||
ignore_errors = True |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Requirements for developing (not just using) the package | ||
|
||
django-stubs | ||
hatch | ||
mypy | ||
pre-commit | ||
requests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
However, I did need to add Question: How do library packages typically handle dependencies only needed for typing? Would it be helpful for django-anymail to declare a "typing" extra ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, for me this was just for my local environment. I generally expect to |
||
tox<4 | ||
types-requests |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ class SparkPostBackendMockAPITestCase(RequestsBackendMockAPITestCase): | |
def setUp(self): | ||
super().setUp() | ||
# Simple message useful for many tests | ||
self.message = mail.EmailMultiAlternatives( | ||
self.message = AnymailMessage( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is no longer required (since you disabled mypy on tests). But it raises a couple of questions: A lot of the tests use Anymail's added attributes directly on Django's EmailMessage or EmailMultiAlternatives, because this is a documented Anymail feature. (Anymail has always allowed duck typing for its added attributes.)
|
||
"Subject", "Text Body", "[email protected]", ["[email protected]"] | ||
) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.