-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Document TypedDict #3583
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
Document TypedDict #3583
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice explanation. I just have three formatting questions.
docs/source/kinds_of_types.rst
Outdated
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An extra "of" here? I would just say "a fixed set of string keys"
docs/source/kinds_of_types.rst
Outdated
|
||
.. note:: | ||
|
||
If you pass a TypedDict object as an argument to a function, no type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if it is important for RtD, but you use two leading spaces here instead of three as everywhere else.
docs/source/kinds_of_types.rst
Outdated
|
||
.. note:: | ||
|
||
You need to install ``mypy_extensions`` using pip to use ``TypedDict``: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here you have four spaces instead of three.
(I edited the PR description so that the corresponding issue will be closed automatically). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with Ivan, it's a very clear piece of documentation.
docs/source/kinds_of_types.rst
Outdated
|
||
.. note:: | ||
|
||
If you pass a TypedDict object as an argument to a function, no type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
docs/source/kinds_of_types.rst
Outdated
|
||
.. code-block:: python | ||
|
||
movie['name'] # Okay, type is str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
docs/source/kinds_of_types.rst
Outdated
|
||
.. code-block:: python | ||
|
||
movie['director'] # Error: 'director' is not a valid key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
docs/source/kinds_of_types.rst
Outdated
------------------ | ||
|
||
You can use an alternative, class-based syntax to define a | ||
TypedDict in Python 3.6: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put "In Python 3.6" in front of the sentence.
docs/source/kinds_of_types.rst
Outdated
``'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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"key values" -> "keys" would be more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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