Issue 36320: typing.NamedTuple to switch from OrderedDict to regular dict (original) (raw)

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

Messages (7)

msg338095 - (view)

Author: Raymond Hettinger (rhettinger) * (Python committer)

Date: 2019-03-16 20:10

Suggestions:

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:12

admin

set

github: 80501

2020-04-04 21:43:10

serhiy.storchaka

set

messages: +

2020-04-04 20:56:21

serhiy.storchaka

set

nosy: + serhiy.storchaka

pull_requests: + <pull%5Frequest18732>

2019-03-18 16:56:15

rhettinger

set

status: open -> closed
resolution: fixed
stage: patch review -> resolved

2019-03-18 16:54:24

miss-islington

set

nosy: + miss-islington
messages: +

2019-03-18 16:33:06

josh.r

set

messages: +

2019-03-18 15:31:00

josh.r

set

nosy: + josh.r
messages: +

2019-03-18 08:28:03

rhettinger

set

keywords: + patch
stage: patch review
pull_requests: + <pull%5Frequest12351>

2019-03-17 19:09:42

rhettinger

set

messages: +

2019-03-17 14:26:07

levkivskyi

set

messages: +

2019-03-16 20:10:14

rhettinger

set

type: behavior

2019-03-16 20:10:01

rhettinger

create