Skip to content

Commit

Permalink
fix: Fix members ordering when members are specified with a boolean
Browse files Browse the repository at this point in the history
Issue #89: #89
  • Loading branch information
pawamoy committed Jul 20, 2023
1 parent b012371 commit c69f9c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def do_format_signature(
def do_order_members(
members: Sequence[Object | Alias],
order: Order,
members_list: list[str] | None,
members_list: bool | list[str] | None,
) -> Sequence[Object | Alias]:
"""Order members given an ordering method.
Expand All @@ -121,7 +121,7 @@ def do_order_members(
Returns:
The same members, ordered.
"""
if members_list:
if isinstance(members_list, list) and members_list:
sorted_members = []
members_dict = {member.name: member for member in members}
for name in members_list:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,35 @@ def main(self): ...
filtered = rendering.do_filter_objects(objects, members_list=members, inherited_members=inherited_members)
names = {obj.name for obj in filtered}
assert names == expected_names


@pytest.mark.parametrize(
("order", "members_list", "expected_names"),
[
(rendering.Order.alphabetical, None, ["a", "b", "c"]),
(rendering.Order.source, None, ["c", "b", "a"]),
(rendering.Order.alphabetical, ["c", "b"], ["c", "b"]),
(rendering.Order.source, ["a", "c"], ["a", "c"]),
(rendering.Order.alphabetical, [], ["a", "b", "c"]),
(rendering.Order.source, [], ["c", "b", "a"]),
(rendering.Order.alphabetical, True, ["a", "b", "c"]),
(rendering.Order.source, False, ["c", "b", "a"]),
],
)
def test_ordering_members(order: rendering.Order, members_list: list[str | None], expected_names: list[str]) -> None:
"""Assert the objects are correctly ordered.
Parameters:
order: The order to use (alphabetical or source).
members_list: The user specified members list.
expected_names: The expected ordered list of object names.
"""

class Obj:
def __init__(self, name: str, lineno: int | None = None) -> None:
self.name = name
self.lineno = lineno

members = [Obj("a", 10), Obj("b", 9), Obj("c", 8)]
ordered = rendering.do_order_members(members, order, members_list) # type: ignore[arg-type]
assert [obj.name for obj in ordered] == expected_names

0 comments on commit c69f9c3

Please sign in to comment.