You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PyStructSequence constructor ignores unknown field names #110235
PyStructSequence constructor takes two arguments: tuple and dict. The tuple specifies values for "visible" fields (i.e. what you get when interpret PyStructSequence as a tuple), the dict specifies values for fields that are accessible only by name (they are like __slot__ attributes).
The original code was added by @mwhudson in bpo-526072 (#36205, ce358e3). Because it was only for pickling support, omitting some validity checks was acceptable.
The problem is that all invalid names are silently ignored. It includes names of "visible" fields and typos.
Any thoughts on allowing the present visible fields in dict to override the values in the input sequence? Currently, we ignore visible field names in dict.
Since we need n_visible_fields <= len(sequence) <= n_fields in __new__(), all visible fields should be present in the sequence argument. If a field value is not allowed to come from both sequence and dict, the dict argument should only contain invisible field names. Also, we need to check if len(sequence) > n_visible_fields, the given invisible field provided in sequence should not present again in dict.
For collections.namedtuple, it will raise TypeError for duplicate arguments:
Activity
serhiy-storchaka commentedon Oct 2, 2023
The original code was added by @mwhudson in bpo-526072 (#36205, ce358e3). Because it was only for pickling support, omitting some validity checks was acceptable.
serhiy-storchaka commentedon Oct 2, 2023
@XuehaiPan, it may be interesting to you, since you already do something similar in the
__replace__().The difference is that this code cannot modify the input dict, because it is not kwargs. But it can count the found names.
XuehaiPan commentedon Oct 2, 2023
Any thoughts on allowing the present visible fields in
dictto override the values in the inputsequence? Currently, we ignore visible field names indict.Since we need
n_visible_fields <= len(sequence) <= n_fieldsin__new__(), all visible fields should be present in thesequenceargument. If a field value is not allowed to come from bothsequenceanddict, thedictargument should only contain invisible field names. Also, we need to check iflen(sequence) > n_visible_fields, the given invisible field provided insequenceshould not present again indict.For
collections.namedtuple, it will raiseTypeErrorfor duplicate arguments:XuehaiPan commentedon Oct 3, 2023
I opened a PR to resolve this.
TypeErrorfor duplicate/unknown fields in PyStructSequence constructor #110258gh-110235: Raise TypeError for duplicate/unknown fields in PyStructSe…
pythongh-110235: Raise TypeError for duplicate/unknown fields in PySt…