-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
repos: | ||
- repo: https://github.com/frostming/fix-future-annotations | ||
rev: 0.1.0 | ||
hooks: | ||
- id: fix-future-annotations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- id: fix-future-annotations | ||
name: fix-future-annotations | ||
description: Upgrade the typing annotations syntax to PEP 585 and PEP 604. | ||
entry: fix-future-annotations | ||
language: python | ||
types: [python] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Frost Ming | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,143 @@ | ||
# fix-future-annotations | ||
|
||
A CLI and pre-commit hook to fix future annotations | ||
A CLI and pre-commit hook to upgrade the typing annotations syntax to PEP 585 and PEP 604. | ||
|
||
|
||
## Upgrade Details | ||
|
||
### [PEP 585] – Type Hinting Generics In Standard Collections | ||
|
||
<table> | ||
<thead> | ||
<tr><th>Old</th><th>New</th></tr> | ||
</thead> | ||
<tbody> | ||
<tr><td> | ||
|
||
```python | ||
typing.Dict[str, int] | ||
List[str] | ||
``` | ||
</td><td> | ||
|
||
```python | ||
dict[str, int] | ||
list[str] | ||
``` | ||
</td></tr></tbody> | ||
</table> | ||
|
||
|
||
### [PEP 604] – Allow writing union types as `X | Y` | ||
|
||
<table> | ||
<thead> | ||
<tr><th>Old</th><th>New</th></tr> | ||
</thead> | ||
<tbody> | ||
<tr><td> | ||
|
||
```python | ||
typing.Union[str, int] | ||
Optional[str] | ||
``` | ||
</td><td> | ||
|
||
```python | ||
str | int | ||
str | None | ||
``` | ||
</td></tr></tbody> | ||
</table> | ||
|
||
### Import aliases handling | ||
|
||
<table> | ||
<thead> | ||
<tr><th>Old</th><th>New</th></tr> | ||
</thead> | ||
<tbody> | ||
<tr><td> | ||
|
||
```python | ||
import typing as t | ||
from typing import Tuple as MyTuple | ||
|
||
def foo() -> MyTuple[str, t.Optional[int]]: | ||
pass | ||
``` | ||
</td><td> | ||
|
||
```python | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
def foo() -> tuple[str, int | None]: | ||
pass | ||
``` | ||
</td></tr></tbody> | ||
</table> | ||
|
||
### Full example | ||
|
||
<table> | ||
<thead> | ||
<tr><th>Old</th><th>New</th></tr> | ||
</thead> | ||
<tbody> | ||
<tr><td> | ||
|
||
```python | ||
from typing import Union, Dict, Optional, Tuple | ||
|
||
MyType = Union[str, int] # non-annotation usage will be preserved | ||
|
||
|
||
def foo() -> Tuple[Dict[str, int], Optional[str]]: | ||
... | ||
``` | ||
</td><td> | ||
|
||
```python | ||
from __future__ import annotations | ||
|
||
from typing import Union | ||
|
||
MyType = Union[str, int] # non-annotation usage will be preserved | ||
|
||
|
||
def foo() -> tuple[dict[str, int], str | None]: | ||
... | ||
``` | ||
</td></tr></tbody> | ||
</table> | ||
|
||
Unused import names will be removed, and if `from __future__ import annotations` is not found in the script, it will be automatically added if the new syntax is being used. | ||
|
||
## Use as pre-commit hook | ||
|
||
Add the following to your `.pre-commit-config.yaml`: | ||
|
||
```yaml | ||
repos: | ||
- repo: https://github.com/frostming/fix-future-annotations | ||
rev: x.y.z # a released version tag | ||
hooks: | ||
- id: fix-future-annotations | ||
``` | ||
## Use as command line tool | ||
```bash | ||
python3 -m pip install git+https://github.com/frostming/fix-future-annotations.git | ||
|
||
fix-future-annotations my_script.py | ||
``` | ||
|
||
## License | ||
|
||
This work is distributed under [MIT](https://github.com/frostming/fix-future-annotations/blob/main/README.md) license. | ||
|
||
[PEP 585]: https://peps.python.org/pep-0585/ | ||
[PEP 604]: https://peps.python.org/pep-0604/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import typing as t | ||
from typing import Tuple as MyTuple | ||
|
||
|
||
def foo() -> MyTuple[str, t.Optional[int]]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
|
||
def foo() -> tuple[str, int | None]: | ||
pass |