๐Ÿ“ฆ ftnext / kotoha-python-linter

โ˜… 0 stars โ‘‚ 1 forks ๐Ÿ‘ 0 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/ftnext/kotoha-python-linter.git
HTTPS git clone https://github.com/ftnext/kotoha-python-linter.git
SSH git clone git@github.com:ftnext/kotoha-python-linter.git
CLI gh repo clone ftnext/kotoha-python-linter
ftnext ftnext docs: Debug baseball script d5c5b7b 7 days ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ examples
๐Ÿ“ src
๐Ÿ“ tests
๐Ÿ“„ .gitignore
๐Ÿ“„ LICENSE
๐Ÿ“„ pyproject.toml
๐Ÿ“„ README.md
๐Ÿ“„ setup.py
๐Ÿ“„ README.md

flake8-kotoha

KoToHa: Kaizen Type Hint

Install

uvx (Recommended)

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

$ uvx --with flake8-kotoha flake8 .

Other options

pipx

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

venv + pip

$ 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

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

Usage

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

$ 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 codedescription
KTH101Use Iterable or Sequence instead of list
KTH102Use Iterable or Sequence instead of tuple
KTH103Use Iterable instead of set
KTH104Use 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/cheatsheetpy3.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.).