Allowing multiple signature in subclass #11282
Replies: 1 comment
|
It isn't really about multiple signatures — it's
I narrowed down what triggers the collapse. Everything below is the same subclass method, only the declared type changes, on pyright 1.1.411: class M1:
f: Callable[[Self], int] # ok
class M2:
f: Callable[["M2"], int] # ok - concrete base type, still fine
type U1[T] = Callable[[T], int]
class M3:
f: U1[Self] # ok - alias with ONE member
type U2[T] = Callable[[T], int] | Callable[[T, int], tuple[int, int]]
class M4:
f: U2[Self] # ERROR
Worth also knowing that implementing the other arm doesn't help: class E5(M4):
def f(self, n: int) -> tuple[int, int]: return (n, n)
# line 49 Type "(self: E5, n: int) -> tuple[int, int]" is not assignable to type "U2[M4]"Both arms fail identically, which rules out "pyright picked the wrong member". That's also exactly why your CRTP version works, and I'd keep it. Writing class Model2[T]:
f2: Func[T]
class Example2(Model2["Example2"]):
def f2(self) -> int: return 1 # no errorIt's more ceremony at the declaration site, but it's saying the thing you actually mean, and it doesn't depend on One alternative I tried that does not work, so you can skip it: replacing the callable union with an overloaded Given |
Uh oh!
There was an error while loading. Please reload this page.
I have a use-case where i want to allow multiple different signature for methods when subclassing.
I've spend some time trying to make it pyright compliant and have discover some kind of weirdness.
pyright is fine if their is only one signature allowed (f1) but flags f2.
Is this normal behavior ? If so could someone explain to me what's happening :).
I've find a way to make pyright happy using CRTP pattern :
but if someone has something nicer i'll be glad.
I know it is a bit of a weird use case, my
Modelis some kind of dataclass (where some data are callables) that then get's normalized at class creation time.All reactions