Created
November 7, 2020 06:01
-
-
Save gjo/fab281c4f97373b7dd4d47114fe316b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
`wired` (と、それを使う `pyramid_services`) で | |
`iface` に `Protocol` を使うと `isinstance` が使えなくなるメモ | |
たぶんwiredでなくてもクラスに何かを書き込むようなライブラリは | |
同じ理由で意図した動作にならないんだろう | |
""" | |
from typing import Protocol, runtime_checkable | |
from wired import ServiceRegistry | |
@runtime_checkable | |
class FooProto(Protocol): | |
i: int | |
def foo(self) -> str: ... | |
class Foo: | |
i = 10 | |
def foo(self) -> str: | |
return str(self.i) | |
foo = Foo() | |
assert isinstance(foo, FooProto) | |
registry = ServiceRegistry() | |
# ↓ここでwiredによって `FooProto._service_iface` が生成されてしまう | |
registry.register_singleton(foo, FooProto) | |
container = registry.create_container() | |
svc = container.get(FooProto) | |
assert svc is foo | |
# ↓`foo._service_iface` がないので、isinstanceがFalseになる | |
assert isinstance(foo, FooProto) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment