From 67df10cbb86225e1e3efc251325cbff883a1ef3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Thu, 5 Oct 2023 13:58:26 +0200 Subject: [PATCH] fix: Prevent alias resolution error when source-ordering members Issue griffe#213: https://github.com/mkdocstrings/griffe/issues/213 --- src/mkdocstrings_handlers/python/rendering.py | 2 ++ tests/test_rendering.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) 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