Repository navigation
Replies: 1 comment
|
I think there are two separate questions here: whether an overload item can be represented by an arbitrary callable object, and whether an overload item can itself contain multiple possible call signatures. For the first case, a nominal class implementing For the second case, a union of callable types introduces an additional ambiguity. A union such as: Callable[[int], int] | Callable[[str], str]doesn't mean that the value supports both signatures. It means the value may implement either callable type, and callers must use arguments that are valid for every possible member. By contrast, an overloaded callable describes one callable supporting multiple signatures. Therefore, automatically flattening a union of callables into an overload set could be unsound. An overloaded callable type is a more plausible candidate for expansion into multiple overload items, but that would need explicit specification of how the resulting signatures are ordered, how overlapping signatures are handled, and whether decorators are allowed to change the effective overload set. I think these distinctions are worth making explicit before deciding whether decorated overload items should accept arbitrary callable types. For the original use case, the most portable approach today is still to declare the desired overload signatures directly rather than rely on a decorator to transform an individual overload item. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have a function which accepts either 1 positional
intargument or any number ofstrarguments, all passed positionally, and packed into a tuple upon return. I thought I could do this:This works in a non-overloaded context (
reveal_type(f_non_overloaded("a", "b"))should revealtuple[Literal['a'], Literal['b']]ortuple[str, str], and attempts to pass non-strs should emit typing errors). However, it doesn't work in an overloaded context.Some quick experiments reveal that
@transform_into_overloaded_callableis either not allowed on an overload item, or does not produce a valid overloaded callable type).(1) also results in confusing error messages across type-checkers; mypy and pyrefly both say something to the effect of
My questions are:
__call__be allowed as the type of an overload item? (My gut feeling says this should be allowed, otherwise someone who wants to use the signature of a nominal type's__call__will end up having to add a no-opdef deco[**P, R](f: Callable[P, R], /) -> Callable[P, R]: return fjust to appease type-checkers; pyright and pyrefly currently require this workaround).All reactions