Skip to content
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

add __copy__ and __deep_copy__ to Cancelled #3105

Closed
wants to merge 4 commits into from

Conversation

jakkdl
Copy link
Member

@jakkdl jakkdl commented Oct 9, 2024

See python-trio/flake8-async#298 for why I want to be able to copy Cancelled.

I suppose there's a minor footgun added if people start copying Cancelled, NoPublicConstructor was added for a reason, but I think the gain outweighs it. Though of course another option is to simply get rid of NoPublicConstructor

I initially tried to write cleaner methods, where I was using __dict__ and the like, but they do not appear to pick up the relevant dunders so I simply listed everything relevant from https://docs.python.org/3/library/exceptions.html#exception-context

Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.58%. Comparing base (2a66a0d) to head (9aa6d36).
Report is 159 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3105   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files         121      121           
  Lines       18166    18191   +25     
  Branches     3275     3278    +3     
=======================================
+ Hits        18091    18116   +25     
  Misses         52       52           
  Partials       23       23           
Files with missing lines Coverage Δ
src/trio/_core/_exceptions.py 100.00% <100.00%> (ø)
src/trio/_core/_tests/test_run.py 100.00% <100.00%> (ø)
---- 🚨 Try these New Features:

Copy link
Member

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

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

Looks good as far as I can tell

Copy link
Member

@oremanj oremanj left a comment

Choose a reason for hiding this comment

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

This is a far better copy than most exception objects have, FYI. (I left a comment on the flake8-trio issue that motivated this.)

@jakkdl
Copy link
Member Author

jakkdl commented Oct 9, 2024

This is a far better copy than most exception objects have, FYI. (I left a comment on the flake8-trio issue that motivated this.)

Thank you! I think we don't need to bother doing it either then, and I'll throw out the complicated logic.

@jakkdl jakkdl requested a review from oremanj October 11, 2024 10:10
@jakkdl jakkdl requested a review from A5rocks October 19, 2024 13:15
@jakkdl
Copy link
Member Author

jakkdl commented Oct 20, 2024

it appears copy.copy has bigger issues with exceptions than trio.Cancelled, see python-trio/flake8-async#298 (comment)
I'll probably close this PR

@jakkdl jakkdl marked this pull request as draft October 20, 2024 13:50
@jakkdl
Copy link
Member Author

jakkdl commented Oct 30, 2024

okay I think my original reasoning for this is no longer valid - copying is too unreliable to use in a general library like trio-websocket that needs to handle arbitrary user exceptions, but we should probably make it possible to copy & pickle Cancelled for general usability reasons.

That's fairly straightforward by implementing __reduce__, but it's maybe bad that all other NoPublicConstructor classes remain unpickleable? I'm at least finding one somewhat related issue: #2135

@jakkdl jakkdl closed this Nov 25, 2024
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.

3 participants