Skip to content

Commit 6be2b5e

Browse files
TommyDew42Alex
authored and
Alex
committed
Relax type requirement on pc.cond (reflex-dev#323)
1 parent 2ce1ee7 commit 6be2b5e

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

pynecone/components/layout/cond.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Create a list of components from an iterable."""
22
from __future__ import annotations
33

4-
from typing import Optional
5-
6-
import pydantic
4+
from typing import Any, Optional
75

86
from pynecone.components.component import Component
97
from pynecone.components.layout.fragment import Fragment
@@ -15,7 +13,7 @@ class Cond(Component):
1513
"""Render one of two components based on a condition."""
1614

1715
# The cond to determine which component to render.
18-
cond: Var[bool]
16+
cond: Var[Any]
1917

2018
# The component to render if the cond is true.
2119
comp1: Component
@@ -26,19 +24,6 @@ class Cond(Component):
2624
# Whether the cond is within another cond.
2725
is_nested: bool = False
2826

29-
@pydantic.validator("cond")
30-
def validate_cond(cls, cond: Var) -> Var:
31-
"""Validate that the cond is a boolean.
32-
33-
Args:
34-
cond: The cond to validate.
35-
36-
Returns:
37-
The validated cond.
38-
"""
39-
assert issubclass(cond.type_, bool), "The var must be a boolean."
40-
return cond
41-
4227
@classmethod
4328
def create(
4429
cls, cond: Var, comp1: Component, comp2: Optional[Component] = None

pynecone/components/tags/cond_tag.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tag to conditionally render components."""
22

3+
from typing import Any
4+
35
from pynecone import utils
46
from pynecone.components.tags.tag import Tag
57
from pynecone.var import Var
@@ -9,7 +11,7 @@ class CondTag(Tag):
911
"""A conditional tag."""
1012

1113
# The condition to determine which component to render.
12-
cond: Var[bool]
14+
cond: Var[Any]
1315

1416
# The code to render if the condition is true.
1517
true_value: str

tests/components/layout/test_cond.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pytest
2+
3+
import pynecone as pc
4+
from pynecone.components.layout.cond import Cond
5+
from pynecone.components.typography.text import Text
6+
7+
8+
@pytest.fixture
9+
def cond_state(request):
10+
class CondState(pc.State):
11+
value: request.param["value_type"] = request.param["value"]
12+
13+
return CondState
14+
15+
16+
@pytest.mark.parametrize(
17+
"cond_state",
18+
[
19+
pytest.param({"value_type": bool, "value": True}),
20+
pytest.param({"value_type": int, "value": 0}),
21+
pytest.param({"value_type": str, "value": "true"}),
22+
],
23+
indirect=True,
24+
)
25+
def test_validate_cond(cond_state: pc.Var):
26+
"""Test if cond can be a pc.Val with any values
27+
28+
Args:
29+
cond_state: A fixture.
30+
"""
31+
cond_component = Cond.create(
32+
cond_state.value,
33+
Text.create("cond is True"),
34+
Text.create("cond is False"),
35+
)
36+
37+
assert str(cond_component) == (
38+
"{cond_state.value ? "
39+
"<Text>{`cond is True`}</Text> : "
40+
"<Text>{`cond is False`}</Text>}"
41+
)

0 commit comments

Comments
 (0)