-
Notifications
You must be signed in to change notification settings - Fork 258
Problem with @overload
ed decorators
#163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Part of this problem is just decorators that are trying to be too clever. In particular, they want to let you write
but also
Of course if we had better type checking in the first place we could have simplified things and required that the first example be written
Barring that, the problem seems to be that a decorator's type, even that for one which doesn't alter the signature of the decorated function, needs a more powerful way of expressing its signature than what you say. I think it should be possible to write this in a stub file:
and then use it like this:
I played with this and mypy doesn't yet support |
I think that using a type variable with a bound is the most reasonable way of implementing this. It's not a top priority thing for mypy right now as there are more serious issues that I want to address first. Falling back to dynamic typing (signature I've also been thinking about mypy specific, ad hoc type system plugins for commonly used library things that can't be represented using the current type system. So instead of adding a general (and complex) new type system feature that should be supported by all tools, we could have a dynamically or partially typed fallback that is 'good enough' and supported by all tools + individual tools can optionally special case some things to get better type checking coverage. |
PEP 484 now specifies type variable bounds, and the pattern in question is fully supported by mypy (I just tried on master). |
Currently, the best possible signature for a decorator is
because there is no other way to express that the callable will be preserved exactly.
However, in the wild, and this fails to work:
Possible solutions:
@overload
proposal, but a bad idea for producing meaningful error messages from a static type checker).@fallback_overload
decorator that only is considered if none of the others applies.Intersection
since it's needed for a coherent type system anyway.typeof(expr)
and use the argument names in the return value.Callable
, possibly with the following syntax:The text was updated successfully, but these errors were encountered: