Repository navigation
pydoc renders from builtins.type note, even if it is incorrect #97959
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Oct 6, 2022 A couple of extra thoughts after reading some more source code:
https://github.com/python/cpython/blame/e39ae6bef2c357a88e232dcab2e4b4c0f367544b/Lib/pydoc.py#L1032-L1042if _is_bound_method(object): imclass = object.__self__.__class__ if cl: if imclass is not cl: note = ' from ' + classname(imclass, mod)
Ok, looks like this is in sync with the following example:
import pydoc from types import MethodType class My: some = MethodType(some, 1) print(pydoc.plain(pydoc.render_doc(My)))
which outputs:
Python Library Documentation: class My in module __main__ class My(builtins.object) | Methods defined here: | | some(*args, **kwargs) from builtins.intI think that
classmethods should just completely skip this part.
It brings more confusion than value. So, it should be:| __init_subclass__(*args, **kwargs) | This method is called when a class is subclassed. | | The default implementation does nothing. It may be | overridden to extend subclasses. | | custom()- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Oct 6, 2022 Good catch.
Why this "from ..." was added in the first case? In what cases it was useful.
I found only one: when
MethodTypeis defined in a class body:class Some: a = MethodType(some_func, some_instance)
Plus,
Enumusesfrom enum.EnumTypeon some methods. I am still testing other things.I found only one: when
MethodTypeis defined in a class body:But where is it used in the real code?
If make this code raising an error instead of adding "from ..." and run pydoc for all stdlib modules, what will fail? Investigate also the history of that code, commit message or issue text can explain its purpose.
Reacted by sobolevnSo, I got back to it. Here's what I found.
test results
Running
pydoctests withraise ValueErrorinstead ofnote = ' from ' + classname(imclass, mod)showed this:2 tests failed: test_enum test_pydocOnly
pydocitself andenumare affected.
Basically, the same modules I've changed in #98120history
There are two commits that touch this line:
- 15 years ago, when unbound methods were removed: ff73795
- 22 years ago, when b7a4830 was merged. It does not have a description. Its title is
Fix linking to classes (in class tree, and add links on unbound methods)
So, I can conclude that these lines have something to do with old unbound methods.
= MethodType()usageThere are some usage of it:
» ag '= MethodType' Lib/test/test_importlib/import_/test_caching.py 60: mock.load_module = MethodType(load_module, mock) Lib/test/test_call.py 754: meth = MethodType(func, args[0])But, lets think about whether is this important to show
from ...part. Compare these two (with and withoutfrom):>>> import types >>> class A: ... x = types.MethodType(some, 1) ... >>> help(A) Help on class A in module __main__: class A(builtins.object) | Methods defined here: | | x = some(*args, **kwargs) from builtins.int | | ---------------------------------------------------------------------- | Data descriptors defined here:
Without:
>>> help(A) Help on class A in module __main__: class A(builtins.object) | Methods defined here: | | x = some(*args, **kwargs) | | ---------------------------------------------------------------------- | Data descriptors defined here:
- Has anything really changed?
- Will users be able to decrypt that
xis aMethodTypebound tointfrom this short message? I don't think so. I've spent several hours trying to decode what it means and how it works.
lib usage
Let's take a look at the
enumcase: it kinda is using this feature.| ---------------------------------------------------------------------- | Methods inherited from enum.EnumType: | | __contains__(value) from enum.EnumType | Return True if `value` is in `cls`. | | `value` is in `cls` if: | 1) `value` is a member of `cls`, or | 2) `value` is the value of one of the `cls`'s members. | | __getitem__(name) from enum.EnumType | Return the member matching `name`. | | __iter__() from enum.EnumType | Return members in definition order. | | __len__() from enum.EnumType | Return the number of members (no aliases) | | ----------------------------------------------------------------------
Is it useful here? Again, I don't think so: we already have
Methods inherited from enum.EnumType:part which clearly says where these methods come from.In my opinion, it brings more confusion than value.
solution
I think that we should at least protect
@classmethodfrom been reported incorrectly, as I did in #98120Or we can completely remove
note = ' from ' + classname(imclass, mod)part, because:- It is an artifact from another era: when unbound methods were a thing
- It does not bring any value to existing
Lib/code - It is quite cryptic to understand for regular users
- It is easy to do, nothing will probably break because of that
@serhiy-storchaka what do you think? :)
I do not see any value in the
from some_module.some_type. I'd like to see it removed.Reacted by sobolevnNot sure if this is related, but
help(enum.Enum)shows:Help on class Enum in module enum: class Enum(builtins.object) | . . .It seems to me that instead of
builtins.objectthat should sayenum.EnumType.Reacted by sobolevn- added a commit that references this issue
on Jan 11, 2024 This issue is much much more complex than it looked at first glance. If you look at the code, you will see other issues with notes for routines. For example an "unbound method" note is never added to unbound methods. It does not work with methods of builtin classes. It seems that this code was written to handle classic classes, and the support of new-style classes was never complete. Some changes was made later to fix runtime errors, but it was not checked that the result is correct and that all cases are supported.
Other issue, is that class members that are bound methods were included in the methods section. But they behave rather like static methods, because they do not accept the self argument.
#113941 fixes these and other issues.
There are still unfixed issues, for example nested classes are not supported (#85799), and unbound methods of builtin classes are not visible as module members (#113942), but this PR is already large.
- added 5 commits that reference this issue
on Feb 11, 2024 About the
enum.Enumcase. It was:| Methods inherited from enum.EnumType: | | __contains__(value) from enum.EnumType | Return True if `value` is in `cls`.First, "from enum.EnumType" is redundant, because there is "from enum.EnumType" in the section title. Usually "from" notes are not repeated for every method.
Second, the section title is not completely correct. They are not normal methods. The first argument of normal arguments is
self(whatever its name). You cannot callenuminstance.__contains__(). You should callenumclass.__contains__(value)-- this is the behavior of static or class method. It is difficult to distinguish them at that level, so for now they are qualified as static methods.BTW, for the "from" example look at the enum.StrEnum help. Now it contains:
| Methods defined here: ... | __repr__(self) from enum.Enum | Return repr(self). | | __str__(self, /) from builtins.str | Return str(self).Note that
__str__is "from builtins.str", and__repr__is "from enum.Enum", despite both are "methods defined here". You can see that__str__is the same as for strings, and__repr__is implemented in Enum, and that they override methods inherited from base classes.
While working on #97958 I've noticed that there's something strange with
help()andclassmethods.Take a look at this example:
It prints:
Take a look at these two entries:
__init_subclass__(*args, **kwargs) from builtins.typecustom() from builtins.typeWhile
typehas__init_subclass__, there's notype.custom. But,helpsays that there is!I think that it is incorrect and can lead to confusion.
Instead it should be:
Linked PRs
help()does not show extra notes forclassmethods #98120