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 — gh-122102: Improve tests for descriptor-related tools in `inspect` by zuo · Pull Request #122144 · python/cpython

Conversation

@zuo

@zuo zuo commented Jul 22, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Improve (and refactor) the tests of ismethoddescriptor() (test.test_inspect.test_inspect.TestIsMethodDescriptor) and isdatadescriptor() (test.test_inspect.test_inspect.TestIsDataDescriptor), making them more comprehensive and consistent.

  • Issue: Need for a few corrections/updates regarding the inspect.ismethoddescriptor()'s and inspect.isdatadescriptor()'s docs and unit tests #122102

Sorry, something went wrong.

Uh oh!

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

All reactions
@zuo
pythongh-122102: Improve tests of inspect.is{data,method}descriptor()
9944ef6
@bedevere-app bedevere-app Bot mentioned this pull request Jul 22, 2024
Need for a few corrections/updates regarding the inspect.ismethoddescriptor()'s and inspect.isdatadescriptor()'s docs and unit tests #122102
Open
@bedevere-app bedevere-app Bot added tests Tests in the Lib/test dir awaiting review labels Jul 22, 2024
@zuo zuo mentioned this pull request Jul 22, 2024
gh-122102: Fix/improve docs of descriptor-related tools in inspect #122104
Merged
picnixz
picnixz reviewed Jul 23, 2024
View reviewed changes
Comment thread Misc/NEWS.d/next/Tests/2024-07-22-01-32-00.gh-issue-122102.6sBzoH.rst Outdated
@@ -0,0 +1,2 @@
Improve the tests for :func:`inspect.ismethoddescriptor` and

@picnixz picnixz Jul 23, 2024

Copy link
Copy Markdown
Member

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 don't think we need a News entry but I may be wrong. Let's leave it like this for now.

Sorry, something went wrong.

Uh oh!

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

zuo and serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@zuo zuo Jul 25, 2024

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

Should I remove this file?

Sorry, something went wrong.

Uh oh!

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

All reactions

@picnixz picnixz Jul 27, 2024

Copy link
Copy Markdown
Member

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 don't know whether the thumb up by Serhiy was to tell me that the first sentence was correct or if it was for the second sentence...).

@serhiy-storchaka Should we keep it or remove it? (I'm in favor of removing it because I don't think people are very interested in that and it's only about coverage).

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Aug 3, 2024

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

OK, I remove it then.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Aug 9, 2024

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

@serhiy-storchaka Then probably the skip news label will be needed, won't it?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py
# Custom method descriptors (non-data descriptors):

class MethodDescriptor:
"""with __get__ and no __set__/__delete__"""

@picnixz picnixz Jul 23, 2024

Copy link
Copy Markdown
Member

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'd prefer havig those descriptions at the test level rather at the docstring level.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 23, 2024 •
edited
Loading

Uh oh!

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

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

Hm. I find attaching them directly to the classes they describe most direct and readable.

However, if you insist they should be listed in the for-loop's source collection, I'll move them.

Do you? :)

Sorry, something went wrong.

Uh oh!

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

All reactions

@picnixz picnixz Jul 23, 2024

Copy link
Copy Markdown
Member

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.

