Behavior for overlapping overload definitions #425
-
|
Hi, From what i understood there are still discussions about overload, but I've read here that overlapping overloads should have compatible return types as in this example from the same comment: class Animal: pass
class Dog(Animal): pass
@overload
def f(x: int) -> Dog: ...
@overload
def f(x: object) -> Animal: ...
var: object = 1
reveal_type(f(var)) # 'Dog' is a valid subtype of 'Animal':My impression from zuban (i think ty has similar plans), you decided to type ImmutableAttr = "MyFrozenDict | tuple[ImmutableAttr] | int | str"
@overload
def fn(a: Iterable[str]) -> tuple[str, ...]: ...
@overload
def fn(a: Mapping[str, Any]) -> MyFrozenDict: ...
def fn(a: Iterable[str]) -> tuple[str, ...] | MyFrozenDict:
if isinstance(a, Mapping):
return MyFrozenDict(a)
else:
return tuple(*a)
x: tuple[str, ...] = fn(["a", "b"])
y: MyFrozenDict = fn({"a": 1})Is my impression correct and you're planning to keep that behaviour (opposed to e.g., pyright)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Your observation is generally correct. I'm not exactly sure what you mean by
though. I think if you're interested how overloads are supposed to work you can read the official documentation here: https://typing.python.org/en/latest/spec/overload.html This is the current spec for overloads. Zuban doesn't exactly implement it this way, but it's very similar and should generally follow these rules. Pyright as far as I know is not compatible with the overload conformance tests and hasn't been for a long time. I'm closing, because I think this answers your initial question, but I'm happy to keep discussing this. |
Beta Was this translation helpful? Give feedback.
Your observation is generally correct. I'm not exactly sure what you mean by
though. I think if you're interested how overloads are supposed to work you can read the official documentation here:
https://typing.python.org/en/latest/spec/overload.html
This is the current spec for overloads. Zuban doesn't exactly implement it this way, but it's very similar and should generally follow these rules. Pyright as far as I know is not compatible with the overload conformance tests and hasn't been for a long time.
I'm closing, because I think this answers your initial question, but I'm happy to keep discu…