Repository navigation
gh-122102: Improve tests for descriptor-related tools in inspect
#122144
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
9944ef6
40ce793
9e5c475
ca12cc2
ebcb0dc
9dab208
fc33fd5
130d795
ec79a04
8e2ea46
bcec47a
b249448
17928e4
288b883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
(further minor tweaks/improvements...)
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1832,10 +1832,10 @@ class MethodDescriptorSub(MethodDescriptor): | |
| MethodDescriptorWithGetNone, | ||
| MethodDescriptorSub, | ||
| ]: | ||
| with self.subTest(f"a descriptor {cls.__doc__}"): | ||
| with self.subTest(f"a descriptor {cls.__doc__}", cls=cls): | ||
| self.assertTrue(inspect.ismethoddescriptor(cls())) | ||
| with self.subTest(f"a descriptor class {cls.__doc__} " | ||
| f"(not a descriptor itself)"): | ||
| with self.subTest(f"a descriptor class (not a descriptor " | ||
| f"itself) {cls.__doc__} ", cls=cls): | ||
| self.assertFalse(inspect.ismethoddescriptor(cls)) | ||
|
|
||
| # Custom data descriptors: | ||
|
|
@@ -1908,10 +1908,10 @@ class DataDescriptorSub7(DataDescriptorWithSetOnly, | |
| DataDescriptorSub6, | ||
| DataDescriptorSub7, | ||
| ]: | ||
| with self.subTest(f"a descriptor {cls.__doc__}"): | ||
| with self.subTest(f"a descriptor {cls.__doc__}", cls=cls): | ||
| self.assertFalse(inspect.ismethoddescriptor(cls())) | ||
| with self.subTest(f"a descriptor class {cls.__doc__} " | ||
| f"(not a descriptor itself)"): | ||
| with self.subTest(f"a descriptor class (not a descriptor " | ||
| f"itself) {cls.__doc__} ", cls=cls): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, they become too verbose. Now I've carefully prepared more concise messages. I hope you find them more readable. |
||
| self.assertFalse(inspect.ismethoddescriptor(cls)) | ||
|
|
||
| def test_builtin_descriptors(self): | ||
|
|
@@ -2025,10 +2025,10 @@ class MethodDescriptorSub(MethodDescriptor): | |
| MethodDescriptorWithGetNone, | ||
| MethodDescriptorSub, | ||
| ]: | ||
| with self.subTest(f"a descriptor {cls.__doc__}"): | ||
| with self.subTest(f"a descriptor {cls.__doc__}", cls=cls): | ||
| self.assertFalse(inspect.isdatadescriptor(cls())) | ||
| with self.subTest(f"a descriptor class {cls.__doc__} " | ||
| f"(not a descriptor itself)"): | ||
| with self.subTest(f"a descriptor class (not a descriptor " | ||
| f"itself) {cls.__doc__} ", cls=cls): | ||
| self.assertFalse(inspect.isdatadescriptor(cls)) | ||
|
|
||
| # Custom data descriptors: | ||
|
|
@@ -2129,10 +2129,10 @@ class DataDescriptorSub15(DataDescriptorWithDeleteOnly, | |
| DataDescriptorSub14, | ||
| DataDescriptorSub15, | ||
| ]: | ||
| with self.subTest(f"a descriptor {cls.__doc__}"): | ||
| with self.subTest(f"a descriptor {cls.__doc__}", cls=cls): | ||
| self.assertTrue(inspect.isdatadescriptor(cls())) | ||
| with self.subTest(f"a descriptor class {cls.__doc__} " | ||
| f"(not a descriptor itself)"): | ||
| with self.subTest(f"a descriptor class (not a descriptor " | ||
| f"itself) {cls.__doc__} ", cls=cls): | ||
| self.assertFalse(inspect.isdatadescriptor(cls)) | ||
|
|
||
| def test_builtin_descriptors(self): | ||
|
|
@@ -2155,15 +2155,15 @@ def function(): | |
|
|
||
| # Example builtin method descriptors (non-data descriptors): | ||
|
|
||
| self.assertFalse( | ||
| inspect.isdatadescriptor(builtin_slot_wrapper), | ||
| 'a builtin slot wrapper is not a data descriptor') | ||
| self.assertFalse( | ||
| inspect.isdatadescriptor(Owner.__dict__['class_method']), | ||
| 'a classmethod object is not a data descriptor') | ||
| self.assertFalse( | ||
| inspect.isdatadescriptor(Owner.__dict__['static_method']), | ||
| 'a staticmethod object is not a data descriptor') | ||
| self.assertFalse( | ||
| inspect.isdatadescriptor(builtin_slot_wrapper), | ||
| 'a builtin slot wrapper is not a data descriptor') | ||
|
|
||
| # ...so also `types.MethodType`/`types.FunctionType` instances | ||
| # (not only that they are not data descriptors, but also | ||
|
|
||
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.
Method descriptors and data descriptors are mutually exclusive. For every case where
ismethoddescriptor()returns True we can test thatisdatadescriptor()returns False and vice verse. Of course, there are objects for which both predicates return False.Uh oh!
There was an error while loading. Please reload this page.
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.
Generally, both those functions –
ismethoddescriptor()andisdatadescriptor()– are tested in similar ways, but separately: each in a dedicated test class (respectively:TestIsMethodDescriptorandTestIsDataDescriptor).Do you suggest that those test classes should be merged into one class? That seems reasonable from the DRY point of view, and to emphasize the mutual exclusion you pointed to. Yet, on the other hand, I am afraid if that wouldn't result in tests whose essence would be harder to grasp (because of being less focused).
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.
PS Please note that the mutual exclusiveness in question is covered by each of these test classes.