๐Ÿ“ฆ ftnext / kotoha-python-linter

๐Ÿ“„ README.md ยท 83 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83# flake8-kotoha

[**K**o**T**o**H**a](https://millionlive-theaterdays.idolmaster-official.jp/idol/kotoha/): **K**aizen **T**ype **H**int

## Install

### uvx (Recommended)

The easiest way to run `flake8` (without manually installation).

```sh
$ uvx --with flake8-kotoha flake8 .
```

### Other options

pipx

```sh
$ pipx install --preinstall flake8-kotoha flake8
$ flake8 -h
...
Installed plugins: flake8-kotoha: 0.1.0, ...
```

venv + pip

```sh
$ python -m venv .venv --upgrade-deps
$ .venv/bin/python -m pip install flake8-kotoha
$ .venv/bin/flake8 -h
...
Installed plugins: flake8-kotoha: 0.1.0, ...
```

uv

```sh
$ uv tool install flake8 --with flake8-kotoha
$ flake8 -h
...
Installed plugins: flake8-kotoha: 0.1.0, ...
```

## Usage

```python
def plus_one(numbers: list[int]) -> list[int]:
    return [n + 1 for n in numbers]
```

```sh
$ flake8 example.py
example.py:1:14: KTH101 Type hint with abstract type `collections.abc.Iterable` or `collections.abc.Sequence`, instead of concrete type `list`
```

## Error codes

Type hints in function **parameters**

Use abstract types instead of concrete ones

| error code | description |
|:----:|:------------|
| KTH101 | Use `Iterable` or `Sequence` instead of `list` |
| KTH102 | Use `Iterable` or `Sequence` instead of `tuple` |
| KTH103 | Use `Iterable` instead of `set` |
| KTH104 | Use `Iterable` instead of `dict` |

## Rationale

https://docs.python.org/ja/3/library/typing.html#typing.List

>Note that to annotate arguments, it is preferred to use an abstract collection type such as `Sequence` or `Iterable` rather than to use `list` or `typing.List`.

https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#standard-duck-types

>Use Iterable for generic iterables (anything usable in "`for`"), and Sequence where a sequence (supporting "`len`" and "`__getitem__`") is required

https://typing.readthedocs.io/en/latest/reference/best_practices.html#arguments-and-return-types

>For arguments, prefer protocols and abstract types (`Mapping`, `Sequence`, `Iterable`, etc.).