Well, it's generally easier when you read the code so I'll leave that decision to the one merging your PR (I don't have those rights). For now, you can keep them as is I think.

Sorry, something went wrong.

Uh oh!

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

zuo reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@serhiy-storchaka serhiy-storchaka Jul 24, 2024

Copy link
Copy Markdown
Member

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.

If they are not used in other tests, and there is no other reason for them to be global, I think that it is easy to read if all related classes are defined in close proximity to the testing code.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 25, 2024 •
edited
Loading

Uh oh!

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

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

So, should those classes and/or their short descriptions (currently being their docstrings) stay as-is, or be moved/reorganized somehow?

Sorry, something went wrong.

Uh oh!

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

All reactions

@picnixz picnixz Jul 27, 2024

Copy link
Copy Markdown
Member

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 personally don't like the descriptions at the docstring level because you need to go back and forth with the class whenever you see the actual test. Alternatively, you can just rename the class to include whatever is being done, namely:

  • MethodDescriptor -> MethodDescriptorWith_Get_NoSet_NoDelete.
  • MethodDescriptorWithGetNone -> MethodDescriptor_GetIsNone_NoSet_NoDelete.

Or, as I said before, do something like:

class MethodDescriptor:
	def __get__(self, *_): pass

for cls, desc in [
	(MethodDescriptor, "__get__ and no __set__/__delete__"),
    ...
]:
	with self.subTest(f'ismethoddescriptor: {desc}', cls=cls):
		...

I see that you also use some classes twice but the tests have a different check (one is on cls() and the other is on cls. Since you don't include the docstring in the second test, you don't need to repeat "__get__ and no __set__/__delete__" the second time as well.

I think it's clearer that way, because when you read the test, you likely forget about the docstrings after you've read them, whereas when you read the list of classes being tested, it's easy to see which description correponds to which class (in addition, with a meaningful name, it's easier to remember when you use them twice).

One thing that I would say: try to keep the diff small (so if you can reduce the number of changes, it's better) and try to be consistent with what has been done (the tests use explicit assertion messages, and not docstrings, so I think there was a reason to).

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Aug 3, 2024

Copy link
Copy Markdown
Contributor 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 have not hard feelings about this matter... @serhiy-storchaka What do you think?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

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

Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

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

@zuo
Update Lib/test/test_inspect/test_inspect.py
40ce793
(use `self.subTest()` in loops)
@zuo
zuo requested a review from picnixz July 23, 2024 15:01
picnixz
picnixz reviewed Jul 23, 2024
View reviewed changes
Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

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

@zuo
Update Lib/test/test_inspect/test_inspect.py
9e5c475
(further minor tweaks/improvements...)
@zuo
zuo requested a review from picnixz July 23, 2024 19:14
picnixz
picnixz approved these changes Jul 24, 2024
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

Except the docstrings on the classes, I think it's fine.

@serhiy-storchaka Do you want to take a look?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py Resolved

Uh oh!

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

Comment thread Lib/test/test_inspect/test_inspect.py Outdated
Comment on lines +1929 to +1930
attribute_slot = Slotermeyer.__dict__['a_slot']
frame_locals = types.FrameType.f_locals

@picnixz picnixz Jul 24, 2024

Copy link
Copy Markdown
Member

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 still think those should be closer to the test they are used in.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 25, 2024 •
edited
Loading

Uh oh!

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

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

That is the trade-off: to make, within this test method, all definitions/assignments of the descriptors used in the test be grouped in one place (and then only use them in consecutive assertions), or to place each of those definitions as close to the assertions they are used in as possible.

I prefer the former variant, but if you insist on the latter one, I'll move the definitions. One problem I see is that it is hard to do that consistently without splitting some of the definitions (in particular, moving a_property from Owner to a separate class...).; and if I do split them and do all necessary movements, then things may become unnecessarily "fragmented" (and, therefore, harder to read/comprehend).

Sorry, something went wrong.

Uh oh!

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

All reactions

@picnixz picnixz Jul 27, 2024

Copy link
Copy Markdown
Member

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 don't understand. attribute_slot is a variable, so you can move that variable where it's being used (line 1976), same for frame_locals. Don't move existing code, only move newest code.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Aug 3, 2024

Copy link
Copy Markdown
Contributor 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 problem with consistency, as perceived by me, is that if I move attribute_slot and frame_locals, I should also move e.g. a_lambda, and probably also the local def/class statements... But I find it a minor one, so I have no problem with adjusting the code according to your demand.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Aug 3, 2024 •
edited
Loading

Uh oh!

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

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

Hm... I propose, however, to get rid of the entire a_lambda variable – it's redundant, as the code without it does not seem less readable (rather quite contrary, actually...).

Sorry, something went wrong.

Uh oh!

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

All reactions

@picnixz picnixz Aug 4, 2024

Copy link
Copy Markdown
Member

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.

You are improving the tests. While consistency is important, readability is usually more for the tests. You declare a variable that is only used once and quite far from it. It's better to 1) not declare the variable 2) or declare it and use it just after. In general, we don't touch old code if there is no need to (e.g., there is no issue with it) but we can improve new code. I'm not sure I'll have time to review your changes today and won't be there for the next 10 days.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Sep 26, 2024

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

@picnixz For the record, according to your suggestion, the assignments to attribute_slot and frame_locals as well as (for consistency) some other stuff – have been moved, so that now they are as close as possible to the places when they are actually used.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Jul 24, 2024
serhiy-storchaka
serhiy-storchaka reviewed Jul 24, 2024
View reviewed changes
Comment thread Lib/test/test_inspect/test_inspect.py
# Custom method descriptors (non-data descriptors):

class MethodDescriptor:
"""with __get__ and no __set__/__delete__"""

@serhiy-storchaka serhiy-storchaka Jul 24, 2024

Copy link
Copy Markdown
Member

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.

If they are not used in other tests, and there is no other reason for them to be global, I think that it is easy to read if all related classes are defined in close proximity to the testing code.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py Outdated
Comment on lines +1913 to +1914
with self.subTest(f"a descriptor class (not a descriptor "
f"itself) {cls.__doc__} ", cls=cls):

@serhiy-storchaka serhiy-storchaka Jul 24, 2024

Copy link
Copy Markdown
Member

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.

Try to change assertFalse to assertTrue and look at the errors report. It may be only me, but I think that too long failure messages are difficult to read.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 25, 2024

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

Indeed, they become too verbose.

Now I've carefully prepared more concise messages. I hope you find them more readable.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py
MethodDescriptorSub,
]:
with self.subTest(f"a descriptor {cls.__doc__}", cls=cls):
self.assertTrue(inspect.ismethoddescriptor(cls()))

@serhiy-storchaka serhiy-storchaka Jul 24, 2024

Copy link
Copy Markdown
Member

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 that isdatadescriptor() returns False and vice verse. Of course, there are objects for which both predicates return False.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 25, 2024 •
edited
Loading

Uh oh!

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

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

Generally, both those functions – ismethoddescriptor() and isdatadescriptor() – are tested in similar ways, but separately: each in a dedicated test class (respectively: TestIsMethodDescriptor and TestIsDataDescriptor).

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).

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jul 18, 2026

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

