Title: Increase `PyStructSequence` compatibility with `collections.namedtuple` · Issue #108647 · python/cpython · GitHub
Open Graph Title: Increase `PyStructSequence` compatibility with `collections.namedtuple` · Issue #108647 · python/cpython
X Title: Increase `PyStructSequence` compatibility with `collections.namedtuple` · Issue #108647 · python/cpython
Description: 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 he...
Open Graph Description: 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: collection...
X Description: 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: collection...
Opengraph URL: https://github.com/python/cpython/issues/108647
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Increase `PyStructSequence` compatibility with `collections.namedtuple` ","articleBody":"# Feature or enhancement\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n`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\".\n\nhttps://github.com/python/cpython/blob/b62a76043e543fbb15cab7e9e8874a798bc600d2/Objects/structseq.c#L4-L7\n\nIMO, it'd be worth to increase compatibility between these two named tuple instances, and also make the extra functionality available in `PyStructSequence`.\n\nMany 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.\n\n---\n\nHere's a list of the `collections.namedtuple` helpers that are not available in `PyStructSequence`:\n\n- `_fields`\n - > Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples\n- `_asdict`\n - > Return a new [dict](https://docs.python.org/3/library/stdtypes.html#dict) which maps field names to their corresponding values.\n- `_field_defaults` (would return an empty dict, if implemented)\n - > Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples.\n- `_replace`\n - > Return a new instance of the named tuple replacing specified fields with new values:\n- `_make`\n - > Class method that makes a new instance from an existing sequence or iterable.\n\n### Proposal:\n\n```python\n>>> import sys\n>>> sys.version_info._fields\n('major', 'minor', 'micro', 'releaselevel', 'serial')\n>>> sys.version_info._asdict()\n{'major': 3, 'minor': 13, 'micro': 0, 'releaselevel': 'alpha', 'serial': 0}\n>>> sys.flags._fields\n('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')\n>>> sys.flags._asdict()\n{'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}\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-108648\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/FFY00","@type":"Person","name":"FFY00"},"datePublished":"2023-08-29T16:20:10Z","url":"https://github.com/python/cpython/issues/108647"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:fd78697f-1e7c-acca-0509-d8a7a56cd730 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D152:19B6A2:1E904D7:2848226:6B0231B0 |
| html-safe-nonce | ee1eec5e86fa8acf021082d2c282a5be923f3a9a8aa5eca4df1f15365fca4736 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTUyOjE5QjZBMjoxRTkwNEQ3OjI4NDgyMjY6NkIwMjMxQjAiLCJ2aXNpdG9yX2lkIjoiNDk5NzI4OTE5MzM3NDg4ODM2OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e4fbaabe76224e2e245d501994d9c8d8ddbcfed00a1ef30edad20ff326ab11e0 |
| hovercard-subject-tag | issue:1872007347 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/108647/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a0a380105a9f3b2cee830b3e599dfff139d3aa3165bfab2fcab2b2dd4b005efd/python/cpython/issues/108647 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a0a380105a9f3b2cee830b3e599dfff139d3aa3165bfab2fcab2b2dd4b005efd/python/cpython/issues/108647 |
| og:image:alt | 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: collection... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FFY00 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width