GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MNT: handle generic descriptors in _setattr_cm by tacaswell · Pull Request #17561 · matplotlib/matplotlib

Conversation

@tacaswell

@tacaswell tacaswell commented Jun 3, 2020

Copy link
Copy Markdown
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 is Flake 8 compliant

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tacaswell tacaswell added this to the v3.2.2 milestone Jun 3, 2020
@tacaswell
tacaswell requested a review from anntzer June 3, 2020 20:55
@anntzer

anntzer commented Jun 3, 2020

Copy link
Copy Markdown
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 whether attr in vars(obj) (in which case we grab the value and restore it later) or, if not, whether hasattr(type(obj), attr) (in which case we don't restore it later) (the second check can't be attr in vars(type(obj)) as that would mishandle inheritance).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jklymak

jklymak commented Jun 3, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell
tacaswell force-pushed the fix_method_patching branch from 73855bf to 136fa20 Compare June 4, 2020 02:29
@tacaswell

tacaswell commented Jun 4, 2020

Copy link
Copy Markdown
Member Author

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 shadowing something is the class hierarchy), but I think that this is still a net improvement.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell

tacaswell commented Jun 4, 2020

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

anntzer
anntzer previously approved these changes Jun 4, 2020
View reviewed changes

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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 :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
QuLogic
QuLogic reviewed Jun 4, 2020
View reviewed changes
Comment thread lib/matplotlib/tests/test_cbook.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell
tacaswell force-pushed the fix_method_patching branch from 136fa20 to 066e6ef Compare June 4, 2020 22:37
@tacaswell

tacaswell commented Jun 4, 2020

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell
tacaswell requested a review from anntzer June 4, 2020 22:52
anntzer
anntzer reviewed Jun 4, 2020
View reviewed changes
Comment thread lib/matplotlib/cbook/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell
tacaswell force-pushed the fix_method_patching branch from e6db01f to 9abe140 Compare June 5, 2020 01:10
@tacaswell
tacaswell dismissed anntzer’s stale review June 5, 2020 01:12

Changed from last review

@QuLogic QuLogic mentioned this pull request Jun 5, 2020
FIX: add set_box_aspect, improve tight bounding box for Axes3D + fix bbox_inches support with fixed box_aspect #17515
Merged
1 of 6 tasks
QuLogic
QuLogic reviewed Jun 5, 2020
View reviewed changes
Comment thread lib/matplotlib/cbook/__init__.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

eric-wieser
eric-wieser reviewed Jun 5, 2020
View reviewed changes
Comment thread lib/matplotlib/cbook/__init__.py Outdated
if attr in obj.__dict__:
return True

if isinstance(getattr(type(obj), attr), property):

@eric-wieser eric-wieser Jun 5, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tacaswell tacaswell Jun 5, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think getattr is better here so we see the super classes, but checking __set__ seems better 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eric-wieser eric-wieser Jun 5, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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. Perhaps inspect.getattrstatic?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tacaswell tacaswell Jun 5, 2020

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tacaswell

tacaswell commented Jun 5, 2020

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell modified the milestones: v3.2.2, v3.3.0 Jun 5, 2020
@tacaswell

tacaswell commented Jun 5, 2020

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell
tacaswell force-pushed the fix_method_patching branch from 4bf4115 to 335cd7e Compare June 5, 2020 20:25
@tacaswell tacaswell modified the milestones: v3.3.0, v3.4.0 Jun 9, 2020
@tacaswell

tacaswell commented Jun 9, 2020

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anntzer

anntzer commented Jun 10, 2020

Copy link
Copy Markdown
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...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell mentioned this pull request Jun 11, 2020
MNT: make _setattr_cm more conservative #17620
Merged
2 tasks done
@tacaswell tacaswell mentioned this pull request Aug 25, 2020
Saving png figures matplotlib 3.3.0 results in "AttributeError: __delete__" matplotlib/ipympl#252
Closed
@QuLogic QuLogic added the status: needs rebase label Jan 21, 2021
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 22, 2021
@jklymak
jklymak marked this pull request as draft April 23, 2021 17:00
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Aug 23, 2021
@tacaswell @QuLogic
MNT: Also handle general descriptors in _setattor_cm
65e6fe4
Co-authored-by: Elliott Sales de Andrade <[email protected]>
@tacaswell
tacaswell force-pushed the fix_method_patching branch from 335cd7e to 65e6fe4 Compare April 9, 2022 21:49
@tacaswell tacaswell changed the title MNT: don't set method objects back onto the objects MNT: handle generic descriptors in _setattr_cm Apr 9, 2022
@tacaswell tacaswell added status: orphaned PR 🌱 Good first issue Open a pull request against these issues if there are no active ones! Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues and removed status: needs rebase labels Apr 9, 2022
@tacaswell

tacaswell commented Apr 9, 2022

Copy link
Copy Markdown
Member Author

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 (see https://docs.python.org/3/howto/descriptor.html ) which is not basic Python.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell modified the milestones: v3.6.0, unassigned Apr 9, 2022
@tacaswell tacaswell added the status: needs tests label Apr 9, 2022
@anntzer

anntzer commented Apr 9, 2022

Copy link
Copy Markdown
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).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell

tacaswell commented Apr 9, 2022

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm

timhoffm commented Apr 15, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell removed the 🌱 Good first issue Open a pull request against these issues if there are no active ones! label Apr 15, 2022
@tacaswell

tacaswell commented Apr 15, 2022

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell closed this Apr 15, 2022
@tacaswell
tacaswell deleted the fix_method_patching branch April 15, 2022 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@QuLogic QuLogic QuLogic left review comments
@anntzer anntzer anntzer left review comments
+1 more reviewer
@eric-wieser eric-wieser eric-wieser left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues status: needs tests status: orphaned PR

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@tacaswell @anntzer @jklymak @timhoffm @QuLogic @eric-wieser
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.