This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Docs: TypedDict is now of type function instead of class
Type: Stage:
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Anthony Sottile, andrei.avk, docs@python, gaborjbernat, gvanrossum, kj, serhiy.storchaka, ssbarnea
Priority: normal Keywords:

Created on 2020-10-08 06:26 by gaborjbernat, last changed 2022-04-11 14:59 by admin.

Messages (7)
msg378212 - (view) Author: gaborjbernat (gaborjbernat) * Date: 2020-10-08 06:26
❯ py -3.8 -c 'from typing import TypedDict; print(type(TypedDict))'
<class 'typing._TypedDictMeta'>

❯ py -3.9 -c 'from typing import TypedDict; print(type(TypedDict))'
<class 'function'>

Python 3.9 changed the type of the TypedDict but the documentation still says it's a class - see https://docs.python.org/3.9/library/typing.html#typing.TypedDict. I must also say I'm suprised you can inherit from a function, but we should update the documentation to reflect that the type of the element in question is no longer class.
msg378213 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2020-10-08 06:32
Seems to be due to https://bugs.python.org/issue40187
msg378216 - (view) Author: gaborjbernat (gaborjbernat) * Date: 2020-10-08 06:41
Static checkers/linters (see e.g. https://github.com/PyCQA/pylint/issues/3884) are caught of guard by being able to inherit from functions. They'll need to adapt, but let's update the documentation to reflect this.
msg378217 - (view) Author: gaborjbernat (gaborjbernat) * Date: 2020-10-08 06:44
This seem to apply to typing.NamedTuple too.
msg398707 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-01 15:19
I haven't looked too much into typing module, so take this with a grain of salt, but my understanding is that TypedDict is a special construct (as the first sentence in the docs defines it), which can be used as a base. I think most users would consider something a kind of a class if it can be inherited from by a user-defined class, so this classification in the docs is useful.

Normally functions can't be used as a base of course. There's no "inheritable from" abc or anything like that and functions are not inherited from "function type". You generally don't care if something is a function, you care if it's a callable and test for that with `callable()`.

So in a sense a function is the most inherently duck typed object in Python.

The only way for a specific check of a function type is AFAIK type(known_function) == type(myobj), but this type of checking is discouraged.

So static checkers that run into a problem with this are probably assuming too much about what a function is or should be in Python.

To sum up, TypedDict is a kind of a construct between a class and a function that's more usefully documented as, and used as, a class. The docs make it very clear that it's not your usual, run of the mill class.
msg398717 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-01 17:36
TypeDict is a callable, it can be used as

    Point2D = TypedDict('Point2D', x=int, y=int, label=str)

It can also be used as a base in class definition, but is is not a class itself. It does not occur in __bases__ of the created class, and it cannot be used in isinstance() and issubclass() checks. It is just yet one weird way to customize class creation.

I agree that documenting it as class is not correct. Perhaps it should be declared as a function (with specifying its signature when used as a function) and referred with the :data: role.
msg398718 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-01 18:01
Actually most names defined in typing are no longer classes.
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86139
2021-08-01 18:01:50serhiy.storchakasetversions: + Python 3.11
nosy: + gvanrossum, docs@python, kj

messages: + msg398718

assignee: docs@python
components: + Documentation
2021-08-01 17:36:05serhiy.storchakasetmessages: + msg398717
2021-08-01 15:19:43andrei.avksetnosy: + andrei.avk
messages: + msg398707
2020-10-15 16:13:57ssbarneasetnosy: + ssbarnea
2020-10-08 06:44:25gaborjbernatsetmessages: + msg378217
2020-10-08 06:41:07gaborjbernatsetmessages: + msg378216
2020-10-08 06:32:59Anthony Sottilesetnosy: + serhiy.storchaka, Anthony Sottile
messages: + msg378213
2020-10-08 06:26:46gaborjbernatcreate