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: structseq types should expose _fields
Type: enhancement Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Enhance Object/structseq.c to match namedtuple and tuple api
View: 1820
Assigned To: Nosy List: abarnert, ethan.furman, meador.inge, skrah, yselivanov
Priority: normal Keywords: patch

Created on 2014-01-13 01:12 by abarnert, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
structseq.diff abarnert, 2014-01-13 01:12 review
Messages (2)
msg207991 - (view) Author: Andrew Barnert (abarnert) * Date: 2014-01-13 01:12
A PyStructSequence type doesn't expose its field names in any way to Python code (except indirectly, via the repr, which you could parse if you really wanted to…).

The docs claim that "Struct sequence objects are the C equivalent of namedtuple() objects", so it seems like the obvious way to expose them is the exact same way namedtuple types do—as a class attribute named "_fields".

Also, in #7796, and in a recent thread on python-ideas, multiple people suggested that "named tuple" is a protocol, not a type, and duck typing on _fields is the obvious way to detect types matching that protocol.

Only the "sequence fields" need to be exposed—fields that don't match up to sequence indices aren't named tuple fields, they're just extra instance attributes (that don't even show up in the repr).

Patch included. For the tests, I tested the fields in struct_time and stat_result in effectively the same way test_repr tests their repr, so it should pass on all the same platforms/builds.
msg207993 - (view) Author: Andrew Barnert (abarnert) * Date: 2014-01-13 01:26
After more searching (I should have searched on structseq instead of PyStructSequence…), this might be a dup of #1820. If so, apologies.
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64429
2014-04-29 11:58:18skrahsetsuperseder: Enhance Object/structseq.c to match namedtuple and tuple api
2014-04-29 11:56:03skrahsetstatus: open -> closed
resolution: duplicate
stage: resolved
2014-04-27 11:06:45skrahsetnosy: + skrah
2014-01-13 04:27:12yselivanovsetnosy: + yselivanov
2014-01-13 04:26:48yselivanovsetcomponents: + Interpreter Core, Library (Lib)
versions: + Python 3.5
2014-01-13 01:33:04ethan.furmansetnosy: + ethan.furman
2014-01-13 01:26:53abarnertsetmessages: + msg207993
2014-01-13 01:14:13meador.ingesetnosy: + meador.inge
2014-01-13 01:12:54abarnertcreate