From 529336e68d94b8319a17ab3de8e87ba5418c200b Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Sat, 4 May 2024 19:25:50 +0200 Subject: [PATCH] FIX: allow `Particle`+`State` in `as_markdown_table()` (#129) --- pyproject.toml | 3 +++ src/ampform_dpd/io.py | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e891caaa..afaff4e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/src/ampform_dpd/io.py b/src/ampform_dpd/io.py index ab705f32..038d0f4d 100644 --- a/src/ampform_dpd/io.py +++ b/src/ampform_dpd/io.py @@ -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]) + + >>> _determine_item_type([True, False]) + + >>> _determine_item_type([True, False, 1]) + + >>> _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