Skip to content

bpo-8722: Document __getattr__ behavior with AttributeError in property#4754

Merged
ncoghlan merged 4 commits into
python:masterfrom
csabella:bpo8722
Feb 5, 2018
Merged

bpo-8722: Document __getattr__ behavior with AttributeError in property#4754
ncoghlan merged 4 commits into
python:masterfrom
csabella:bpo8722

Conversation

@csabella

@csabella csabella commented Dec 8, 2017

Copy link
Copy Markdown
Contributor

When a property get method raises an AttributeError, __getattr__ is called if it exists. This is the same as when __getattribute__ fails to find an attribute.

https://bugs.python.org/issue8722

@terryjreedy terryjreedy 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.

In title, change 'DOC:' to document.

Suggested message for commit and blurb:

When a property get method raises AttributeError, getattr is called if it exists. This is the same as when getattribute fails to find an attribute.

Comment thread Doc/reference/datamodel.rst Outdated
an :exc:`AttributeError` because *name* is not an instance attribute, or
when it is not found in the class tree for ``self``. This method should
return the (computed) attribute value or raise an :exc:`AttributeError`
exception.

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.

You changed my suggested revision. I am guessing because it was slightly off, in that when a property raises, getattribute does not. However, I prefer to change 'usual places' because a property is not a place. I also prefer referring to properties second. How about:

Called when normal attribute lookup fails with an AttributeError. Either name is neither an instance attribute nor an attribute in the class tree for self, and meth:__getattribute__ raises an :exc:AttributeError; or :meth:__get__ of a name property raises :exc:AttributeError. This method should either return ... or raise... .

Any further revision?

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.

I left out the backticks needed to quote double underscores so that they are not interpreted as bold markup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't mean to take away any of the intention with your original suggested revision. From other tickets I've looked at, I've learned that there's a balance between maintaining some of the existing conversational feel to the docs (especially if it's existed for a long time) when modifying the docs, so I was trying to incorporate that. I was trying not to step on toes (and I stepped on yours :-) )

I had moved properties first because when looking at the attribute access order docs, it said that a descriptor method might override the default behavior, so I tried to indicate an order of application, but I don't think it matters. Actually, maybe instead of 'normal attribute lookup', we should use 'default attribute access', like in https://docs.python.org/3/howto/descriptor.html?highlight=descriptors#definition-and-introduction?

For revisions, the 'Either.. neither...' line somehow doesn't sound quite right. Maybe it should all be after a semi-colon or dash?

Called when the default attribute access fails with an AttributeError - either meth:getattribute raises an :exc:AttributeError because name is not an instance attribute or an attribute in the class tree for self; or :meth:get of a name property raises :exc:AttributeError. This method should either return ... or raise... .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@terryjreedy Do you think a test should be added to test_descr.py for this scenario (ie, combining a property with __getattr__)?

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@csabella csabella changed the title bpo-8722: DOC: __getattr__ behavior with AttributeError in property bpo-8722: Document __getattr__ behavior with AttributeError in property Dec 8, 2017
@csabella

Copy link
Copy Markdown
Contributor Author

@terryjreedy I made some changes to reflect the conversation we had on the last comment. You had posed several questions on the bug tracker. Was there anything you needed from me on that?

@terryjreedy

Copy link
Copy Markdown
Member

Please add a blurb, which will trigger re-testing. We can revise if we expand to set/delattr.

@csabella

csabella commented Feb 3, 2018

Copy link
Copy Markdown
Contributor Author

@terryjreedy Added a blurb.

@terryjreedy

Copy link
Copy Markdown
Member

I gather that markup is now allows in blurbs. Does it result in links from the html log to the docs?

If Nick does not respond, I will ask for another reviewer on core-mentorship. After 8 years, this should be finished ;-).

@csabella

csabella commented Feb 3, 2018

Copy link
Copy Markdown
Contributor Author

@terryjreedy In the blurb docs, it says:

Finally, go to the end of the file, and enter your NEWS entry. This should be a single paragraph of English text using simple ReST markup.

The 3.7 changelog does link back to the docs. https://docs.python.org/3.7/whatsnew/changelog.html#changelog

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm away from my computer this weekend, and the one behaviour I'd want to double check is whether overriding __getattribute__ without calling up to super().__getattribute__ also skips the __getattr__ fall back.

The docs could be made independent of that by deleting the phrase ":meth:__getattribute__ raises an :exc:AttributeError because" and just leaving the explanation of how that may fail.

@terryjreedy

Copy link
Copy Markdown
Member

After nearly 8 years, a couple of days will not hurt ;-).

@ncoghlan

ncoghlan commented Feb 5, 2018

Copy link
Copy Markdown
Contributor

Huh, turns out I was wrong and the proposed docs are right (the __getattribute__ docs do explain that the __getattr__ fallback is implemented outside __getattribute__: https://docs.python.org/3/reference/datamodel.html#object.__getattribute__).

So I'll go ahead and merge this. Thanks @csabella! :)

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @csabella for the PR, and @ncoghlan for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @csabella for the PR, and @ncoghlan for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-5542 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 5, 2018
…ty (pythonGH-4754)

When `__getattr__` is implemented, attribute lookup will always fall back to that,
even if the initial failure comes from `__getattribute__` or a descriptor's `__get__`
method (including property methods).
(cherry picked from commit d1f3181)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
@bedevere-bot

Copy link
Copy Markdown

GH-5543 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 5, 2018
…ty (pythonGH-4754)

When `__getattr__` is implemented, attribute lookup will always fall back to that,
even if the initial failure comes from `__getattribute__` or a descriptor's `__get__`
method (including property methods).
(cherry picked from commit d1f3181)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
@csabella csabella deleted the bpo8722 branch February 20, 2018 16:32
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.

6 participants