Skip to content

Commit

Permalink
feat: add pre-commit-hooks entry
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Dec 1, 2022
1 parent ffe41e8 commit b39b431
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
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
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
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]
21 changes: 21 additions & 0 deletions LICENSE
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.
142 changes: 141 additions & 1 deletion README.md
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/
7 changes: 6 additions & 1 deletion fix_future_annotations/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ def main(argv: list[str] | None = None) -> None:
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="+", help="File(s) to fix")
parser.add_argument(
"--write", "-w", action="store_true", help="Write changes to the file"
"--check",
"-c",
dest="write",
default=True,
action="store_false",
help="Only check the files without writing",
)
args = parser.parse_args(argv)
has_diff = False
Expand Down
13 changes: 10 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
[project]
name = "fix-future-annotations"
version = "0.1.0"
description = "A CLI and pre-commit hook to fix future annotations"
description = "A CLI and pre-commit hook to upgrade the typing annotations syntax to PEP 585 and PEP 604"
authors = [
{name = "Frost Ming", email = "mianghong@gmail.com"},
{name = "Frost Ming", email = "me@frostming.com"},
]
dependencies = [
"tokenize-rt>=5.0.0",
]
requires-python = ">=3.8"
readme = "README.md"
license = {text = "MIT"}
dynamic = ["version"]

[project.scripts]
fix-future-annotations = "fix_future_annotations._main:main"

[project.urls]
Homepage = "https://github.com/frostming/fix-future-annotations"
Releases = "https://github.com/frostming/fix-future-annotations/releases"

[tool.pdm.version]
source = "scm"

[tool.pdm.dev-dependencies]
test = [
"pytest>=7.2.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/samples/import_aliases.py
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
7 changes: 7 additions & 0 deletions tests/samples/import_aliases_fix.py
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

0 comments on commit b39b431

Please sign in to comment.