FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-110222: Add support of PyStructSequence in copy.replace() by XuehaiPan · Pull Request #110223 · python/cpython · GitHub

/ cpython Public

gh-110222: Add support of PyStructSequence in copy.replace() - #110223

Merged
serhiy-storchaka merged 30 commits into
python:mainfrom
XuehaiPan:copy-replace-structseq
Oct 4, 2023
Merged

gh-110222: Add support of PyStructSequence in copy.replace()#110223
serhiy-storchaka merged 30 commits into
python:mainfrom
XuehaiPan:copy-replace-structseq

Conversation

XuehaiPan commented Oct 2, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

Resolves #110222

The implementation in this PR is roughly equivalent to:

class SomeStructSeq:
    def __replace__(self, /, **kwargs):
        if self.n_unamed_fields > 0:
            raise TypeError(f'__replace__() is not supported for {self.__class__} '
                            'because it has unnamed field(s)')

        field_names = self._get_field_names()
        _, (tup, dct) = self.__reduce__()
        seq = list(tup)
        for i, name in field_names[:self.n_sequence_fields]:
            if name in kwargs:
                seq[i] = kwargs.pop(name)
        for name in field_names[self.n_sequence_fields:]:
            if name in kwargs:
                dct[name] = kwargs.pop(name)
        if kwargs:
            raise TypeError(f'Got unexpected field name(s): {list(kwargs)!r}')
        return self.__class__(seq, dct)

Refactored:

class SomeStructSeq:
    def __replace__(self, /, **kwargs):
        if self.n_unamed_fields > 0:
            raise TypeError(f'__replace__() is not supported for {self.__class__} '
                            'because it has unnamed field(s)')

        clone = self._clone()
        field_names = self._get_field_names()
        if kwargs:
            for i, name in enumerate(field_names):
                if name in kwargs:
                    clone[i] = kwargs.pop(name)
            if kwargs:
                raise TypeError(f'Got unexpected field name(s): {list(kwargs)!r}')
        return clone

serhiy-storchaka self-requested a review October 2, 2023 16:04
Comment thread Lib/test/test_copy.py Outdated
Comment thread Objects/structseq.c Outdated
XuehaiPan marked this pull request as ready for review October 2, 2023 16:59
XuehaiPan requested a review from rhettinger as a code owner October 2, 2023 16:59

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I am surprised to see a correct complex C code from a new contributor. It is not easy to write all these error checks and decrefs on the first version.

The implementation creates arguments (a tuple and a dict) for __new__() and calls it. It is not the most efficient way, because it creates intermediate tuple and dict and can create string keys for name-only fields multiple times, but it works, and performance is not critical. I'm just wondering, whether it was your first idea or you considered other approaches and they turned out more complex? Did you try to create a new instance with PyStructSequence_New(), then copy ob_items from old structure to the new, then iterate the kwargs dict in one pass and patch ob_items? It looks simpler to me, but I may have missed some details.

Comment thread Lib/collections/__init__.py Outdated

Copy link
Copy Markdown
Contributor Author

I'm just wondering, whether it was your first idea or you considered other approaches and they turned out more complex? Did you try to create a new instance with PyStructSequence_New(), then copy ob_items from old structure to the new, then iterate the kwargs dict in one pass and patch ob_items? It looks simpler to me, but I may have missed some details.

@serhiy-storchaka My first thought is to create a copy of the current structseq and then replace the new values from kwargs. But I found it is kind of complex to handle the unnamed fields. Then I implement it with a similar structure to the __reduce__() implementation.

I will refactor it with the former implementation since we have disabled the support for types with unnamed fields.

Copy link
Copy Markdown
Contributor Author

I will refactor it with the former implementation since we have disabled the support for types with unnamed fields.

Done.

rhettinger removed their request for review October 3, 2023 02:15

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Very well.

I have few more comments, mostly test suggestions.

Comment thread Lib/collections/__init__.py Outdated
Comment thread Lib/test/test_structseq.py Outdated
Comment thread Lib/test/test_structseq.py Outdated
Comment thread Lib/test/test_structseq.py Outdated
Comment thread Lib/test/test_structseq.py Outdated
Comment thread Objects/structseq.c Outdated
Comment thread Objects/structseq.c Outdated
Comment thread Lib/test/test_structseq.py Outdated
Comment thread Lib/test/test_structseq.py Outdated

Copy link
Copy Markdown
Member

LGTM!

serhiy-storchaka merged commit 3bbe3b7 into python:main Oct 4, 2023

Copy link
Copy Markdown
Member

Thank you for your contribution @XuehaiPan.

XuehaiPan deleted the copy-replace-structseq branch October 4, 2023 17:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support of PyStructSequence in copy.replace()

2 participants


Back | FazBrowse Home | New Git URL