diff --git a/src/mkdocstrings_handlers/python/rendering.py b/src/mkdocstrings_handlers/python/rendering.py index ef4b5071..c54eb2cd 100644 --- a/src/mkdocstrings_handlers/python/rendering.py +++ b/src/mkdocstrings_handlers/python/rendering.py @@ -37,6 +37,8 @@ def _sort_key_alphabetical(item: CollectorItem) -> Any: def _sort_key_source(item: CollectorItem) -> Any: # if 'lineno' is none, the item will go to the start of the list. + if item.is_alias: + return item.alias_lineno if item.alias_lineno is not None else -1 return item.lineno if item.lineno is not None else -1 diff --git a/tests/test_rendering.py b/tests/test_rendering.py index b7b7af82..38d81dbb 100644 --- a/tests/test_rendering.py +++ b/tests/test_rendering.py @@ -156,10 +156,12 @@ def test_ordering_members(order: rendering.Order, members_list: list[str | None] """ class Obj: - def __init__(self, name: str, lineno: int | None = None) -> None: + def __init__(self, name: str, lineno: int | None = None, *, is_alias: bool = False) -> None: self.name = name self.lineno = lineno + self.alias_lineno = lineno + self.is_alias = is_alias - members = [Obj("a", 10), Obj("b", 9), Obj("c", 8)] + members = [Obj("a", 10, is_alias=True), Obj("b", 9, is_alias=False), Obj("c", 8, is_alias=True)] ordered = rendering.do_order_members(members, order, members_list) # type: ignore[arg-type] assert [obj.name for obj in ordered] == expected_names