Last active
January 21, 2022 04:03
-
-
Save stroxler/4760cbc5e49df2295cd0b524328b1c73 to your computer and use it in GitHub Desktop.
How might `inspect.signature_of_type` work?
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
class Dog: | |
def __init__(self, color: str) -> None: | |
self.color = color | |
def __call__(self, command: str) -> None: | |
print("Dog will now " + command) | |
inspect.signature(Dog("brown")) == inspect.signature(Dog) # False | |
inspect.signature(Dog("brown")) == inspect.signature_of_type(Dog) # True |
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
from typing import * | |
FuncType0 = Callable[[int, str], bool] | |
class FuncType1(Protocol): | |
def __call__(self, __0: int, __1: str, /) -> bool ... | |
inspect.signature_of_type(FuncType0) == inspect.signature_of_type(FuncType1) |
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
from typing import * | |
P = ParamSpec("P") | |
FuncType0a = Callable[P, bool] | |
FuncType0b = Callable[Concatenate[int, P], bool] | |
class FuncType1a(Protocol): | |
def __call__(self, *__args: P.args, **__kwargs: P.kwargs) -> bool ... | |
class FuncType1b(Protocol): | |
def __call__(self, __0: int, /, *__args: P.args, **__kwargs: P.kwargs) -> bool ... | |
inspect.signature_of_type(FuncType0a) == inspect.signature_of_type(FuncType1a) | |
inspect.signature_of_type(FuncType0b) == inspect.signature_of_type(FuncType1b) |
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
from typing import * | |
Ts_varadic = TypeVarTuple("Ts") | |
FuncType0a = Callable[[int, *Ts], bool] | |
class FuncType1a(Protocol, Generic[Ts]): | |
def __call__(self, __0: int, *__args: *Ts) -> bool ... | |
inspect.signature_of_type(FuncType0a) == inspect.signature_of_type(FuncType1a) | |
# But what about this? We can't have a positional-only argument after the vararg, | |
# so there's no way to express this as a callback protocol! | |
# | |
# We could still produce a Signature where the final `str` is marked as | |
# POSITIONAL_ONLY in spite of coming after the vararg. This probably makes sense, | |
# but such a signature would be impossible to produce using the existing | |
# `inspect.signature` function since no method could actually expose that signature. | |
FuncType0a = Callable[[int, *Ts, str], bool] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment