streambricks 0.2.11__tar.gz → 0.2.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {streambricks-0.2.11 → streambricks-0.2.13}/PKG-INFO +1 -1
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/__init__.py +1 -1
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/main.py +40 -11
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/type_helpers.py +8 -1
- {streambricks-0.2.11 → streambricks-0.2.13}/.copier-answers.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.github/dependabot.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.gitignore +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/LICENSE +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/README.md +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/docs/.empty +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/duties.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/mkdocs.yml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/pyproject.toml +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/state.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/__init__.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/tests/__init__.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/tests/conftest.py +0 -0
- {streambricks-0.2.11 → streambricks-0.2.13}/tests/test_widgets.py +0 -0
|
@@ -6,10 +6,10 @@ from collections.abc import Callable, Sequence
|
|
|
6
6
|
from datetime import date, datetime, time
|
|
7
7
|
from decimal import Decimal
|
|
8
8
|
from enum import Enum
|
|
9
|
-
from typing import
|
|
9
|
+
from typing import Any, Literal, TypeVar, get_args, get_origin, overload
|
|
10
10
|
|
|
11
11
|
import fieldz
|
|
12
|
-
from pydantic import SecretStr
|
|
12
|
+
from pydantic import BaseModel, SecretStr
|
|
13
13
|
import streamlit as st
|
|
14
14
|
|
|
15
15
|
from streambricks.widgets.model_widget.field_metadata_renderers import (
|
|
@@ -38,12 +38,10 @@ from streambricks.widgets.type_helpers import (
|
|
|
38
38
|
is_sequence_type,
|
|
39
39
|
is_set_type,
|
|
40
40
|
is_union_type,
|
|
41
|
+
unpack_annotated,
|
|
41
42
|
)
|
|
42
43
|
|
|
43
44
|
|
|
44
|
-
if TYPE_CHECKING:
|
|
45
|
-
from pydantic import BaseModel
|
|
46
|
-
|
|
47
45
|
T = TypeVar("T")
|
|
48
46
|
WidgetFunc = Callable[..., T]
|
|
49
47
|
TForm = TypeVar("TForm", bound=BaseModel)
|
|
@@ -54,7 +52,6 @@ FIELD_METADATA_RENDERERS: dict[str, WidgetFunc[Any]] = {
|
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
|
|
57
|
-
# Mapping of Python types to render functions
|
|
58
55
|
PRIMITIVE_RENDERERS = {
|
|
59
56
|
str: render_str_field,
|
|
60
57
|
bool: render_bool_field,
|
|
@@ -469,7 +466,7 @@ def render_model_instance_field(
|
|
|
469
466
|
value: Any = None,
|
|
470
467
|
label: str | None = None,
|
|
471
468
|
disabled: bool = False,
|
|
472
|
-
help: str | None = None, # noqa: A002
|
|
469
|
+
help: str | None = None, # noqa: A002
|
|
473
470
|
**field_info: Any,
|
|
474
471
|
) -> Any:
|
|
475
472
|
"""Render a nested model instance field."""
|
|
@@ -478,8 +475,32 @@ def render_model_instance_field(
|
|
|
478
475
|
error_msg = f"Model class not provided for field {key}"
|
|
479
476
|
raise ValueError(error_msg)
|
|
480
477
|
|
|
481
|
-
#
|
|
482
|
-
if
|
|
478
|
+
# Handle Union types (including Python 3.10+ pipe syntax)
|
|
479
|
+
if is_union_type(model_class) or hasattr(model_class, "__or__"):
|
|
480
|
+
model_type_name = getattr(
|
|
481
|
+
model_class,
|
|
482
|
+
"__name__",
|
|
483
|
+
"Union[" + ", ".join(str(t) for t in get_args(model_class)) + "]",
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
# Initialize value if none
|
|
487
|
+
if value is None:
|
|
488
|
+
# For Union types, try to create an instance of the first model type
|
|
489
|
+
possible_types = get_args(model_class)
|
|
490
|
+
for possible_type in possible_types:
|
|
491
|
+
if is_dataclass_like(possible_type):
|
|
492
|
+
try:
|
|
493
|
+
value = try_create_default_instance(possible_type)
|
|
494
|
+
if value is not None:
|
|
495
|
+
break
|
|
496
|
+
except Exception: # noqa: BLE001
|
|
497
|
+
continue
|
|
498
|
+
|
|
499
|
+
if value is None:
|
|
500
|
+
st.error(f"Could not create instance of any type in {model_type_name}")
|
|
501
|
+
return None
|
|
502
|
+
# Initialize value if none for non-union types
|
|
503
|
+
elif value is None:
|
|
483
504
|
value = try_create_default_instance(model_class)
|
|
484
505
|
if value is None: # If creation failed
|
|
485
506
|
try:
|
|
@@ -496,7 +517,9 @@ def render_model_instance_field(
|
|
|
496
517
|
with st.expander("Edit", expanded=True):
|
|
497
518
|
updated_value = {}
|
|
498
519
|
try:
|
|
499
|
-
|
|
520
|
+
# Use the actual class of the value instance, not the field type
|
|
521
|
+
actual_class = value.__class__
|
|
522
|
+
for field in fieldz.fields(actual_class):
|
|
500
523
|
field_name = field.name
|
|
501
524
|
field_value = get_with_default(value, field_name, field)
|
|
502
525
|
field_help = get_description(field)
|
|
@@ -523,6 +546,10 @@ def render_model_instance_field(
|
|
|
523
546
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
524
547
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
525
548
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
549
|
+
if (ann := unpack_annotated(annotation)) != annotation:
|
|
550
|
+
annotation = ann
|
|
551
|
+
field_info["type"] = annotation
|
|
552
|
+
|
|
526
553
|
json_schema_extra = field_info.get("json_schema_extra", {})
|
|
527
554
|
if (
|
|
528
555
|
json_schema_extra
|
|
@@ -694,7 +721,9 @@ def render_model_field(model_class, field_name, value=None, container=st):
|
|
|
694
721
|
raise ValueError(error_msg)
|
|
695
722
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
696
723
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
697
|
-
|
|
724
|
+
dct = field.native_field.json_schema_extra or {} # type: ignore
|
|
725
|
+
field_info.update(dct)
|
|
726
|
+
field_info["json_schema_extra"] = dct
|
|
698
727
|
label = field_name.replace("_", " ").title()
|
|
699
728
|
help_text = get_description(field)
|
|
700
729
|
if help_text:
|
|
@@ -3,11 +3,18 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from collections.abc import Sequence
|
|
6
|
-
from typing import Any, Literal, get_args, get_origin
|
|
6
|
+
from typing import Annotated, Any, Literal, get_args, get_origin
|
|
7
7
|
|
|
8
8
|
import fieldz
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
def unpack_annotated(annotation: Any) -> Any:
|
|
12
|
+
"""Unpack an Annotated type annotation to its base type."""
|
|
13
|
+
if get_origin(annotation) is Annotated and (args := get_args(annotation)):
|
|
14
|
+
return args[0]
|
|
15
|
+
return annotation
|
|
16
|
+
|
|
17
|
+
|
|
11
18
|
def is_literal_type(annotation: Any) -> bool:
|
|
12
19
|
"""Check if a type annotation is a Literal type."""
|
|
13
20
|
# Check directly against the origin or special attribute
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{streambricks-0.2.11 → streambricks-0.2.13}/src/streambricks/widgets/model_widget/type_renderers.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|