Reconsidering ParametersOf and ReturnType
Hello everyone. I created an issue on the mypy GitHub here, as I didn't know about this mailing list: https://github.com/python/mypy/issues/9472 I have been a Python programmer for 12 years now, and in my day-to-day job, I use TypeScript constantly now. The type checking in TypeScript is now very mature and quite easy to use, and I find myself every day wanting more for my back end Python code of what I already have for my front end TypeScript code. I think one lesson I've learned from my 3+ years of TypeScript use is that the TypeScript utility types are very useful, in particular the `Parameters` and `ReturnType` utility types, as documented here: https://www.typescriptlang.org/docs/handbook/utility-types.html#parametersty... Shantanu on GitHub was kind enough to point out that these utility types have been brought up before in a PEP, and rejected. See here: https://www.python.org/dev/peps/pep-0612/#defining-parametersof I believe it is worth reconsidering the efficacy of these utility types. As an end-user of TypeScript and Pyright, I can say that they make a lot of sense to me, and they are easy to explain to the people I train and manage. The specifics of how they are implemented I can't comment on, as I don't understand the full complexity of the Python type systems, but I can certainly say that they are obvious to the people I work with, and very useful, as we use them often in TypeScript code. The prior art on the part of TypeScript seems to indicate the utility types are useful as a general concept. I believe that if these utility types are implemented, they will form a part of solving the problem of how to correctly specify types for function decorators. Because it will be very easy to extract the parameters and return type of a Callable, you will be able to write a new type for a function that aliases one part and changes another part, such as using the same arguments for a function F as before, with a new return type, or the same return type as before, with a ParamSpec or list of arguments, etc. What does everyone else think about this topic?
Have you checked out [PEP 612](https://www.python.org/dev/peps/pep-0612/)? It defines `ParamSpec` and `Concatenate`. The return type can already be handled through `Callable` TypeVar matching. Does that combination of functionality satisfy the use cases you have in mind? PEP 612 is a Python 3.10 feature, but it is already implemented in Pyright if you want to give it a try. A more general `Parameters` mechanism is complicated in Python because of named parameters, which are not supported in TypeScript. In TypeScript, the `Parameters` utility function returns a tuple with each entry corresponding to a parameter position. It's not clear what an analogous mechanism would return in Python.
There is more detail on the decision to go with ParamSpec over the ParametersOf syntax on this point of the mailing list (https://mail.python.org/archives/list/typing-sig@python.org/message/DGU4SFZ6...) [It would also be worthwhile to take a look at that discussion in the context of the larger discussion on this thread (https://mail.python.org/archives/list/typing-sig@python.org/thread/UDHSH4EVV...)]. TL:DR, both syntaxes can express the same types, but using destructuring into variables fit the existing shape of the type syntax better than an operator driven syntax like they use consistently in TypeScript.
participants (3)
-
dev@w0rp.com
-
Eric Traut
-
Mark Mendoza