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

Mypy Typing Migration and Some typo fixes #299

Merged
merged 17 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CONTRIBUTING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ The settings for `mypy` are in the `mypy.ini`, settings for `flake8` are contain

All of these checks and formatters are invoked by pre-commit hooks. These hooks are run remotely on GitHub. In order to ensure that your code conforms to these standards, and, therefore, passes the remote checks, you can install the pre-commit hooks to be run locally. This is done by running (with your environment active)

**Note**: We use the modern mypy types introduced in Python 3.10 and above. See some of the [documentation here](https://mypy.readthedocs.io/en/stable/builtin_types.html)

For example, this means that we're using `list[str], tuple[int, int], tuple[int, ...], dict[str, int], type[C]` as built-in types and `Iterable[int], Sequence[bool], Mapping[str, int], Callable[[...], ...]` from collections.abc (as now recommended by mypy).

We are also moving to the new Optional and Union specification style:
```python
Optional[typing_stuff] -> typing_stuff | None
Union[typing1, typing2] -> typing1 | typing2
Optional[Union[typing1, typing2]] -> typing1 | typing2 | None
```

```bash
pre-commit install
```
Expand Down
4 changes: 3 additions & 1 deletion mypy_disallow_legacy_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"Hashable",
"Generator",
"Tuple",
"Mapping",
scarere marked this conversation as resolved.
Show resolved Hide resolved
"Type",
]


Expand All @@ -34,7 +36,7 @@ def filter_files_to_ignore(file_paths: list[str]) -> list[str]:


def construct_same_line_import_regex() -> str:
return rf"from typing import [^\n]*?({type_or})[^\n]*?\n$"
return rf"from typing import ([^\n]*?, )*({type_or})(\n|, [^\n]*?\n)"
scarere marked this conversation as resolved.
Show resolved Hide resolved


def construct_multi_line_import_regex() -> str:
Expand Down