mypy
from Python
Type hints cheat sheet - mypy 1.10.0 documentation
Running mypy and managing imports - mypy 1.7.1 documentation
py.typed is 何
スパータイプ系いろいろ
collections.abc --- コンテナの抽象基底クラス — Python 3.13.1 ドキュメント
Mapping[K, V]: dict, TypedDict どっちも取る時に割合よく使うか
Iterable[T]: イテレート可能なもの、list, set, dict, generator
Sequence[T]: list, tuple, str など
Container[T]; in 演算子使えるもの
Sized: len() をサポートするもの
Collection[T]: Sized, Iterable, Container
type statement
7. 単純文 (simple statement) — Python 3.12.3 ドキュメント
あれ type Foo = Literal[...] のような type あるんだ、とおもったら 3.12
[* ParamSpec
typing --- 型ヒントのサポート — Python 3.13.1 ドキュメント
以下のように使える
code:paramspec.py
P = ParamSpec("P")
R = TypeVar("R")
def my_decoreater(f: CallableP, R) -> CallableP, R:
@wraps(f)
def decorated(*args: P.args, **kwargs: P.kwargs) -> R:
...
return f(*args, **kwargs)
return decoreated
Never
Python 3.11の新機能:型チェッカーでロジックの間違いを検出できるtyping.assert_never関数とtyping.Never型 | gihyo.jp
assert_never 関数が呼ばれている行に到達できるケースがあると型チェックでエラーに
More types - mypy 1.9.0+dev.1cdeecdcc11115a3d15610801fdf91b0ed6bbe0a.dirty documentation
NoReturn?
pyproject.toml の mypy
code:pyproject.toml
tool.mypy
mypy_path = "stubs"
tool.mypy.overrides
module = "attr", "firebase_admin"
ignore_missing_imports = true
stubgen
Automatic stub generation (stubgen) — Mypy 0.910 documentation
mypy の型つくるやつ
$ stubgen -p selenium -o stubs
Python3.5のType Hintについて - Toku's Blog
missing library stubs or py.typed marker
/Mijinko/module is installed, but missing library stubs or py.typed markerが出たときの対処法
python/typeshed: Collection of library stubs for Python, with static types
PEP 561 – Distributing and Packaging Type Information | peps.python.org
そこそこ使われているライブラリでも発生する場合 PR したほうが良いのだろうか?
Issue search results
Add support for using the library with mypy · Issue 18 · acederberg/pydantic-settings-yaml
VSCode の mypy 拡張でサブディレクトリの設定を読んでくれない
monorepo などで困る
Automatically use mypy.ini or pyproject.toml when not in the root directory of the workspace · Issue 190 · microsoft/vscode-mypy
code:settings.json
"mypy-type-checker.args": [
"--config-file=${workspaceFolder}/python-api/pyproject.toml",
],
ignore_missing_imports しても no-untyped-call に怒られる
code:sample.py
import google.auth
creds, _ = google.auth.default()
などで error: Call to untyped function "default" in typed context [no-untyped-call] で怒られる
外部ライブラリで型提供していなかったら ignore_missing_importsやと言われるが、
code:pyproject.toml
tool.mypy.overrides
module = "google.auth", "google.auth.*"
ignore_missing_imports = true
disallow_untyped_calls = false
などしてだめ
おそらくこの overrides 側の disallow_untyped_calls=false は意味なくて
呼び出し側が untyped な call をしているわけだから disallow_untyped_calls=false しないと効かない?
となると全体に影響が出て嫌だなあと思っていると untyped_calls_excludes があった
code:pyproject.toml
tool.mypy
disallow_untyped_calls = true
untyped_calls_exclude = "google.auth"
これで解決
Generator の型
from collections.abc import Generator, Iterator
Generator[T, U, V]
yield で返す値, send で受け取る値, return で返す値
Iterator[T] は Generator[T, None, None] と実質的におなじ
id か name どちらかをとる
@overload https://blog.edward-li.com/tech/advanced-python-features/