Skip to content

Commit

Permalink
FIX: allow Particle+State in as_markdown_table() (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed May 4, 2024
1 parent 9f1d1e9 commit 529336e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ addopts = [
"--ignore=docs/conf.py",
"-m not slow",
]
doctest_optionflags = [
"IGNORE_EXCEPTION_DETAIL",
]
filterwarnings = [
"error",
"ignore:the imp module is deprecated in favour of importlib.*:DeprecationWarning",
Expand Down
25 changes: 23 additions & 2 deletions src/ampform_dpd/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,34 @@ def as_markdown_table(obj: Sequence) -> str:


def _determine_item_type(obj) -> type:
"""Determine the type of the items in a sequence.
>>> _determine_item_type([1, 2, 3])
<class 'int'>
>>> _determine_item_type([True, False])
<class 'bool'>
>>> _determine_item_type([True, False, 1])
<class 'int'>
>>> _determine_item_type([3.14, 1 + 1j])
Traceback (most recent call last):
...
ValueError: Not all items are of type float'
"""
if not isinstance(obj, abc.Sequence):
return type(obj)
if len(obj) < 1:
msg = "Need at least one entry to render a table"
raise ValueError(msg)
item_type = type(obj[0])
if not all(isinstance(i, item_type) for i in obj):
existing_types = {type(i) for i in obj}
existing_types = {
typ
for typ in existing_types
if not any(
typ is not other and issubclass(typ, other) for other in existing_types
)
}
item_type = next(iter(existing_types))
if len(existing_types) != 1:
msg = f"Not all items are of type {item_type.__name__}"
raise ValueError(msg)
return item_type
Expand Down

0 comments on commit 529336e

Please sign in to comment.