pythonのtyping_extensions.Protocolがなぜ嬉しいか(propertyの例)
mypyで使えるProtocolが便利という話の例。 structural subtypingをやる際にgoでもほしいと想うことが多かった例がProtocolでは大丈夫なので良いなーという例(今回はそのうちのひとつだけを紹介)。
Protocol?
Protocolをいつ使いたくなるかというと、大抵は複数ある何かを同一視したくなった場合。
例えば以下の様な例があげられる。
- クラスの継承関係を無視して同様のふるまいを持つものを同一視したいとき
- ある値を持つものを同一視したいとき
- 関数とcallable objectを同一視したいとき
- 同一視した表現を受け取って再帰的に同一視した表現を返したいとき (self-reference)
mypyでの型チェックはnominalなので、こういう構造を利用して同一視したい場合にはstructural subtypingが欲しくなる。これ用のinterfaceをProtocolと呼んでいる。
状況説明
例えばただただnameが欲しいだけの状況を考えてみる。こういう関数に渡されることをイメージした状態。
# <?????> の所に入る値は後々明らかになる def get_name(o: <?????>) -> str: return o.name
nameが取得できれば同じと見做す。そのような状況のときに、外から眺めた見た目としては、以下の2つのクラスは同じふるまいをするように見える(属性アクセスでnameが取れるので)。
class Person: name: str def __init__(self, name: str) -> None: self.name = name class Display: def __init__(self, typ: t.Type[t.Any]) -> None: self.typ = typ @property def name(self) -> str: return self.typ.__name__
例えば以下の様な形で使われる。
get_name(Person("foo")) # => "foo" get_name(Display(Person)) # => "Person"
duck-typing としてはとても自然。
Protocol を記述
そんなわけで上の様に動作することを期待するProtocolを記述したい。見た目だけで考えると以下のように書けると思うかもしれない。name
が見えれば良いので。
from typing_extensions as tx class HasName(tx.Protocol): name: str
しかしこれはだめ。以下のようなエラーが出る。
$ mypy --strict 04protocol.py ... 04protocol.py:31: error: Argument 1 to "get_name" has incompatible type "Display"; expected "HasName" 04protocol.py:31: note: Protocol member HasName.name expected settable variable, got read-only attribute
エラーの意味はwriteableでもある属性を期待しているがread-onlyな属性が渡されているよということ。前者は後者を内包しているから一見通るのが正しいと感じもするけれど、確かに安全性の観点から考えるならread-onlyであることを強調したい(まじめに色々なことは考えていないけれど。意図しないミスを防ぎたいという観点で考えるとこの2つを混同して使っている状況に危うさを感じたりはしそう)。
そんなわけで以下の様なProtocolを書いてあげると大丈夫。
class HasName(tx.Protocol): @property def name(self) -> str: ...
というわけで先程まで秘密にしていたget_name()
の定義は以下の様になる。
def get_name(o: HasName) -> str: return o.name
今度は以下が動く。
def main() -> None: get_name(Person("foo")) get_name(Display(Person)) # get_name(object())
goでもこういうinterface(pythonでのProtocolは概ねgoのinterface)を定義したいのだよなー。例えば自動生成系のやつなどでstructに値を持つ形で済ませられれば最高なのだけれど、メソッドを強制されたりするので。
$ mypy --strict 05protoco.py
やりましたね。手元で動かしたい人の為のgist
refs
pythonでlogging.Loggerとlogging.LoggerAdapterを許す型を定義する方法のメモ
pythonでlogging.Loggerとlogging.LoggerAdapterを許す型を定義する方法のメモ。
別の言い方をするとunion typeとprotocolを使ったstructured subtypingの違いのメモ。
(:warning: おそらくまだProtocolをimportするにはtyping_extensionsが必要です)
例
例えば以下のような関数があるとする。これを利用したい。duck typing的にはl.info()
の形で使えれば十分。
def use(l: logging.Logger) -> None: l.info("hello")
ここでちょっとしたラッピングをしたオブジェクトでも使いたくなる。
こういうときにはlogging.LoggerAdapterを使ってラップすることが多い(逆に言うとLoggerAdapterを使わなかった場合にはログ出力中の行番号や呼び出された関数名などの位置に関する情報が壊れる)。
class WLogger(logging.LoggerAdapter): def process( self, msg: str, kwargs: t.MutableMapping[str, t.Any] ) -> t.Tuple[str, t.MutableMapping[str, t.Any]]: return f"Wrap [{msg}]", kwargs
利用してみる。大丈夫そう。2つ目は Wrap [%s]
でラップされた出力になっている。
$ python 00first.py INFO:__main__:hello (use) INFO:__main__:Wrap [hello] (use)
コードの全体は以下の様な以下の様なコード。
import typing as t import logging logger = logging.getLogger(__name__) class WLogger(logging.LoggerAdapter): def process( self, msg: str, kwargs: t.MutableMapping[str, t.Any] ) -> t.Tuple[str, t.MutableMapping[str, t.Any]]: return f"Wrap [{msg}]", kwargs def use(l: logging.Logger) -> None: l.info("hello") def main() -> None: logging.basicConfig( level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)" ) use(logger) use(WLogger(logger, {})) if __name__ == "__main__": main()
mypyでチェック
さて先程のコードをmypyでチェックしようとエラーが起きる。当たり前といえば当たり前だけれど。
$ mypy --strict 00first.py 00first.py:24: error: Argument 1 to "use" has incompatible type "WLogger"; expected "Logger"
logging.AdapterはLoggerではないので当然。コレへの対応方法をどうするかという話。
対応方法
対応方法は2つある。
- Unionを使う方法
- Protocolを使う方法
Unionを使う方法
1つの対応方法はUnionを使う方法。以下のような形に変更する。
LoggerType = t.Union[logging.Logger, logging.LoggerAdapter]
そしてuse()の型を変える。
-def use(l: logging.Logger) -> None: +def use(l: LoggerType) -> None: l.info("hello")
今度は大丈夫。
$ mypy --strict 01union.py
Protocolを使う方法
もう1つの方法はProtocolを使う方法。Protocolというのはまぁ雑に言えばTypeScriptのinterfaceのようなものとおもってくれれば良い。導入の経緯などはPEP544などを参考に。
signatureなどの確認にはtypeshedを覗くのが手軽。こんな感じの値を定義してあげる。
from typing_extensions import Protocol _SysExcInfoType = t.Union[ t.Tuple[type, BaseException, t.Optional[types.TracebackType]], t.Tuple[None, None, None], ] _ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException] class LoggerProtocol(Protocol): def info( self, msg: t.Any, *args: t.Any, exc_info: _ExcInfoType = ..., stack_info: bool = ..., extra: t.Optional[t.Dict[str, t.Any]] = ..., **kwargs: t.Any, ) -> None: ...
そしてuse()の型もこちらを使ったものに変更。こうしてあげると、logging.Loggerと同様のinfo()を持ったオブジェクトということになる(ちなみに可変長引数部分などが組み合わさったときの型の等値性が意外と厄介だったりする。まぁ今回の話とは別の話)。
-def use(l: logging.Logger) -> None: +def use(l: LoggerProtocol) -> None: l.info("hello")
こちらも大丈夫。
$ mypy --strict 02protocol.py
Unionを利用したものとProtocolを利用したものとの違いは?
次の疑問はUnionを利用したものとProtocolを利用したものとの違いは?ということになるかもしれない。これは以下の様な形で分かるかもしれない。
Unionを利用するとは?
例えばUnionはまだ分岐が終わっていない状態で型情報を保持し続けるので、特定の型が選択された状態の関数を利用するにはガード(isinstanceによる分岐)が必要。
def use(l: LoggerType) -> None: l.info("hello") use2(l) def use2(l: logging.LoggerAdapter) -> None: l.info("hai")
use2()の部分でダメ。
$ mypy --strict 03union.py 03union.py:19: error: Argument 1 to "use2" has incompatible type "Union[Logger, LoggerAdapter]"; expected "LoggerAdapter"
例えば以下の様に変える必要がある。
def use(l: LoggerType) -> None: l.info("hello") if isinstance(l, logging.LoggerAdapter): use2(l)
Protocolを利用するとは?
Protocolは静的なducktypingと見做せる。同様に具体的な型に直接変換されたりなどもしない。
$ mypy --strict 04protocol.py 04protocol.py:37: error: Argument 1 to "use2" has incompatible type "LoggerProtocol"; expected "LoggerAdapter"
そしてisinstanceによる分岐は動的に行われるので以下の分岐はOK。
def use(l: LoggerProtocol) -> None: l.info("hello") if isinstance(l, logging.LoggerAdapter): use2(l) def use2(l: logging.LoggerAdapter) -> None: l.info("hai")
一度制限されたProtocolになったものをより豊かなProtocolとして扱うことはできない。
$ mypy --strict 05protocol.py 05protocol.py:61: error: Argument 1 to "use2" has incompatible type "LoggerProtocol"; expected "LoggerProtocol2" 05protocol.py:61: note: 'LoggerProtocol' is missing following 'LoggerProtocol2' protocol member: 05protocol.py:61: note: debug
このとき
def use(l: LoggerProtocol) -> None: l.info("hello") use2(l) def use2(l: LoggerProtocol2) -> None: l.info("hello") class LoggerProtocol2(Protocol): def debug( self, msg: t.Any, *args: t.Any, exc_info: _ExcInfoType = ..., stack_info: bool = ..., extra: t.Optional[t.Dict[str, t.Any]] = ..., **kwargs: t.Any, ) -> None: ... def info( self, msg: t.Any, *args: t.Any, exc_info: _ExcInfoType = ..., stack_info: bool = ..., extra: t.Optional[t.Dict[str, t.Any]] = ..., **kwargs: t.Any, ) -> None: ...
逆はOK。LoggerProtocol2はLoggerProtocolを含んでいるので。
$ mypy --strict 06protocol.py
このとき
def use(l: LoggerProtocol2) -> None: l.info("hello") use2(l) def use2(l: LoggerProtocol) -> None: l.info("hello")
両方利用した場合
ちなみにlogging.LoggerAdapterもlogging.Loggerも両者を含んでいるので以下もOK。つまりUnion[logger,LoggerAdapter]
も LoggerProtocolを満たす。
$ mypy --strict 07both.py
このとき
def use(l: LoggerType) -> None: l.info("hello") use2(l) use3(l) def use2(l: LoggerProtocol) -> None: l.info("hello") def use3(l: LoggerProtocol2) -> None: l.info("hello")