Skip to content

raise AttributeError in __getattr__ if lookup fails #95

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
merged 4 commits into from
Dec 5, 2016

Conversation

vincentschut
Copy link
Contributor

This fixes #88 by raising an AttributeError instead of a KeyError in getattr, which is the more correct thing to do (because hasattr only returns False when it gets an AttributeError, and propagates all other exceptions instead).

@alimanfoo
Copy link
Member

Thank you @vincentschut. Would you be able to add a unit test to cover this behaviour?

@vincentschut
Copy link
Contributor Author

Sure, will try. Though I'm not (yet) fluent in unittests, so beware: noob stupidities ahead! :-)
Would it be sufficient to add a line to test_hierarchy.py:test_getattr() which checks that hasattr does not raise but instead returns False on an non-existing attribute? Or would you rather have a separate, more extensive test?

@alimanfoo
Copy link
Member

alimanfoo commented Dec 5, 2016 via email

@vincentschut
Copy link
Contributor Author

Thank you for these detailed steps, very informative.
I think I nailed it, checks pass both locally and with Travis. Let me know if you expect anything else.

@@ -492,6 +492,8 @@ def test_getattr(self):
# test
eq(g1['foo'], g1.foo)
eq(g2['bar'], g2.bar)
# test that hasattr returns False instead of an exception (issue #88)
eq(hasattr(g1, '__unexistingattribute__'), False)
Copy link
Member

@alimanfoo alimanfoo Dec 5, 2016

Choose a reason for hiding this comment

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

Very minor nit-pick: it would be slightly more direct to use assert_false(hasattr(g1, '__unexistingattribute__')).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem, changed accordingly.

@alimanfoo
Copy link
Member

alimanfoo commented Dec 5, 2016 via email

@alimanfoo alimanfoo merged commit 70f7c1d into zarr-developers:master Dec 5, 2016
@alimanfoo
Copy link
Member

Great, thank you!

@alimanfoo alimanfoo added this to the v2.2 milestone Dec 15, 2016
@alimanfoo alimanfoo mentioned this pull request Oct 24, 2017
4 tasks
@alimanfoo alimanfoo modified the milestones: v2.2, v2.1.4 Nov 20, 2017
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.

Group __getattr__ should raise AttributeError
2 participants