-
-
Notifications
You must be signed in to change notification settings - Fork 214
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
An example mypy configuration #31225
Conversation
``` $ mypy @mypy_typed_modules.txt Success: no issues found in 1 source file ```
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.
I'd be interested to see a github actions job that validates this
.github/workflows/type_checking.yml
Outdated
push: | ||
paths: | ||
- '*.py' |
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.
won't this only trigger when the code is pushed to master?
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.
It should trigger when you push to a PR. (I think paths
should actually be '**.py'
.)
Fwiw, you can specify to only trigger on pushes to master with
push:
branches: [master]
@@ -101,7 +101,7 @@ def to_datetime_str(value): | |||
return value.isoformat(timespec='milliseconds') | |||
|
|||
|
|||
serializers = { | |||
serializers: dict[tuple[Optional[str], Optional[str]], Callable] = { |
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.
I'm disappointed that type hints like this are apparently required by mypy. It's ugly and provides very little information that is not already present in the literals below.
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.
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.
That's more verbosity and no more useful IMO.
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.
If you want to use variables as parameters or default values, and you want a type checker to verify their type, then the type checker needs to know what type they are. You can choose to use this format, or the # type: ...
comment format.
Alternatively, if you don't want the type checker to check its type, you can explicitly tell the type checker to ignore it using # type: ignore
or (preferred) # type: ignore[assignment]
.
created: Optional[bool] = None | ||
closed: Optional[bool] = None | ||
extra_fields: dict[str, Any] = field(default_factory=dict) | ||
form_question_values: dict[str, Any] = field(default_factory=dict) |
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.
Was the switch from attrs to dataclasses required by mypy? If yes, can we no longer use attrs if the code is checked by mypy?
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.
No, the switch wasn't required, but it helps mypy to identify the types of fields. In the typing-extensions library and in Python 3.11, PEP 681 is adding more support for authors of libraries like attrs, pydantic, SQLAlchemy and Django ORMs, etc. so that type checkers can check field types.
I fixed some problems with the config file, and cleaned up the commit history here: #31475 Closing this PR. |
Technical Summary
An example mypy configuration.
Context: [CEP] The use of type hints
Safety Assurance
Safety story
Automated test coverage
This example would become part of automated checks.
Rollback instructions
Labels & Review