Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MNT: handle generic descriptors in _setattr_cm#17561

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

tacaswell
Copy link
Member

PR Summary

If we are monkey-patching methods then we need to be sure that when
we restore the original attribute we need to make sure we don't
stick the method instance in place (which creates a circular
reference).

PR Checklist

  • Has Pytest style unit tests
  • Code isFlake 8 compliant

@tacaswelltacaswell added this to thev3.2.2 milestoneJun 3, 2020
@tacaswelltacaswell requested a review fromanntzerJune 3, 2020 20:55
@anntzer
Copy link
Contributor

I don't think that would work right e.g. for properties either.
What was the actual case where you needed this?
I guess the correct solution would be something like checking whetherattr in vars(obj) (in which case we grab the value and restore it later) or, if not, whetherhasattr(type(obj), attr) (in which case we don't restore it later) (the second check can't beattr in vars(type(obj)) as that would mishandle inheritance).

@jklymak
Copy link
Member

jklymak commentedJun 3, 2020
edited
Loading

This closes#17542 ? EDIT: sorry, confused the PRs...

@tacaswell
Copy link
MemberAuthor

I added a test for a property as well, it seems to work.

Noticed this while I was working on#17515 (where I ended up just doing it all "by hand") and this will be relevant for#17560 .

I do see how this is still going to go sideways for class attributes (as we end up with an equal value at the instance level shadowingsomething is the class hierarchy), but I think that this is still a net improvement.

@tacaswell
Copy link
MemberAuthor

@jklymak To be fair Ialmost put these commits in#17560 .

anntzer
anntzer previously approved these changesJun 4, 2020
Copy link
Contributor

@anntzeranntzer left a comment

Choose a reason for hiding this comment

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

I guess it's already an improvement :)

@tacaswell
Copy link
MemberAuthor

Added comments with links to both the tests and the implementation. I had tweaked the implementation a bit to give a better path to support un-masking class level attributes but in the process of writing out how it could be implemented I saw how to implement it. If people are unsure, I'm happy to drop the second commit.

@tacaswelltacaswell requested a review fromanntzerJune 4, 2020 22:52
if attr in obj.__dict__:
return True

if isinstance(getattr(type(obj), attr), property):
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps safer would be:

if attr in type(obj).__dict__ and hasattr( type(obj).__dict__[attr], '__set__'):

which covers all descriptors

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I thinkgetattr is better here so we see the super classes, but checking__set__ seems better 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

getattr will invoke some descriptors still. Perhapsinspect.getattrstatic?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

🤦 The rabbit hole gets deeper!

@tacaswell
Copy link
MemberAuthor

Well, this started as a special case for methods and turned into the full general case for descriptors and class level attributes...

@tacaswelltacaswell modified the milestones:v3.2.2,v3.3.0Jun 5, 2020
@tacaswell
Copy link
MemberAuthor

This is also now a bigger change than I think we should backport.

@tacaswelltacaswell modified the milestones:v3.3.0,v3.4.0Jun 9, 2020
@tacaswell
Copy link
MemberAuthor

Pushed to 3.4, can be moved back but I would rather focus on other PRs for 3.3.

@anntzer
Copy link
Contributor

If you want to get something in for 3.3, can we instead just error out for anything that's not in the instance__dict__ for now (and perhaps special case methods too), and add in features in an as-needed fashion? I'm not sure setattr_cm makes sense for arbitrary properties (or worse, arbitrary descriptors) anyways...

@QuLogicQuLogic modified the milestones:v3.4.0,v3.5.0Jan 22, 2021
@jklymakjklymak marked this pull request as draftApril 23, 2021 17:00
@QuLogicQuLogic modified the milestones:v3.5.0,v3.6.0Aug 23, 2021
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@tacaswelltacaswell changed the titleMNT: don't set method objects back onto the objectsMNT: handle generic descriptors in _setattr_cmApr 9, 2022
@tacaswelltacaswell added status: orphaned PR Good first issueOpen a pull request against these issues if there are no active ones! Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues and removed status: needs rebase labelsApr 9, 2022
@tacaswell
Copy link
MemberAuthor

I rebased this, but am going to orphan it.

I think the next step here is to write out a couple more pathological test cases involving custom descriptors.

Labeled as "good first issue" because there is not API design ("it goes back to as it was" is the API) but medium difficulty as it requires and understanding of the Python descriptor protocol (seehttps://docs.python.org/3/howto/descriptor.html ) which is not basic Python.

@tacaswelltacaswell modified the milestones:v3.6.0,unassignedApr 9, 2022
@anntzer
Copy link
Contributor

(I think we should just support instance attributes and a small, explicitly listed subset of descriptors (each of which should be motivated by an actual use case), as writing a general utility seems to be way more complex than it is really worth).

@tacaswell
Copy link
MemberAuthor

Fair, I'm also open to "close this as a bad idea" ;)

@timhoffm
Copy link
Member

timhoffm commentedApr 15, 2022
edited
Loading

I agree with@anntzer. Let's do all the things we need but not more (=motivated by an actual use case).

@tacaswelltacaswell removed the Good first issueOpen a pull request against these issues if there are no active ones! labelApr 15, 2022
@tacaswell
Copy link
MemberAuthor

Given the feedback, I'm going to close this.

If we end up needing something more sophisticated we will either remember and revive this or implement something more narrowly tailored.

@tacaswelltacaswell deleted the fix_method_patching branchApril 15, 2022 14:50
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@QuLogicQuLogicQuLogic left review comments

@eric-wiesereric-wiesereric-wieser left review comments

@anntzeranntzeranntzer left review comments

Assignees
No one assigned
Labels
Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesstatus: needs testsstatus: orphaned PR
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@tacaswell@anntzer@jklymak@timhoffm@QuLogic@eric-wieser

[8]ページ先頭

©2009-2025 Movatter.jp