-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support Callable[..., t] (with literal ellipsis, unspecified argument types and specified return type) #393
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
I notice that in the existing stubs you've tried to annotate
This is similar to the way people used to do things in C++03. Boost even came with a I think the easy answer is just to not try to fully annotate all possibilities. Yes, it means that |
You can just leave out types of (some) arguments or give them
|
My gut feeling is that it makes sense to have ugly annotations for We should probably have a fallback overload variant that takes *args to cover all cases (even if this would imply less precise types). However, for less frequently used modules/functions falling back to dynamic typing is probably the most reasonable approach. I.e., we'd consider Another argument is that one the first examples that a programmer with a functional programming background tries to type check uses |
You're missing the point here. (Probably my fault.) I'm asking how to declare the types of arguments (or return values) that are themselves functions. This is why I used
That's exactly my point. Sometimes, it's hard, verbose, or even impossible to annotate the argument types of the function argument, but easy to annotate the return type of the function argument. And often that's sufficient to provide a precise return type for the higher-order function. Again, look at So, it's useful to be able to specify an argument type that means |
Ah, sorry, I misunderstood your point. There is currently no way to do this. I guess we could have extra syntax for something like this, such as More generally, callable types are restricted to a fixed number of arguments and there's no way to have keyword arguments, Here are some (maybe stupid) ideas:
|
I wasn't even trying to solve the fully general problem, because I was assuming that it's unsolvable, or at least not worth solving. If you do want to solve it, it's even worse than you're thinking. Because of keyword arguments, even the parameter names can matter, not just the number and types. So you need something at least as complex as Getting this right is part of why programming in Swift, or doing compile-time metaprogramming in C++, is not as much fun as programming in Python. ML and Haskell solve this by just not allowing anything fancy. In Haskell, all functions take one argument; if you need more, you either take a tuple, or you write it curried (which are of course equivalent). That makes rigorously defining the type of any function dead simple. Too bad that won't work for Python (or C++ or Swift). But maybe the complicated cases don't need to be solved, as long as the simple ones can be. I think my proposal of just using
There's one intermediate case that seems potentially important: I want a function that takes an |
The current syntax seems to cover the majority of use cases for callable values. I was aware of the potential complexity and that's why the current annotation syntax is so simplistic. Another option would be to use
|
The PEP 484 draft uses |
There are a couple parts to this: * Compile module attribute accesses by compiling the LHS on its own instead of by loading its fullname (which will be what mypy thinks its source is) * Only use the static modules if they have actually been imported Closes #393.
It would be handy to be able to specify a callable whose argument types are not checked but whose return type is, e.g., by specifying the argument types as
None
instead of a list.For example, consider
map
:How would you annotate that? Trying to actually specify the argument types to
function
to match the element types of the iterables initerables
would require something horrible like C++11's parameter packs and variable-length type tuples. But just specifyingFunction
means there's no way to know that the result iterates values of the same type the function returns, which seems like a pity.If you could just use
None
orAny
instead of a list, you could do this:The text was updated successfully, but these errors were encountered: