Document TypedDict#3583
Conversation
ilevkivskyi
left a comment
There was a problem hiding this comment.
Very nice explanation. I just have three formatting questions.
|
|
||
| movie = {'name': 'Blade Runner', 'year': 1982} | ||
|
|
||
| Only a fixed of set of string keys is expected (``'name'`` and |
There was a problem hiding this comment.
An extra "of" here? I would just say "a fixed set of string keys"
|
|
||
| .. note:: | ||
|
|
||
| If you pass a TypedDict object as an argument to a function, no type |
There was a problem hiding this comment.
I am not sure if it is important for RtD, but you use two leading spaces here instead of three as everywhere else.
|
|
||
| .. note:: | ||
|
|
||
| You need to install ``mypy_extensions`` using pip to use ``TypedDict``: |
There was a problem hiding this comment.
And here you have four spaces instead of three.
|
(I edited the PR description so that the corresponding issue will be closed automatically). |
gvanrossum
left a comment
There was a problem hiding this comment.
Agreed with Ivan, it's a very clear piece of documentation.
|
|
||
| .. note:: | ||
|
|
||
| If you pass a TypedDict object as an argument to a function, no type |
There was a problem hiding this comment.
If you pass a TypedDict object as an argument to a function
I think you mean here "if you pass a dict literal as an argument to a function expecting a TypedDict object". (Except the term "dict literal" is not used anywhere else in the mypy docs.)
Also it's not just when passing to a function -- it's the same when assigning to a variable that's already typed as a TypedDict. Though that is perhaps too advanced?
|
|
||
| .. code-block:: python | ||
|
|
||
| movie['name'] # Okay, type is str |
There was a problem hiding this comment.
Maybe show these examples in a little more context, e.g. name = movie['name']? Then you can refer to the type of the variable in the comment, which is perhaps a simpler concept than the type of an expression.
|
|
||
| .. code-block:: python | ||
|
|
||
| movie['director'] # Error: 'director' is not a valid key |
There was a problem hiding this comment.
(Here it's less obvious that adding a variable would help, though for consistency maybe it's still the right thing to do.)
| However, mypy still lets use ``[]`` with a partial TypedDict -- you | ||
| just need to be careful with it, as it could result in a ``KeyError``. | ||
| Requiring ``get()`` everywhere would be too cumbersome. (Note that you | ||
| are free to use ``get()`` with total TypedDicts as well.) |
There was a problem hiding this comment.
Maybe you can explore the subject of totality and subtyping in more depth here? I admit that I'm thoroughly confused about the matter (as you can tell from some of my comments on PR #3558). The explanation later of how you can use subclassing to mix required and non-required items also makes me wonder.
| ------------------ | ||
|
|
||
| You can use an alternative, class-based syntax to define a | ||
| TypedDict in Python 3.6: |
There was a problem hiding this comment.
I'd put "In Python 3.6" in front of the sentence.
| ``'year'`` above), and each key has an independent value type (``str`` | ||
| for ``'name'`` and ``int`` for ``'year'`` above). We've previously | ||
| seen the ``Dict[K, V]`` type, which lets you declare uniform | ||
| dictionary types, where every value has the same type, and arbitrary key |
There was a problem hiding this comment.
"key values" -> "keys" would be more readable.
gvanrossum
left a comment
There was a problem hiding this comment.
LGTM too, but I think we may need to wait until totality is actually merged: #3558.
|
Great, this is now live at http://mypy.readthedocs.io/en/latest/kinds_of_types.html#typeddict |
|
What happened to this? When I search Google for "mypy typeddict" it takes me to the Kinds of Types page, but this content is no longer there. The only reason I know of it is seeing it in someone else's code. It is still mentioned all over the issue tracker but nowhere in the documentation it seems. |
Fixes #3453