Add Expand special form to allow typing **kwargs with a TypedDict
Hello, the ability to type **kwargs using TypedDict is a popular request (https://github.com/python/mypy/issues/4441). The proposal to use the form **kwargs: **TypedDictType is problematic because it causes a syntax error. Introducing a new Expand special form would enable adding this feature quite easily. There is a preliminary PR to support this feature in mypy - https://github.com/python/mypy/pull/10576 What are your opinions on this?
I'm strongly in favor given how popular this is. I'd brought up a proposal for this in the past along with some examples and edge cases: [1]. This includes handling things like inherited TypedDicts and compatibility rules for such functions, which differ from the compatibility rules for TypedDict. As mentioned in the above thread, the main advantages are: 1. better type safety: we get precise types for each keyword instead of using a broad `**kwargs: object`. We also forbid extraneous keyword arguments unless specified. This cannot be done right now. 2. code reuse: we can use TypedDict inheritance to avoid having to repeat the same keyword-only parameters in multiple places (and make mistakes). This seems to be needed a lot in certain class hierarchies. For example, in Apache airflow, `PythonVirtualEnvOperator` subclasses `PythonOperator` [2]. Its constructor accepts a bunch of keyword-only parameters, uses some of them directly, and passes on the remaining keyword parameters to `PythonOperator`'s constructor. That in turn expects certain keyword-only parameters and passes the rest to its parent class constructor. So, the TypedDict hierarchy for the keyword arguments would naturally mirror the class hierarchy. Regarding syntax, we could reuse `Unpack` from PEP 646: `def foo(**kwargs: Unpack[Movie]) -> None:` or add a new operator like `UnpackKwargs`. That would avoid a syntax change for `**Movie` unless necessary. [1]: https://mail.python.org/archives/list/typing-sig@python.org/message/MLMTWLOZ... [2]: https://github.com/apache/airflow/blob/main/airflow/operators/python.py#L299... On Tue, Aug 10, 2021 at 2:16 PM <framagie@gmail.com> wrote:
Hello,
the ability to type **kwargs using TypedDict is a popular request ( https://github.com/python/mypy/issues/4441).
The proposal to use the form **kwargs: **TypedDictType is problematic because it causes a syntax error. Introducing a new Expand special form would enable adding this feature quite easily.
There is a preliminary PR to support this feature in mypy - https://github.com/python/mypy/pull/10576
What are your opinions on this? _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: gohanpra@gmail.com
-- S Pradeep Kumar
+1 as well, from a dynamic-typing guy. On Tue, 2021-08-10 at 15:09 -0700, S Pradeep Kumar wrote:
I'm strongly in favor given how popular this is. I'd brought up a proposal for this in the past along with some examples and edge cases: [1]. This includes handling things like inherited TypedDicts and compatibility rules for such functions, which differ from the compatibility rules for TypedDict.
As mentioned in the above thread, the main advantages are:
1. better type safety: we get precise types for each keyword instead of using a broad `**kwargs: object`. We also forbid extraneous keyword arguments unless specified. This cannot be done right now. 2. code reuse: we can use TypedDict inheritance to avoid having to repeat the same keyword-only parameters in multiple places (and make mistakes).
This seems to be needed a lot in certain class hierarchies. For example, in Apache airflow, `PythonVirtualEnvOperator` subclasses `PythonOperator` [2]. Its constructor accepts a bunch of keyword-only parameters, uses some of them directly, and passes on the remaining keyword parameters to `PythonOperator`'s constructor. That in turn expects certain keyword-only parameters and passes the rest to its parent class constructor. So, the TypedDict hierarchy for the keyword arguments would naturally mirror the class hierarchy.
Regarding syntax, we could reuse `Unpack` from PEP 646: `def foo(**kwargs: Unpack[Movie]) -> None:` or add a new operator like `UnpackKwargs`. That would avoid a syntax change for `**Movie` unless necessary.
[1]: https://mail.python.org/archives/list/typing-sig@python.org/message/MLMTWLOZ... [2]: https://github.com/apache/airflow/blob/main/airflow/operators/python.py#L299...
On Tue, Aug 10, 2021 at 2:16 PM <framagie@gmail.com> wrote:
Hello,
the ability to type **kwargs using TypedDict is a popular request (https://github.com/python/mypy/issues/4441).
The proposal to use the form **kwargs: **TypedDictType is problematic because it causes a syntax error. Introducing a new Expand special form would enable adding this feature quite easily.
There is a preliminary PR to support this feature in mypy - https://github.com/python/mypy/pull/10576
What are your opinions on this? _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: gohanpra@gmail.com
_______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: pbryan@anode.ca
So far I only went through PEP 646 briefly and I am not really convinced if reusing Unpack would be a good thing or not. Will try to give it more thought but for now having a seprate special form like Expand or UnpackKwargs makes more sense to me. On the other hand, from user's perspective being able to reuse Unpack would be more comfortable.
participants (4)
-
framagie@gmail.com
-
Franek Magiera
-
Paul Bryan
-
S Pradeep Kumar