You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrapped overloaded function drops all but the first overload #15737
When wrapping an overloaded function and using ParamSpec to preserve the call signature, only the first overload is preserved on the resulting function.
Supporting overloads for ParamSpec is difficult. PEP 612 (which introduced ParamSpec) didn't contemplate this case or discuss the behaviors and limitations thereof. If the ParamSpec is used to parameterize a class or a generic type alias, it's not clear how to handle an overload. However, in the case where the function returns a Callable[P, T] (as the example above does), the meaning and behavior is well defined. I've implemented support for this in pyright, so the above type checks as you would expect. If someone is interested in implementing this in mypy and would like some tips or suggestions based on my experience, let me know.
@maffoo is the project you were running into this on open source?
Hi @hauntsaninja, yes, the project where I ran into this is in "duet", a small library for doing concurrency with futures: https://github.com/google/duet. In particular, there's a function duet.sync that wraps an async function into a synchronous version and we've been using a mypy plugin to get the typing right (https://github.com/google/duet/blob/main/duet/typing.py), but really what we'd like is to use ParamSpec and define duet.sync like:
Activity
erictraut commentedon Jul 22, 2023
Supporting overloads for
ParamSpecis difficult. PEP 612 (which introducedParamSpec) didn't contemplate this case or discuss the behaviors and limitations thereof. If theParamSpecis used to parameterize a class or a generic type alias, it's not clear how to handle an overload. However, in the case where the function returns aCallable[P, T](as the example above does), the meaning and behavior is well defined. I've implemented support for this in pyright, so the above type checks as you would expect. If someone is interested in implementing this in mypy and would like some tips or suggestions based on my experience, let me know.Basic support for decorated overloads (#15898)
hauntsaninja commentedon Aug 19, 2023
@maffoo is the project you were running into this on open source?
maffoo commentedon Aug 21, 2023
Hi @hauntsaninja, yes, the project where I ran into this is in "duet", a small library for doing concurrency with futures: https://github.com/google/duet. In particular, there's a function
duet.syncthat wraps an async function into a synchronous version and we've been using a mypy plugin to get the typing right (https://github.com/google/duet/blob/main/duet/typing.py), but really what we'd like is to useParamSpecand defineduet.synclike:Unfortunately, we use
duet.syncinternally with overloaded functions in many places, which is how I encountered this issue. I have a branch where I've switched to usingParamSpec(https://github.com/google/duet/tree/u/maffoo/param-spec). I tried out the latest version of mypy with that branch and it seems to work as expected (in particular see the file https://github.com/google/duet/blob/u/maffoo/param-spec/check.py). Thanks for fixing this!