@@ -407,15 +407,18 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
407
407
if not getattr (self .obj , "__test__" , True ):
408
408
return []
409
409
410
- # NB. we avoid random getattrs and peek in the __dict__ instead
411
- # (XXX originally introduced from a PyPy need, still true?)
410
+ # Avoid random getattrs and peek in the __dict__ instead.
412
411
dicts = [getattr (self .obj , "__dict__" , {})]
413
412
for basecls in self .obj .__class__ .__mro__ :
414
413
dicts .append (basecls .__dict__ )
414
+
415
+ # In each class, nodes should be definition ordered. Since Python 3.6,
416
+ # __dict__ is definition ordered.
415
417
seen : Set [str ] = set ()
416
- values : List [Union [nodes .Item , nodes .Collector ]] = []
418
+ dict_values : List [List [ Union [nodes .Item , nodes .Collector ] ]] = []
417
419
ihook = self .ihook
418
420
for dic in dicts :
421
+ values : List [Union [nodes .Item , nodes .Collector ]] = []
419
422
# Note: seems like the dict can change during iteration -
420
423
# be careful not to remove the list() without consideration.
421
424
for name , obj in list (dic .items ()):
@@ -433,13 +436,14 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]:
433
436
values .extend (res )
434
437
else :
435
438
values .append (res )
436
-
437
- def sort_key (item ):
438
- fspath , lineno , _ = item .reportinfo ()
439
- return (str (fspath ), lineno )
440
-
441
- values .sort (key = sort_key )
442
- return values
439
+ dict_values .append (values )
440
+
441
+ # Between classes in the class hierarchy, reverse-MRO order -- nodes
442
+ # inherited from base classes should come before subclasses.
443
+ result = []
444
+ for values in reversed (dict_values ):
445
+ result .extend (values )
446
+ return result
443
447
444
448
def _genfunctions (self , name : str , funcobj ) -> Iterator ["Function" ]:
445
449
modulecol = self .getparent (Module )
0 commit comments