PS Please note that the mutual exclusiveness in question is covered by each of these test classes.

Sorry, something went wrong.

Uh oh!

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

All reactions
zuo and others added 2 commits July 24, 2024 13:49
@zuo @picnixz
Update Lib/test/test_inspect/test_inspect.py
ca12cc2
Co-authored-by: Bénédikt Tran <[email protected]>
@zuo
Update Lib/test/test_inspect/test_inspect.py
ebcb0dc
(further rearrangements/improvements...)
@zuo
zuo requested review from picnixz and serhiy-storchaka July 25, 2024 19:34
zuo added 4 commits August 3, 2024 19:50
@zuo
Remove Misc/NEWS.d/next/Tests/2024-07-22-01-32-00.gh-issue-122102.6sB…
9dab208
…zoH.rst
@zuo
Update Lib/test/test_inspect/test_inspect.py
fc33fd5
(further cosmetics...)
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
130d795
…ptor
@zuo
Include also functools.partial-related cases
ec79a04
@zuo

zuo commented Aug 3, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka @picnixz
The latest commit adjusts the stuff to the changes from gh-122087 (namely, from the PR gh-122219 – i.e., the Python-3.14-dedicated part of the gh-122087's changes).

All reactions

Sorry, something went wrong.

Uh oh!

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

zuo added 3 commits August 5, 2024 12:45
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
8e2ea46
…ptor
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
bcec47a
…ptor
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
b249448
…ptor
picnixz
picnixz reviewed Dec 24, 2024
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

Am I correct that there are actually duplicated cases of base classes for method and data descriptors? if so, those classes could be either global (and named "Descriptor" without the Data/Method prefix).

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_inspect/test_inspect.py

class DataDescriptorWithSetOnly:
"""with __set__ (and no __get__)"""
def __set__(self, *_): pass

@picnixz picnixz Dec 24, 2024

Copy link
Copy Markdown
Member

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.

Now that I look at it again, I'm really considering to generate the classes using type() but I'm not sure it's better.

Sorry, something went wrong.

Uh oh!

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

All reactions

@zuo zuo Jan 27, 2025

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

Why? I don't think it is.

Sorry, something went wrong.

Uh oh!

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

All reactions
@github-actions

github-actions Bot commented Apr 17, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 17, 2026
zuo added 2 commits July 18, 2026 13:23
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
17928e4
…ptor
@zuo
Merge branch 'python:main' into test-inspect-ismethoddescriptor-isdat…
288b883
…adescriptor
@zuo

zuo commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

And nor for something completely different: let's try to complete also this :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@zware zware added skip news and removed stale Stale PR or inactive for long period of time. labels Jul 18, 2026
@github-actions

github-actions Bot commented Oct 25, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 25, 2026
@zuo
Merge branch 'main' into test-inspect-ismethoddescriptor-isdatadescri…
7f3f315
…ptor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz approved these changes
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review skip news stale Stale PR or inactive for long period of time. tests Tests in the Lib/test dir

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.

4 participants

@zuo @serhiy-storchaka @picnixz @zware
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 01402503 — 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.