Skip to content

Increase PyStructSequence compatibility with collections.namedtuple  #108647

Closed as not planned
@FFY00

Description

@FFY00

Feature or enhancement

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

collections.namedtuple provides a couple useful helper functions, which are not present in PyStructSequence, its C API. Even though its documentation does not promise collections.namedtuple compatibility, it refers to it as a "named tuple".

The structseq helper is considered an internal CPython implementation
detail. Docs for modules using structseqs should call them
"named tuples" (be sure to include a space between the two
words and add a link back to the term in Docs/glossary.rst).

IMO, it'd be worth to increase compatibility between these two named tuple instances, and also make the extra functionality available in PyStructSequence.

Many times already, I have had the need for some of these helpers, with them being unavailable. An example use-case would be converting sys.version_info or sys.flags to a dictionary.


Here's a list of the collections.namedtuple helpers that are not available in PyStructSequence:

  • _fields
    • Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples

  • _asdict
    • Return a new dict which maps field names to their corresponding values.

  • _field_defaults (would return an empty dict, if implemented)
    • Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples.

  • _replace
    • Return a new instance of the named tuple replacing specified fields with new values:

  • _make
    • Class method that makes a new instance from an existing sequence or iterable.

Proposal:

>>> import sys
>>> sys.version_info._fields
('major', 'minor', 'micro', 'releaselevel', 'serial')
>>> sys.version_info._asdict()
{'major': 3, 'minor': 13, 'micro': 0, 'releaselevel': 'alpha', 'serial': 0}
>>> sys.flags._fields
('debug', 'inspect', 'interactive', 'optimize', 'dont_write_bytecode', 'no_user_site', 'no_site', 'ignore_environment', 'verbose', 'bytes_warning', 'quiet', 'hash_randomization', 'isolated', 'dev_mode', 'utf8_mode', 'warn_default_encoding', 'safe_path', 'int_max_str_digits')
>>> sys.flags._asdict()
{'debug': 0, 'inspect': 0, 'interactive': 0, 'optimize': 0, 'dont_write_bytecode': 0, 'no_user_site': 0, 'no_site': 0, 'ignore_environment': 0, 'verbose': 0, 'bytes_warning': 0, 'quiet': 0, 'hash_randomization': 1, 'isolated': 0, 'dev_mode': False, 'utf8_mode': 0, 'warn_default_encoding': 0, 'safe_path': False, 'int_max_str_digits': 4300}

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions