Skip to content

Commit 0a50fe3

Browse files
conditional loading of NoneType
1 parent a15a6e9 commit 0a50fe3

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
"pyplot",
194194
"pytest",
195195
"qrules",
196+
"redef",
196197
"setuptools",
197198
"spflueger",
198199
"struct",

src/qrules/io/_dot.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import logging
99
import re
1010
import string
11+
import sys
1112
from collections import abc
1213
from fractions import Fraction
1314
from functools import singledispatch
1415
from inspect import isfunction
15-
from types import NoneType
1616
from typing import TYPE_CHECKING, Any, cast
1717

1818
import attrs
@@ -25,6 +25,10 @@
2525
from qrules.topology import FrozenTransition, MutableTransition, Topology, Transition
2626
from qrules.transition import ProblemSet, ReactionInfo, State
2727

28+
if sys.version_info >= (3, 10):
29+
from types import NoneType
30+
31+
2832
if TYPE_CHECKING:
2933
from collections.abc import Iterable
3034

@@ -299,19 +303,26 @@ def as_string(obj: Any) -> str:
299303
>>> as_string(10)
300304
'new int rendering'
301305
"""
302-
_LOGGER.warning(f"No DOT renderer implemented type {type(obj).__name__}")
306+
if obj is not None:
307+
_LOGGER.warning(f"No DOT renderer implemented type {type(obj).__name__}")
303308
return str(obj)
304309

305310

306-
@as_string.register(NoneType)
307-
@as_string.register(int)
311+
if sys.version_info >= (3, 10):
312+
313+
@as_string.register(NoneType)
314+
def _(obj: Any) -> str:
315+
return str(obj)
316+
317+
318+
@as_string.register(int) # type: ignore[no-redef]
308319
@as_string.register(float)
309320
@as_string.register(str)
310321
def _(obj: Any) -> str:
311322
return str(obj)
312323

313324

314-
@as_string.register(dict)
325+
@as_string.register(dict) # type: ignore[no-redef]
315326
def _(obj: dict) -> str:
316327
lines = []
317328
for key, value in obj.items():
@@ -334,7 +345,7 @@ def __render_key_and_value(key: str, value: Any) -> str:
334345
return as_string(value)
335346

336347

337-
@as_string.register(InteractionProperties)
348+
@as_string.register(InteractionProperties) # type: ignore[no-redef]
338349
def _(obj: InteractionProperties) -> str:
339350
lines = []
340351
if obj.l_magnitude is not None:
@@ -355,7 +366,7 @@ def _(obj: InteractionProperties) -> str:
355366
return "\n".join(lines)
356367

357368

358-
@as_string.register(EdgeSettings)
369+
@as_string.register(EdgeSettings) # type: ignore[no-redef]
359370
@as_string.register(NodeSettings)
360371
def _(settings: EdgeSettings | NodeSettings) -> str:
361372
output = ""
@@ -418,7 +429,7 @@ def __render_domain(domain: list[Any], key: str) -> str:
418429
return "[" + ", ".join(domain_str) + "]"
419430

420431

421-
@as_string.register(Particle)
432+
@as_string.register(Particle) # type: ignore[no-redef]
422433
def _(particle: Particle) -> str:
423434
return particle.name
424435

@@ -437,7 +448,7 @@ def _state_to_str(state: State) -> str:
437448
return f"{particle}[{spin_projection}]"
438449

439450

440-
@as_string.register(tuple)
451+
@as_string.register(tuple) # type: ignore[no-redef]
441452
def _(obj: tuple) -> str:
442453
if len(obj) == 2:
443454
if isinstance(obj[0], Particle) and isinstance(obj[1], (Fraction, float, int)):

0 commit comments

Comments
 (0)