Skip to content

MNT: make _setattr_cm more conservative #17620

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

Merged

Conversation

tacaswell
Copy link
Member

PR Summary

  • special case methods because they are descriptors
  • fail on any other non-instance attribute

This is a simpler version of #17561

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant

@tacaswell tacaswell added this to the v3.3.0 milestone Jun 11, 2020
@QuLogic
Copy link
Member

QuLogic commented Jun 11, 2020

CI does not like this at all.

@tacaswell
Copy link
Member Author

Indeed, I thought I ran the tests before I pushed. I must have lost track of what I was doing locally...

@tacaswell tacaswell force-pushed the simplified_setattr_cm_method_handling branch from 4111ab6 to d0849a6 Compare June 11, 2020 23:03
@tacaswell
Copy link
Member Author

🤦 I edited one working tree and installed and tested a different one.

@tacaswell tacaswell force-pushed the simplified_setattr_cm_method_handling branch from d0849a6 to 3b4da04 Compare June 12, 2020 00:01
Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

Not sure what features we should be supporting in the future (re: #17561), but at least this makes it clear what we don't.

origs = {}
for attr in kwargs:
orig = getattr(obj, attr, sentinel)
# monkey patching on a new attribute, this is OK
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps you can reorder this to make the common case first, e.g.

if (attr in obj.__dict__ or orig is sentinel
        or isinstance(getattr(type(obj), attr), (property, types.FunctionType)):
    origs[attr] = orig
else:
    raise ValueError(...)

Checking to types.FunctionType in the type is the same as checking for methods, and (as noted in the other PR, I think) won't give a wrong result if an unrelated bound method is assigned as instance var (self.foo = otherobj.method) -- at least I think this point should be fixed.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense. I agree the method from another object is a valid concern (sorry I didn't fully register it before).

@tacaswell tacaswell force-pushed the simplified_setattr_cm_method_handling branch from 6ee399a to 756b9c3 Compare June 14, 2020 20:42
@tacaswell tacaswell requested a review from anntzer June 14, 2020 20:42
Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

just style things.

 - special case methods and properties because they are descriptors
 - fail on any other non-instance attribute
@tacaswell tacaswell force-pushed the simplified_setattr_cm_method_handling branch from 756b9c3 to 9e88fa5 Compare June 15, 2020 15:43
@anntzer anntzer merged commit e86e059 into matplotlib:master Jun 15, 2020
@tacaswell tacaswell deleted the simplified_setattr_cm_method_handling branch June 15, 2020 18:29
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