-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Pydoc to list data descriptors in _fields order if it exists #69067
Comments
Currently, help() lists out data descriptors in alphabetical order. This is fine in the general case, however if the fields are parts of a named tuple, it is more sensible to list them in the order found in the tuple. The presence of a named tuple can be detected by the presence of a _fields attribute that is a list of strings. That strings can be used as a primary sort key before an alphabetical sort of anything not listed in _fields. >>> Person = namedtuple('Person', ['nickname', 'firstname', 'age'])
>>> help(Person)
Help on class Person in module __main__: class Person(builtins.tuple)
| Person(nickname, firstname, age)
|
...
|
| | Static methods defined here: The data descriptors should list nickname, then firstname, then age to match the tuple order in _fields. |
With the attached patch, the new output is: | ---------------------------------------------------------------------- |
Can this be enabled only for namedtuples? Otherwise this might be a backwards incompatible change, for example: class Foo:
'''spam'''
_fields = None |
Named tuples are not a type, they are a protocol. Guido has agreed that checking for _fields is an acceptable and preferred way of finding out whether something is a namedtuple. I can add a check to at least check the value of _fields is an iterable of strings. If it still aliases with some random use of _fields, the only consequence is that the matching field names will appear in a different order. |
They are, but for protocols we usually use dunder names. "_fields" is a common enough attribute name for all kinds of objects, not necessarily namedtuples. Can we at least check if the class is a tuple subclass and then use its '_fields' for sorting?
+1 for checking if it's an iterable of strings. |
No, protocols and duck typing do not always use dunder names. In fact checking for dunder names explicitly is probably the less common of the two cases. (We are talking about "protocol" here in a generic sense, not the restricted set of those that include dunder methods.) |
New changeset f5c40ab9e233 by Raymond Hettinger in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: