Skip to content

Commit b5c653d

Browse files
improve docstring handling for catalog API docs
1 parent 5ebe0e0 commit b5c653d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/pygambit/catalog.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def __new__(cls, *args, **kwargs) -> gbt.Game:
3131

3232
@staticmethod
3333
def _game() -> gbt.Game:
34-
"""Override this method in subclasses to define the game."""
34+
"""
35+
Examples
36+
--------
37+
>>> {classname}() # Gets the game instance
38+
"""
3539
raise NotImplementedError("Subclasses must implement _game() method")
3640

3741
@classmethod
@@ -40,6 +44,14 @@ def _extract_description(cls, game: gbt.Game) -> None:
4044
cleaned_docstring = ""
4145
if cls.__doc__:
4246
cleaned_docstring = inspect.cleandoc(cls.__doc__)
47+
# If the class has a _game function, concatenate its docstring to the class docstring
48+
game_docstring = inspect.getdoc(cls._game)
49+
if game_docstring:
50+
game_docstring = inspect.cleandoc(game_docstring)
51+
# For games from file, sub in the class name
52+
game_docstring = game_docstring.replace("{classname}", cls.__name__)
53+
cleaned_docstring += "\n\n" + game_docstring
54+
cls.__doc__ = cleaned_docstring
4355
if len(cleaned_docstring) > 0:
4456
game.description = cleaned_docstring
4557

src/pygambit/catalog_games.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def _game(unique_NE_variant: bool = False):
2626
-------
2727
gbt.Game
2828
The constructed extensive-form game.
29+
30+
Examples
31+
--------
32+
>>> OneShotTrust._game(unique_NE_variant=False) # Constructs the standard game
33+
>>> OneShotTrust._game(unique_NE_variant=True) # Constructs the game with unique NE variant
2934
"""
3035
g = gbt.Game.new_tree(
3136
players=["Buyer", "Seller"], title="One-shot trust game, after Kreps (1990)"

tests/test_catalog.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def test_custom_game_subclass_extracts_metadata(self):
3939

4040
def test_can_get_game_description_from_docstring(self):
4141
"""CatalogGame should get description from docstring over game description."""
42-
assert ExampleGameWithDocstring().description == "Alternative test game description."
42+
assert (
43+
"Alternative test game description." in
44+
ExampleGameWithDocstring().description
45+
)
4346

4447
def test_catalog_py_game_with_parameters(self):
4548
"""
@@ -56,8 +59,8 @@ def test_catalog_yml_game_instantiation(self):
5659
def test_catalog_yml_game_description(self):
5760
"""Custom CatalogGame subclasses reading from catalog.yml should return Game instances."""
5861
assert (
62+
"A simple implementation of a two person Prisoner's Dilemma game." in
5963
gbt.catalog.PrisonersDilemma().description
60-
== "A simple implementation of a two person Prisoner's Dilemma game."
6164
)
6265

6366

0 commit comments

Comments
 (0)