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

An example mypy configuration #31225

Closed
wants to merge 13 commits into from
Closed

An example mypy configuration #31225

wants to merge 13 commits into from

Conversation

kaapstorm
Copy link
Contributor

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

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

```
$ mypy @mypy_typed_modules.txt
Success: no issues found in 1 source file
```
@kaapstorm kaapstorm added the Open for review: do not merge A work in progress label Mar 9, 2022
@kaapstorm kaapstorm requested a review from millerdev March 9, 2022 18:18
@dimagimon dimagimon added the dependencies Pull requests that update a dependency file label Mar 9, 2022
@kaapstorm kaapstorm requested a review from snopoke March 9, 2022 18:19
Copy link
Contributor

@snopoke snopoke left a 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

Comment on lines 4 to 6
push:
paths:
- '*.py'
Copy link
Contributor

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?

Copy link
Contributor Author

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]

@kaapstorm kaapstorm added product/invisible Change has no end-user visible impact and removed Open for review: do not merge A work in progress labels Apr 7, 2022
@kaapstorm kaapstorm requested a review from a team as a code owner April 7, 2022 23:44
@@ -101,7 +101,7 @@ def to_datetime_str(value):
return value.isoformat(timespec='milliseconds')


serializers = {
serializers: dict[tuple[Optional[str], Optional[str]], Callable] = {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@kaapstorm
Copy link
Contributor Author

I fixed some problems with the config file, and cleaned up the commit history here: #31475

Closing this PR.

@kaapstorm kaapstorm closed this Apr 15, 2022
@kaapstorm kaapstorm deleted the nh/mypy branch April 15, 2022 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file product/invisible Change has no end-user visible impact
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants