Skip to content

Commit 4694fb8

Browse files
authored
Add is_component_type utility (#104)
* feat: add is_component_type utility * fix typo * feat: add is_htmy_component_type()
1 parent e633a79 commit 4694fb8

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

htmy/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.10.1"
1+
__version__ = "0.10.2"
22

33
from .core import ContextAware as ContextAware
44
from .core import Formatter as Formatter
@@ -38,6 +38,8 @@
3838
from .utils import as_component_sequence as as_component_sequence
3939
from .utils import as_component_type as as_component_type
4040
from .utils import is_component_sequence as is_component_sequence
41+
from .utils import is_component_type as is_component_type
42+
from .utils import is_htmy_component_type as is_htmy_component_type
4143
from .utils import join
4244
from .utils import join_components as join_components
4345

htmy/utils.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

33
from collections.abc import Generator
4-
from typing import TYPE_CHECKING, TypeGuard
4+
from typing import TYPE_CHECKING, Any, TypeGuard
5+
6+
from htmy.typing import ComponentType
57

68
if TYPE_CHECKING:
7-
from .typing import Component, ComponentSequence, ComponentType
9+
from .typing import Component, ComponentSequence, ComponentType, HTMYComponentType
810

911

1012
def join_components(
@@ -44,13 +46,26 @@ def join(*items: str | None, separator: str = " ") -> str:
4446
return separator.join(i for i in items if i)
4547

4648

49+
def is_htmy_component_type(comp: Any) -> TypeGuard[HTMYComponentType]:
50+
"""Returns whether the given object is a `HTMYComponentType` (has a `htmy` method)."""
51+
# Lazy check, should be good enough.
52+
return hasattr(comp, "htmy")
53+
54+
55+
def is_component_type(comp: Any) -> TypeGuard[ComponentType]:
56+
"""Returns whether the given object is a `ComponentType`."""
57+
# Lazy check when it comes the htmy components, should be good enough.
58+
# No `is_htmy_component_type()` call to avoid trivial extra function call.
59+
return comp is None or isinstance(comp, str) or hasattr(comp, "htmy")
60+
61+
4762
def is_component_sequence(comp: Component) -> TypeGuard[ComponentSequence]:
48-
"""Returns whether the given component is a component sequence."""
63+
"""Returns whether the given component is a `ComponentSequence`."""
4964
return isinstance(comp, (list, tuple))
5065

5166

5267
def as_component_sequence(comp: Component) -> ComponentSequence:
53-
"""Returns the given component as a component sequence."""
68+
"""Returns the given component as a `ComponentSequence`."""
5469
# mypy doesn't understand the `is_component_sequence` type guard.
5570
return comp if is_component_sequence(comp) else (comp,) # type: ignore[return-value]
5671

0 commit comments

Comments
 (0)