GH-46145: make PyStructSequence compatible with namedtuple#108648
GH-46145: make PyStructSequence compatible with namedtuple#108648FFY00 wants to merge 21 commits intopython:mainfrom
PyStructSequence compatible with namedtuple#108648Conversation
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
_fields and _asdict to PyStructSequence_fields, _field_defaults, and _asdict to PyStructSequence
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
|
Sorry for the force-push mess, I should have taken some more time preparing the PR locally. |
…seq_dict Signed-off-by: Filipe Laíns <lains@riseup.net>
|
|
||
| // Set _fields_defaults to an empty dir, as we don't support defaults | ||
| defaults = PyDict_New(); | ||
| if (!defaults) |
There was a problem hiding this comment.
| if (!defaults) | |
| if (!defaults) { |
nit
Signed-off-by: Filipe Laíns <lains@riseup.net>
_fields, _field_defaults, and _asdict to PyStructSequence_fields, _field_defaults, and _asdict to PyStructSequence
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
|
It has been a while and nobody has complained about this, so I am tempted to merge it, but I'd like someone else to also sign off first. @serhiy-storchaka maybe you could have a look? |
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>
_fields, _field_defaults, and _asdict to PyStructSequencePyStructSequence compatible with namedtuple
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I do not think that it needs _replace after adding more general __replace__. _replace in namedtuples may be deprecated in future.
_make is also redundant. There is a great obstacle in unifying namedtuples and structseqs -- their constructors have incompatible interfaces (this is why implementing __replace__ was important). Namedtuple's _make is like a structseq constructor -- it takes field values as a tuple.
_asdict can easily be implemented as a general external function if we have a set of field names. It does not need a method.
Unfortunately, fields are represented differently in dataclasses and namedtuples. We should first provide an general way to get field names for dataclasses and namedtuples, and then we can add support for structseq.
BTW, I'm planning to implement different mechanism to create struct-like types. They will have fields of different types, so a dataclass-like protocol can be bore useful.
| def test_asdict_unnamed(self): | ||
| t = _testcapi.MixedNamedTuple((0, 1)) | ||
| self.assertRaises(ValueError, t._asdict) | ||
| def test_copy_replace_all_fields_visible(self): |
XuehaiPan
left a comment
There was a problem hiding this comment.
Will need to update dataclasses.asdict(...) and dataclass.astuple(...) and corresponding tests to handle namedtuple and structseq.
namedtule types are determined by heuristic:
def is_namedtuple(obj):
return isinstance(obj, tuple) and hasattr(obj, '_fields')Lines 1448 to 1468 in 9949091
Lines 1522 to 1529 in 9949091
|
This PR is stale because it has been open for 30 days with no activity. |
PyStructSequencecompatibility withcollections.namedtuple#108647