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: typing.NamedTuple to switch from OrderedDict to regular dict
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: levkivskyi Nosy List: josh.r, levkivskyi, miss-islington, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-03-16 20:10 by rhettinger, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12396 merged rhettinger, 2019-03-18 08:28
PR 19370 merged serhiy.storchaka, 2020-04-04 20:56
Messages (7)
msg338095 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-03-16 20:10
Suggestions:
* Deprecate _field_types in favor of __annotations__.
* Convert __annotations__ from OrderedDict to a regular dict.

This will make the API cleaner. The first one will also remove a difference between NamedTuple and namedtuple().  The second is consistent with the decision to convert _asdict() to a regular dictionary since OrderedDict is no longer necessary or desirable. The second will also make the signature of __annotations__ match that from other classes.
msg338130 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-03-17 14:26
Good idea! This should be easy to fix/update, this was initially discussed in https://github.com/python/typing/issues/339.
msg338136 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-03-17 19:09
Okay, I'll put together a PR and assign it to you :-)
msg338243 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2019-03-18 15:30
Would it make sense to convert _field_types to a property, so the method(s) that implement the property can do:

warnings.warn("_field_types is deprecated; use __annotations__ instead", DeprecationWarning)

to indicate the deprecation programmatically, not just in the documentation? The property could be backed by __annotations__ directly; they're already aliases of one another, so the only difference in behavior would be if someone was actually reassigning _field_types after class definition time (which I'm hoping is an invalid use case...). Would save some headaches for folks who run with warnings enabled, but don't read the What's New notices in detail.
msg338253 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2019-03-18 16:33
Blech. Just remembered _field_types is a *class* attribute, not an instance attribute, so it (just) can't be a plain property on NamedTuple itself.

And because NamedTuple is super-weird (AFAICT, class X(typing.NamedTuple): pass defines a class where the class is not a subclass of typing.NamedTuple, nor are its instances instances of NamedTuple, it's just wrapping an invocation of collections.namedtuple, which directly subclasses tuple with no metaclass involvement), and making a "class property" of the type we'd need requires a metaclass (which for tuple subclasses isn't an option), serious hackery or both ( https://stackoverflow.com/q/5189699/364696 ), it's probably not worth the effort to provide this warning. The only way to do it, AFAICT, would be to give the root tuple class a metaclass to provide the _field_types property, and that's a non-starter given it would, among other things, probably slow every single use of tuples just to provide the warning for this one niche case.

Boo.
msg338259 - (view) Author: miss-islington (miss-islington) Date: 2019-03-18 16:54
New changeset f7b57df0c09c3a04ab27ba06eb2feb989bbb16cb by Miss Islington (bot) (Raymond Hettinger) in branch 'master':
bpo-36320: Switch typing.NamedTuple from OrderedDict to regular dict (GH-12396)
https://github.com/python/cpython/commit/f7b57df0c09c3a04ab27ba06eb2feb989bbb16cb
msg365792 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-04 21:43
New changeset 0d1d7c8bae3f9fe9e937d2931dcbbd3555d1a9f1 by Serhiy Storchaka in branch '3.8':
bpo-36320: Use the deprecated-removed directive for _field_types (GH-19370)
https://github.com/python/cpython/commit/0d1d7c8bae3f9fe9e937d2931dcbbd3555d1a9f1
History
Date User Action Args
2022-04-11 14:59:12adminsetgithub: 80501
2020-04-04 21:43:10serhiy.storchakasetmessages: + msg365792
2020-04-04 20:56:21serhiy.storchakasetnosy: + serhiy.storchaka

pull_requests: + pull_request18732
2019-03-18 16:56:15rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-03-18 16:54:24miss-islingtonsetnosy: + miss-islington
messages: + msg338259
2019-03-18 16:33:06josh.rsetmessages: + msg338253
2019-03-18 15:31:00josh.rsetnosy: + josh.r
messages: + msg338243
2019-03-18 08:28:03rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request12351
2019-03-17 19:09:42rhettingersetmessages: + msg338136
2019-03-17 14:26:07levkivskyisetmessages: + msg338130
2019-03-16 20:10:14rhettingersettype: behavior
2019-03-16 20:10:01rhettingercreate