Skip to content

Add support of PyStructSequence in copy.replace() #110222

Closed

Description

@XuehaiPan

Feature or enhancement

Proposal:

This issue is to make the concept of "named tuple" support the __replace__ protocol.

collections.namedtuple and typing.NamedTuple already support the __replace__ protocol in:

PyStructSequences are also named tuples but they do not support the __replace__ protocol yet.

It would be convenient if PyStructSequence be supported in copy.replace().

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Linked PRs

Activity

  1. AlexWaygood commented on Oct 2, 2023

    @AlexWaygood
    Member
  2. XuehaiPan commented on Oct 2, 2023

    @XuehaiPan
    ContributorAuthor

    I created a PR to resolve this.

  3. serhiy-storchaka commented on Oct 2, 2023

    @serhiy-storchaka
    Member

    PyStructSequence with unnamed fields is a special case. Currently the only such structure is os.stat_result. Accessible by name fields st_atime, st_mtime and st_ctime are float values. Corresponding unnamed fields 7-9 are integer values. It is an old trick for compatibility. __replace__() can replace named fields, but unnamed fields will keep the old value. Integer and float value will be de-synchronized, and the code that reads unnamed fields will work incorrectly.

    The simplest solution is to disable __replace__() for PyStructSequence with unnamed fields. Such structures can even be deprecated, but this is a different issue.

    There may be a special implementation of __replace__() for os.stat_result, which derives values for unnamed fields from corresponding values for named fields. But float value can be less precise than integer value, because it has limited precision. So it needs to take into account *_ns fields. It is separate very complex issue.

  4. XuehaiPan commented on Oct 2, 2023

    @XuehaiPan
    ContributorAuthor

    PyStructSequence with unnamed fields is a special case. Currently the only such structure is os.stat_result. Accessible by name fields st_atime, st_mtime and st_ctime are float values. Corresponding unnamed fields 7-9 are integer values. It is an old trick for compatibility. __replace__() can replace named fields, but unnamed fields will keep the old value. Integer and float value will be de-synchronized, and the code that reads unnamed fields will work incorrectly.

    I can confirm this is a bug for PyStructSequence with unnamed fields if we rely on __reduce__(). But I'm not sure why pickle.loads(pickle.dumps(stat)) works fine for os.stat_result.

    The simplest solution is to disable __replace__() for PyStructSequence with unnamed fields. Such structures can even be deprecated, but this is a different issue.

    I agree to raise an error for this (e.g., TypeError) since os.stat_result is the only case to have unnamed fields.

    There may be a special implementation of __replace__() for os.stat_result, which derives values for unnamed fields from corresponding values for named fields. But float value can be less precise than integer value, because it has limited precision. So it needs to take into account *_ns fields. It is separate very complex issue.

    I think it's better to disable __replace__() for PyStructSequence with unnamed fields.

  5. added a commit that references this issue on Oct 4, 2023
  6. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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