-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Increase PyStructSequence
compatibility with collections.namedtuple
#108647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
How do these proposed changes interact with the |
My proposal for that would be to either not consider unnamed fields in these helpers, or not provide the helpers for We could also consider trying to fit the unnamed fields into the API, but I don't think that's a good idea. |
I have updated the PR to not provide the helpers when there are unnamed fields. That's probably the safest bet right now, and we can change it in the future if we decide. |
Is it a duplicate of #46145? |
I'm not sure I recall all of the specifics correctly, but are you allowed to add unnamed fields to an existing type? Specifically, I'm thinking about backward compatibility. My concern is that if you added fields and suddenly the helpers vanished, this wouldn't be a very friendly change. But maybe I'm thinking about it backwards: things like What's an example of a stdlib type with unnamed fields? |
Yes, sorry! I missed that in my initial search.
Well, I think this is kind of a grey area. I think it heavily depends on the use-case, adding a field, either named or unnamed, will change the tuple format, so that can often be a backwards incompatible change on its own. I understand and share the concern about preventing people from adding unnamed fields to fully named AFAICT the only example in the stdlib with unnamed fields is |
Closing as duplicate |
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 inPyStructSequence
, its C API. Even though its documentation does not promisecollections.namedtuple
compatibility, it refers to it as a "named tuple".cpython/Objects/structseq.c
Lines 4 to 7 in b62a760
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
orsys.flags
to a dictionary.Here's a list of the
collections.namedtuple
helpers that are not available inPyStructSequence
:_fields
_asdict
_field_defaults
(would return an empty dict, if implemented)_replace
_make
Proposal:
Linked PRs
PyStructSequence
compatible withnamedtuple
#108648The text was updated successfully, but these errors were encountered: