Skip to content

help() on types has strange (if defined) notice for attributes that are defined #112266

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

Closed
sobolevn opened this issue Nov 19, 2023 · 5 comments
Closed
Assignees
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

sobolevn commented Nov 19, 2023

Feature or enhancement

Let's say we have a regular class and we call help() on it:

>>> class A: ...
... 
>>> help(A)
Help on class A in module __main__:

class A(builtins.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

This leaves a strange impression: what does it mean for __dict__?

dictionary for instance variables (if defined)

It is defined.

The same for regular __doc__:

>>> A.__dict__['__dict__'].__doc__
'dictionary for instance variables (if defined)'

Let's see what happens when __dict__ and __weakref__ are not defined:

>>> class B:
...    __slots__ = ()
... 
>>> help(B)
Help on class B in module __main__:

class B(builtins.object)

And:

>>> B.__dict__['__dict__']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    B.__dict__['__dict__']
    ~~~~~~~~~~^^^^^^^^^^^^
KeyError: '__dict__'

The historical reason behind it is: 373c741#diff-1decebeef15f4e0b0ce106c665751ec55068d4d1d1825847925ad4f528b5b872R1356-R1377

What do others think: should we remove (if defined) part?
If so, I have a PR ready.

Linked PRs

@sobolevn sobolevn added type-feature A feature request or enhancement docs Documentation in the Doc dir labels Nov 19, 2023
@sobolevn sobolevn self-assigned this Nov 19, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 19, 2023

What it's trying to say is that there might not be any instance variables defined, i.e., A().__dict__ might be empty if it has no instance variables

@chgnrdv
Copy link
Contributor

chgnrdv commented Nov 19, 2023

I guess that "if defined" refers not to __dict__ attribute itself but to variables it contains. Same is for __weakref__ and weak references. Maybe "if any defined" would sound better in this case?

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 19, 2023

I personally find the current text clear, but I'm a native English speaker, and it's true that the current text is very terse. If the current text isn't clear to non-native English speakers, then I agree we should change it to something that is.

I actually quite like @sobolevn's idea of just removing the "(if defined)". I'm not sure it adds anything; if it's unclear to some people, let's just get rid of it

@sobolevn
Copy link
Member Author

sobolevn commented Nov 19, 2023

Context: while working on #112143 I've spent like 20 minutes debugging help and __dict__ outputs to find out that this behaviour is expected. So, I got totally confused.

After the change:

>>> class A: ...
... 
>>> help(A)
Help on class A in module __main__:

class A(builtins.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object

sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 19, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 19, 2023
@terryjreedy
Copy link
Member

I agree with the change.

AlexWaygood pushed a commit that referenced this issue Nov 19, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 19, 2023
…akref__` docstrings (pythonGH-112268)

(cherry picked from commit f812914)

Co-authored-by: Nikita Sobolev <[email protected]>
AlexWaygood pushed a commit that referenced this issue Nov 19, 2023
…eakref__` docstrings (GH-112268) (#112270)

gh-112266: Remove `(if defined)` part from `__dict__` and `__weakref__` docstrings (GH-112268)
(cherry picked from commit f812914)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 20, 2023
AlexWaygood pushed a commit that referenced this issue Nov 20, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants