Skip to content

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

Merged
merged 2 commits into from
Jun 23, 2017
Merged

Document TypedDict #3583

merged 2 commits into from
Jun 23, 2017

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 21, 2017

Fixes #3453

Verified

This commit was signed with the committer’s verified signature.
heitorlessa Heitor Lessa
Copy link
Member

@ilevkivskyi ilevkivskyi left a 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.


movie = {'name': 'Blade Runner', 'year': 1982}

Only a fixed of set of string keys is expected (``'name'`` and
Copy link
Member

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"


.. note::

If you pass a TypedDict object as an argument to a function, no type
Copy link
Member

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.


.. note::

You need to install ``mypy_extensions`` using pip to use ``TypedDict``:
Copy link
Member

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.

@ilevkivskyi
Copy link
Member

(I edited the PR description so that the corresponding issue will be closed automatically).

Copy link
Member

@gvanrossum gvanrossum left a 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.


.. note::

If you pass a TypedDict object as an argument to a function, no type
Copy link
Member

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?


.. code-block:: python

movie['name'] # Okay, type is str
Copy link
Member

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.


.. code-block:: python

movie['director'] # Error: 'director' is not a valid key
Copy link
Member

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.)
Copy link
Member

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.

------------------

You can use an alternative, class-based syntax to define a
TypedDict in Python 3.6:
Copy link
Member

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.

``'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
Copy link
Member

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.

Verified

This commit was signed with the committer’s verified signature.
heitorlessa Heitor Lessa
Copy link
Member

@gvanrossum gvanrossum left a 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.

@ilevkivskyi ilevkivskyi merged commit 5d85832 into master Jun 23, 2017
@ilevkivskyi ilevkivskyi deleted the typeddict-doc branch June 23, 2017 06:48
@gvanrossum
Copy link
Member

@thejohnfreeman
Copy link

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.

@emmatyping
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants