summaryrefslogtreecommitdiffstats
path: root/Lib/pydoc.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2015-08-19 05:25:16 (GMT)
committerRaymond Hettinger <python@rcn.com>2015-08-19 05:25:16 (GMT)
commit95801bbe4e96aeb706c8fcf9fb3698c39a72b847 (patch)
tree997f06d37a9893afaf862d012a110a19f706b675 /Lib/pydoc.py
parent15b87bfedcd40f601cb3c9caf75d2366c48e19fc (diff)
downloadcpython-95801bbe4e96aeb706c8fcf9fb3698c39a72b847.zip
cpython-95801bbe4e96aeb706c8fcf9fb3698c39a72b847.tar.gz
cpython-95801bbe4e96aeb706c8fcf9fb3698c39a72b847.tar.bz2
Issue #24879: Teach pydoc to display named tuple fields in the order they were defined.
Diffstat (limited to 'Lib/pydoc.py')
-rwxr-xr-xLib/pydoc.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index ee558bf..f4f2530 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -209,6 +209,18 @@ def classify_class_attrs(object):
results.append((name, kind, cls, value))
return results
+def sort_attributes(attrs, object):
+ 'Sort the attrs list in-place by _fields and then alphabetically by name'
+ # This allows data descriptors to be ordered according
+ # to a _fields attribute if present.
+ fields = getattr(object, '_fields', [])
+ try:
+ field_order = {name : i-len(fields) for (i, name) in enumerate(fields)}
+ except TypeError:
+ field_order = {}
+ keyfunc = lambda attr: (field_order.get(attr[0], 0), attr[0])
+ attrs.sort(key=keyfunc)
+
# ----------------------------------------------------- module manipulation
def ispackage(path):
@@ -867,8 +879,7 @@ class HTMLDoc(Doc):
object.__module__)
tag += ':<br>\n'
- # Sort attrs by name.
- attrs.sort(key=lambda t: t[0])
+ sort_attributes(attrs, object)
# Pump out the attrs, segregated by kind.
attrs = spill('Methods %s' % tag, attrs,
@@ -1286,8 +1297,8 @@ location listed above.
else:
tag = "inherited from %s" % classname(thisclass,
object.__module__)
- # Sort attrs by name.
- attrs.sort()
+
+ sort_attributes(attrs, object)
# Pump out the attrs, segregated by kind.
attrs = spill("Methods %s:\n" % tag, attrs,