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
Priority: normal Keywords: patch

Created on 2019-03-16 20:10 by rhettinger, last changed 2019-03-18 16:56 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12396 merged rhettinger, 2019-03-18 08:28
Messages (6)
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
History
Date User Action Args
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