Skip to content

Commit 2e8cedb

Browse files
authored
Remove member_order attributes (#13962)
1 parent c58caef commit 2e8cedb

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

sphinx/ext/autodoc/_documenters.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ class Documenter:
7979
#: name by which the directive is called (auto...) and the default
8080
#: generated directive name
8181
objtype: ClassVar = 'object'
82-
#: order if autodoc_member_order is set to 'groupwise'
83-
member_order: ClassVar = 0
8482
#: true if the generated content may contain titles
8583
titles_allowed: ClassVar = True
8684

@@ -174,14 +172,6 @@ def _load_object_by_name(self) -> Literal[True] | None:
174172
self.parent = parent
175173
self.object_name = props.object_name
176174
self.object = props._obj
177-
if self.objtype == 'method':
178-
if 'staticmethod' in props.properties: # type: ignore[attr-defined]
179-
# document static members before regular methods
180-
self.member_order -= 1 # type: ignore[misc]
181-
elif 'classmethod' in props.properties: # type: ignore[attr-defined]
182-
# document class methods before static methods as
183-
# they usually behave as alternative constructors
184-
self.member_order -= 2 # type: ignore[misc]
185175
self._load_object_has_been_called = True
186176
return True
187177

@@ -497,7 +487,7 @@ def sort_members(
497487
"""Sort the given member list."""
498488
if order == 'groupwise':
499489
# sort by group; alphabetically within groups
500-
documenters.sort(key=lambda e: (e[0].member_order, e[0].name))
490+
documenters.sort(key=lambda e: (e[0].props._groupwise_order_key, e[0].name))
501491
elif order == 'bysource':
502492
if (
503493
isinstance(self, ModuleDocumenter)
@@ -780,7 +770,6 @@ class FunctionDocumenter(Documenter):
780770
props: _FunctionDefProperties
781771

782772
objtype = 'function'
783-
member_order = 30
784773

785774

786775
class DecoratorDocumenter(FunctionDocumenter):
@@ -797,7 +786,6 @@ class ClassDocumenter(Documenter):
797786
props: _ClassDefProperties
798787

799788
objtype = 'class'
800-
member_order = 20
801789
option_spec: ClassVar[OptionSpec] = {
802790
'members': members_option,
803791
'undoc-members': bool_option,
@@ -836,7 +824,6 @@ class ExceptionDocumenter(ClassDocumenter):
836824
props: _ClassDefProperties
837825

838826
objtype = 'exception'
839-
member_order = 10
840827

841828

842829
class DataDocumenter(Documenter):
@@ -847,7 +834,6 @@ class DataDocumenter(Documenter):
847834
__uninitialized_global_variable__ = True
848835

849836
objtype = 'data'
850-
member_order = 40
851837
option_spec: ClassVar[OptionSpec] = dict(Documenter.option_spec)
852838
option_spec['annotation'] = annotation_option
853839
option_spec['no-value'] = bool_option
@@ -860,7 +846,6 @@ class MethodDocumenter(Documenter):
860846

861847
objtype = 'method'
862848
directivetype = 'method'
863-
member_order = 50
864849

865850

866851
class AttributeDocumenter(Documenter):
@@ -869,7 +854,6 @@ class AttributeDocumenter(Documenter):
869854
props: _AssignStatementProperties
870855

871856
objtype = 'attribute'
872-
member_order = 60
873857
option_spec: ClassVar[OptionSpec] = dict(Documenter.option_spec)
874858
option_spec['annotation'] = annotation_option
875859
option_spec['no-value'] = bool_option
@@ -887,7 +871,6 @@ class PropertyDocumenter(Documenter):
887871
props: _FunctionDefProperties
888872

889873
objtype = 'property'
890-
member_order = 60
891874

892875

893876
class TypeAliasDocumenter(Documenter):
@@ -896,7 +879,6 @@ class TypeAliasDocumenter(Documenter):
896879
props: _TypeStatementProperties
897880

898881
objtype = 'type'
899-
member_order = 70
900882
option_spec: ClassVar[OptionSpec] = {
901883
'no-index': bool_option,
902884
'no-index-entry': bool_option,

sphinx/ext/autodoc/_property_types.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def parent_names(self) -> tuple[str, ...]:
7979
def dotted_parts(self) -> str:
8080
return '.'.join(self.parts)
8181

82+
@property
83+
def _groupwise_order_key(self) -> int:
84+
return 0
85+
8286

8387
@dataclasses.dataclass(frozen=False, kw_only=True, slots=True)
8488
class _ModuleProperties(_ItemProperties):
@@ -139,6 +143,10 @@ def canonical_full_name(self) -> str | None:
139143
return None
140144
return f'{modname}.{qualname}'
141145

146+
@property
147+
def _groupwise_order_key(self) -> int:
148+
return 10 if self.obj_type == 'exception' else 20
149+
142150

143151
@dataclasses.dataclass(frozen=False, kw_only=True, slots=True)
144152
class _FunctionDefProperties(_ItemProperties):
@@ -174,6 +182,21 @@ def is_final(self) -> bool:
174182
def is_staticmethod(self) -> bool:
175183
return 'staticmethod' in self.properties
176184

185+
@property
186+
def _groupwise_order_key(self) -> int:
187+
if self.obj_type == 'method':
188+
if self.is_classmethod:
189+
# document class methods before static methods as
190+
# they usually behave as alternative constructors
191+
return 48
192+
if self.is_staticmethod:
193+
# document static members before regular methods
194+
return 49
195+
return 50
196+
if self.obj_type == 'property':
197+
return 60
198+
return 30
199+
177200

178201
@dataclasses.dataclass(frozen=False, kw_only=True, slots=True)
179202
class _AssignStatementProperties(_ItemProperties):
@@ -194,6 +217,10 @@ class _AssignStatementProperties(_ItemProperties):
194217
_obj_repr_rst: str
195218
_obj_type_annotation: str | None
196219

220+
@property
221+
def _groupwise_order_key(self) -> int:
222+
return 40 if self.obj_type == 'data' else 60
223+
197224

198225
@dataclasses.dataclass(frozen=False, kw_only=True, slots=True)
199226
class _TypeStatementProperties(_ItemProperties):
@@ -202,3 +229,7 @@ class _TypeStatementProperties(_ItemProperties):
202229
_obj___name__: str | None
203230
_obj___qualname__: str | None
204231
_obj___value__: str # The aliased annotation
232+
233+
@property
234+
def _groupwise_order_key(self) -> int:
235+
return 70

0 commit comments

Comments
 (0)