streambricks 0.2.12__tar.gz → 0.2.14__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.12 → streambricks-0.2.14}/PKG-INFO +1 -1
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/__init__.py +1 -1
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/main.py +157 -48
- streambricks-0.2.14/src/streambricks/widgets/type_helpers.py +392 -0
- streambricks-0.2.12/src/streambricks/widgets/type_helpers.py +0 -235
- {streambricks-0.2.12 → streambricks-0.2.14}/.copier-answers.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.github/dependabot.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.gitignore +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/LICENSE +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/README.md +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/docs/.empty +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/duties.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/mkdocs.yml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/pyproject.toml +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/state.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/__init__.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/tests/__init__.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/tests/conftest.py +0 -0
- {streambricks-0.2.12 → streambricks-0.2.14}/tests/test_widgets.py +0 -0
|
@@ -31,13 +31,16 @@ from streambricks.widgets.type_helpers import (
|
|
|
31
31
|
add_new_item,
|
|
32
32
|
create_default_instance,
|
|
33
33
|
get_description,
|
|
34
|
+
get_field,
|
|
34
35
|
get_inner_type,
|
|
36
|
+
get_union_type_options,
|
|
35
37
|
get_with_default,
|
|
36
38
|
is_dataclass_like,
|
|
37
39
|
is_literal_type,
|
|
38
40
|
is_sequence_type,
|
|
39
41
|
is_set_type,
|
|
40
42
|
is_union_type,
|
|
43
|
+
unpack_annotated,
|
|
41
44
|
)
|
|
42
45
|
|
|
43
46
|
|
|
@@ -77,24 +80,55 @@ def render_union_field( # noqa: PLR0911
|
|
|
77
80
|
) -> Any:
|
|
78
81
|
"""Render a field that can accept multiple types."""
|
|
79
82
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
80
|
-
|
|
83
|
+
|
|
84
|
+
type_options = get_union_type_options(annotation) # list of (type, description)
|
|
85
|
+
|
|
86
|
+
default_index = 0
|
|
87
|
+
if value is not None:
|
|
88
|
+
for i, (t, _) in enumerate(type_options):
|
|
89
|
+
if (is_literal_type(t) and value in get_args(t)) or isinstance(value, t):
|
|
90
|
+
default_index = i
|
|
91
|
+
break
|
|
92
|
+
|
|
93
|
+
type_descriptions = [desc for _, desc in type_options]
|
|
81
94
|
type_key = f"{key}_type"
|
|
82
|
-
type_names = [
|
|
83
|
-
t.__name__ if hasattr(t, "__name__") else str(t) for t in possible_types
|
|
84
|
-
]
|
|
85
95
|
selected_type_name = st.selectbox(
|
|
86
96
|
f"Type for {label or key}",
|
|
87
|
-
options=
|
|
97
|
+
options=type_descriptions,
|
|
98
|
+
index=default_index,
|
|
88
99
|
key=type_key,
|
|
89
100
|
disabled=disabled,
|
|
90
101
|
help=help,
|
|
91
102
|
)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
selected_type = possible_types[selected_type_index]
|
|
103
|
+
selected_type_index = type_descriptions.index(selected_type_name)
|
|
104
|
+
selected_type, _ = type_options[selected_type_index]
|
|
95
105
|
field_key = f"{key}_value"
|
|
106
|
+
|
|
107
|
+
# Special case for literal types
|
|
108
|
+
if is_literal_type(selected_type):
|
|
109
|
+
literal_values = get_args(selected_type)
|
|
110
|
+
value_index = 0
|
|
111
|
+
if value in literal_values:
|
|
112
|
+
value_index = literal_values.index(value)
|
|
113
|
+
|
|
114
|
+
return st.selectbox(
|
|
115
|
+
f"Value ({selected_type_name})",
|
|
116
|
+
options=literal_values,
|
|
117
|
+
index=value_index,
|
|
118
|
+
key=field_key,
|
|
119
|
+
disabled=disabled,
|
|
120
|
+
help=help,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# Special case for None type
|
|
124
|
+
if selected_type is type(None):
|
|
125
|
+
st.info("None selected")
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
# For other types, use standard field renderer
|
|
96
129
|
modified_field_info = field_info.copy()
|
|
97
130
|
modified_field_info["type"] = selected_type
|
|
131
|
+
|
|
98
132
|
typed_value: Any = None
|
|
99
133
|
if value is not None:
|
|
100
134
|
try:
|
|
@@ -132,7 +166,7 @@ def render_union_field( # noqa: PLR0911
|
|
|
132
166
|
if selected_type is bool and not isinstance(result, bool):
|
|
133
167
|
return bool(result)
|
|
134
168
|
except (ValueError, TypeError) as e:
|
|
135
|
-
error_msg = f"Cannot convert {result} to {
|
|
169
|
+
error_msg = f"Cannot convert {result} to {selected_type_name}: {e!s}"
|
|
136
170
|
st.error(error_msg)
|
|
137
171
|
if selected_type is int:
|
|
138
172
|
return 0
|
|
@@ -459,13 +493,13 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
459
493
|
return optional_wrapper
|
|
460
494
|
|
|
461
495
|
|
|
462
|
-
def render_model_instance_field(
|
|
496
|
+
def render_model_instance_field( # noqa: PLR0911
|
|
463
497
|
*,
|
|
464
498
|
key: str,
|
|
465
499
|
value: Any = None,
|
|
466
500
|
label: str | None = None,
|
|
467
501
|
disabled: bool = False,
|
|
468
|
-
help: str | None = None, # noqa: A002
|
|
502
|
+
help: str | None = None, # noqa: A002
|
|
469
503
|
**field_info: Any,
|
|
470
504
|
) -> Any:
|
|
471
505
|
"""Render a nested model instance field."""
|
|
@@ -473,26 +507,57 @@ def render_model_instance_field(
|
|
|
473
507
|
if model_class is None:
|
|
474
508
|
error_msg = f"Model class not provided for field {key}"
|
|
475
509
|
raise ValueError(error_msg)
|
|
510
|
+
if is_union_type(model_class):
|
|
511
|
+
if value is None:
|
|
512
|
+
# For Union types, try each possible model type
|
|
513
|
+
possible_types = get_args(model_class)
|
|
514
|
+
for possible_type in possible_types:
|
|
515
|
+
if is_dataclass_like(possible_type):
|
|
516
|
+
try:
|
|
517
|
+
# Try to create a minimal valid instance
|
|
518
|
+
value = create_default_instance(possible_type)
|
|
519
|
+
break
|
|
520
|
+
except Exception as e: # noqa: BLE001
|
|
521
|
+
st.warning(f"Could not create {possible_type.__name__}: {e}")
|
|
522
|
+
continue
|
|
476
523
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
524
|
+
if value is None:
|
|
525
|
+
for possible_type in possible_types:
|
|
526
|
+
if not is_dataclass_like(possible_type):
|
|
527
|
+
if possible_type is type(None):
|
|
528
|
+
return None
|
|
529
|
+
if is_literal_type(possible_type):
|
|
530
|
+
return get_args(possible_type)[
|
|
531
|
+
0
|
|
532
|
+
] # Return first literal value
|
|
533
|
+
|
|
534
|
+
st.error(
|
|
535
|
+
f"Could not create instance of any type in {model_class.__name__}"
|
|
536
|
+
)
|
|
486
537
|
return None
|
|
538
|
+
# Initialize value if none for non-union types
|
|
539
|
+
elif value is None:
|
|
540
|
+
try:
|
|
541
|
+
# Try to create a minimal valid instance
|
|
542
|
+
value = create_default_instance(model_class)
|
|
543
|
+
except Exception as e: # noqa: BLE001
|
|
544
|
+
st.error(f"Failed to create {model_class.__name__}: {e}")
|
|
545
|
+
return None
|
|
487
546
|
|
|
488
547
|
st.markdown(f"**{label or key}**")
|
|
489
548
|
if help:
|
|
490
549
|
st.caption(help)
|
|
491
550
|
|
|
492
551
|
with st.expander("Edit", expanded=True):
|
|
552
|
+
if value is None:
|
|
553
|
+
st.error("Cannot render model: creation failed")
|
|
554
|
+
return None
|
|
555
|
+
|
|
493
556
|
updated_value = {}
|
|
494
557
|
try:
|
|
495
|
-
|
|
558
|
+
# Use the actual class of the value instance, not the field type
|
|
559
|
+
actual_class = value.__class__
|
|
560
|
+
for field in fieldz.fields(actual_class, parse_annotated=True):
|
|
496
561
|
field_name = field.name
|
|
497
562
|
field_value = get_with_default(value, field_name, field)
|
|
498
563
|
field_help = get_description(field)
|
|
@@ -519,6 +584,10 @@ def render_model_instance_field(
|
|
|
519
584
|
def get_field_renderer(field_info: dict[str, Any]) -> WidgetFunc[Any]: # noqa: PLR0911
|
|
520
585
|
"""Get the appropriate renderer for a field based on its type and constraints."""
|
|
521
586
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
587
|
+
if (ann := unpack_annotated(annotation)) != annotation:
|
|
588
|
+
annotation = ann
|
|
589
|
+
field_info["type"] = annotation
|
|
590
|
+
|
|
522
591
|
json_schema_extra = field_info.get("json_schema_extra", {})
|
|
523
592
|
if (
|
|
524
593
|
json_schema_extra
|
|
@@ -584,7 +653,7 @@ def render_model_readonly[T](
|
|
|
584
653
|
|
|
585
654
|
with st.container():
|
|
586
655
|
# Get all fields from the model
|
|
587
|
-
for field in fieldz.fields(model_class):
|
|
656
|
+
for field in fieldz.fields(model_class, parse_annotated=True):
|
|
588
657
|
field_name = field.name
|
|
589
658
|
if field_name in excluded_fields:
|
|
590
659
|
continue
|
|
@@ -672,7 +741,7 @@ def display_model_readonly(value, key=None):
|
|
|
672
741
|
for field in fieldz.fields(model_class):
|
|
673
742
|
field_name = field.name
|
|
674
743
|
field_value = getattr(value, field_name, None)
|
|
675
|
-
if field_value ==
|
|
744
|
+
if field_value == fieldz.Field.MISSING:
|
|
676
745
|
field_value = get_with_default(value, field_name, field)
|
|
677
746
|
sub_key = f"{key}_{field_name}" if key else field_name
|
|
678
747
|
cols = st.columns([0.3, 0.7])
|
|
@@ -684,13 +753,12 @@ def display_model_readonly(value, key=None):
|
|
|
684
753
|
|
|
685
754
|
def render_model_field(model_class, field_name, value=None, container=st):
|
|
686
755
|
"""Render a field from a model using a compact layout."""
|
|
687
|
-
field =
|
|
688
|
-
if field is None:
|
|
689
|
-
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
690
|
-
raise ValueError(error_msg)
|
|
756
|
+
field = get_field(model_class, field_name)
|
|
691
757
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
692
758
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
693
|
-
|
|
759
|
+
dct = field.native_field.json_schema_extra or {} # type: ignore
|
|
760
|
+
field_info.update(dct)
|
|
761
|
+
field_info["json_schema_extra"] = dct
|
|
694
762
|
label = field_name.replace("_", " ").title()
|
|
695
763
|
help_text = get_description(field)
|
|
696
764
|
if help_text:
|
|
@@ -739,7 +807,6 @@ def render_model_form(
|
|
|
739
807
|
result = {}
|
|
740
808
|
field_groups: dict[str, Any] = {}
|
|
741
809
|
for field in fieldz.fields(model_class):
|
|
742
|
-
# Check if field has a category defined
|
|
743
810
|
category = "General"
|
|
744
811
|
if "category" in field.metadata:
|
|
745
812
|
category = field.metadata["category"]
|
|
@@ -756,34 +823,61 @@ def render_model_form(
|
|
|
756
823
|
for i, (_group_name, fields) in enumerate(field_groups.items()):
|
|
757
824
|
with tabs[i]:
|
|
758
825
|
for field in fields:
|
|
759
|
-
|
|
760
|
-
if field_name in excluded_fields:
|
|
826
|
+
if field.name in excluded_fields:
|
|
761
827
|
continue
|
|
762
|
-
current_value = get_with_default(instance,
|
|
763
|
-
result[
|
|
764
|
-
model_class,
|
|
828
|
+
current_value = get_with_default(instance, field.name, field)
|
|
829
|
+
result[field.name] = render_model_field(
|
|
830
|
+
model_class, field.name, current_value
|
|
765
831
|
)
|
|
766
832
|
else:
|
|
767
833
|
# Single category, render fields directly
|
|
768
|
-
for field in fieldz.fields(model_class):
|
|
769
|
-
|
|
770
|
-
if field_name in excluded_fields:
|
|
834
|
+
for field in fieldz.fields(model_class, parse_annotated=True):
|
|
835
|
+
if field.name in excluded_fields:
|
|
771
836
|
continue
|
|
772
|
-
current_value = get_with_default(instance,
|
|
773
|
-
result[
|
|
774
|
-
model_class,
|
|
837
|
+
current_value = get_with_default(instance, field.name, field)
|
|
838
|
+
result[field.name] = render_model_field(
|
|
839
|
+
model_class, field.name, current_value
|
|
775
840
|
)
|
|
776
841
|
|
|
777
842
|
return fieldz.replace(instance, **result)
|
|
778
843
|
|
|
779
844
|
|
|
780
845
|
if __name__ == "__main__":
|
|
781
|
-
from typing import Literal
|
|
846
|
+
from typing import Annotated, Literal
|
|
782
847
|
|
|
783
|
-
|
|
848
|
+
import pydantic
|
|
784
849
|
|
|
785
850
|
from streambricks.helpers import run
|
|
786
851
|
|
|
852
|
+
# Example models to simulate chunker configs
|
|
853
|
+
class LlamaIndexChunkerConfig(BaseModel):
|
|
854
|
+
"""LlamaIndex chunker configuration."""
|
|
855
|
+
|
|
856
|
+
type: Literal["llamaindex"] = "llamaindex"
|
|
857
|
+
chunk_size: int = 1024
|
|
858
|
+
chunk_overlap: int = 20
|
|
859
|
+
|
|
860
|
+
class MarkdownChunkerConfig(BaseModel):
|
|
861
|
+
"""Markdown chunker configuration."""
|
|
862
|
+
|
|
863
|
+
type: Literal["markdown"] = "markdown"
|
|
864
|
+
split_level: int = 1
|
|
865
|
+
|
|
866
|
+
class AiChunkerConfig(BaseModel):
|
|
867
|
+
"""AI-based chunker configuration."""
|
|
868
|
+
|
|
869
|
+
type: Literal["ai"] = "ai"
|
|
870
|
+
max_chunk_size: int = 2000
|
|
871
|
+
|
|
872
|
+
# Create an Annotated union type similar to ChunkerConfig
|
|
873
|
+
ChunkerConfig = Annotated[
|
|
874
|
+
LlamaIndexChunkerConfig | MarkdownChunkerConfig | AiChunkerConfig,
|
|
875
|
+
pydantic.Field(discriminator="type"),
|
|
876
|
+
]
|
|
877
|
+
|
|
878
|
+
# Create a literal type similar to ChunkerShorthand
|
|
879
|
+
ChunkerShorthand = Literal["markdown", "llamaindex", "ai"]
|
|
880
|
+
|
|
787
881
|
class SubModel(BaseModel):
|
|
788
882
|
"""Test submodel."""
|
|
789
883
|
|
|
@@ -792,36 +886,51 @@ if __name__ == "__main__":
|
|
|
792
886
|
active: bool = True
|
|
793
887
|
|
|
794
888
|
class TestModel(BaseModel):
|
|
795
|
-
"""Test model."""
|
|
889
|
+
"""Test model with complex union types."""
|
|
890
|
+
|
|
891
|
+
# This field simulates the problematic chunker field
|
|
892
|
+
chunker: ChunkerConfig | ChunkerShorthand | None = None
|
|
893
|
+
"""Optional chunker configuration. If None, processes entire document at once."""
|
|
796
894
|
|
|
797
895
|
status: int | str | bool = 2
|
|
798
896
|
"""A field that can be either int, str, or bool."""
|
|
897
|
+
|
|
799
898
|
boolean: bool = True
|
|
800
899
|
"""Optional text field."""
|
|
900
|
+
|
|
801
901
|
long_text: str | None = "test " * 40
|
|
802
902
|
"""Long text."""
|
|
803
903
|
|
|
804
904
|
optional_int: int | None = None
|
|
805
905
|
"""An optional integer that can be None."""
|
|
906
|
+
|
|
806
907
|
optional_string: str | None = None
|
|
807
908
|
"""An optional string that can be None."""
|
|
909
|
+
|
|
808
910
|
optional_model: SubModel | None = None
|
|
809
911
|
"""An optional nested model that can be None."""
|
|
810
912
|
|
|
811
|
-
tags: list[str] = Field(default_factory=list)
|
|
913
|
+
tags: list[str] = pydantic.Field(default_factory=list)
|
|
812
914
|
"""A list of string tags."""
|
|
813
|
-
|
|
915
|
+
|
|
916
|
+
numbers: list[int | float] = pydantic.Field(default_factory=list)
|
|
814
917
|
"""A list of numbers (int or float)"""
|
|
815
|
-
|
|
918
|
+
|
|
919
|
+
settings: list[SubModel] = pydantic.Field(default_factory=list)
|
|
816
920
|
"""A list of nested models"""
|
|
817
|
-
|
|
921
|
+
|
|
922
|
+
priorities: list[Literal["Low", "Medium", "High"]] = pydantic.Field(
|
|
923
|
+
default_factory=list
|
|
924
|
+
)
|
|
818
925
|
"""A list of priority levels."""
|
|
819
926
|
|
|
820
927
|
def demo():
|
|
821
928
|
st.title("Pydantic Form Demo")
|
|
822
929
|
if "model" not in st.session_state:
|
|
823
930
|
st.session_state.model = TestModel(status=2)
|
|
824
|
-
|
|
931
|
+
|
|
932
|
+
st.session_state.model = render_model_form(st.session_state.model)
|
|
933
|
+
|
|
825
934
|
with st.expander("Current Model State", expanded=True):
|
|
826
935
|
st.json(st.session_state.model.model_dump_json(indent=2))
|
|
827
936
|
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"""Primitive type handlers for Pydantic form fields."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Annotated, Any, Literal, get_args, get_origin
|
|
7
|
+
|
|
8
|
+
import fieldz
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# For reference, these are the fieldz classes:
|
|
12
|
+
#
|
|
13
|
+
# @dataclasses.dataclass(**DC_KWARGS)
|
|
14
|
+
# class Constraints:
|
|
15
|
+
# gt: int | float | None = None
|
|
16
|
+
# ge: int | float | None = None
|
|
17
|
+
# lt: int | float | None = None
|
|
18
|
+
# le: int | float | None = None
|
|
19
|
+
# multiple_of: int | float | None = None
|
|
20
|
+
# min_length: int | None = None # for str
|
|
21
|
+
# max_length: int | None = None # for str
|
|
22
|
+
# max_digits: int | None = None # for decimal
|
|
23
|
+
# decimal_places: int | None = None # for decimal
|
|
24
|
+
# pattern: str | None = None
|
|
25
|
+
# deprecated: bool | None = None
|
|
26
|
+
# tz: bool | None = None
|
|
27
|
+
# predicate: Callable[[Any], bool] | None = None
|
|
28
|
+
# # enum: list[Any] | None = None
|
|
29
|
+
# # const: Any | None = None
|
|
30
|
+
#
|
|
31
|
+
# def __rich_repr__(self) -> Iterable[tuple[str, Any]]:
|
|
32
|
+
# for name, val in dataclasses.asdict(self).items():
|
|
33
|
+
# if val is not None:
|
|
34
|
+
# yield name, val
|
|
35
|
+
#
|
|
36
|
+
#
|
|
37
|
+
# @dataclasses.dataclass(**DC_KWARGS)
|
|
38
|
+
# class Field(Generic[_T]):
|
|
39
|
+
# MISSING: ClassVar[Literal[_MISSING_TYPE.MISSING]] = _MISSING_TYPE.MISSING
|
|
40
|
+
#
|
|
41
|
+
# name: str,
|
|
42
|
+
# type
|
|
43
|
+
# description: str | None = None
|
|
44
|
+
# title: str | None = None
|
|
45
|
+
# default: _T | Literal[_MISSING_TYPE.MISSING] = MISSING
|
|
46
|
+
# default_factory: Callable[[], _T] | Literal[_MISSING_TYPE.MISSING] = MISSING
|
|
47
|
+
# repr: bool = True
|
|
48
|
+
# hash: bool | None = None
|
|
49
|
+
# init: bool = True
|
|
50
|
+
# compare: bool = True
|
|
51
|
+
# metadata: Mapping[Any, Any] = dataclasses.field(default_factory=dict)
|
|
52
|
+
# kw_only: bool = False
|
|
53
|
+
# # extra
|
|
54
|
+
# frozen: bool = False
|
|
55
|
+
# native_field: Any | None = dataclasses.field(
|
|
56
|
+
# default=None, compare=False, repr=False
|
|
57
|
+
# )
|
|
58
|
+
# constraints: Constraints | None = None
|
|
59
|
+
|
|
60
|
+
# # populated during parse_annotated
|
|
61
|
+
# annotated_type: builtins.type[_T] | None = dataclasses.field(
|
|
62
|
+
# default=None, repr=False, compare=False
|
|
63
|
+
# )
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def unpack_annotated(annotation: Any) -> Any:
|
|
67
|
+
"""Unpack an Annotated type annotation to its base type."""
|
|
68
|
+
if get_origin(annotation) is Annotated and (args := get_args(annotation)):
|
|
69
|
+
return args[0]
|
|
70
|
+
return annotation
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def is_literal_type(annotation: Any) -> bool:
|
|
74
|
+
"""Check if a type annotation is a Literal type."""
|
|
75
|
+
# Check directly against the origin or special attribute
|
|
76
|
+
return (
|
|
77
|
+
get_origin(annotation) is Literal
|
|
78
|
+
or getattr(annotation, "__origin__", None) is Literal
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def is_union_type(annotation: Any) -> bool:
|
|
83
|
+
import typing
|
|
84
|
+
|
|
85
|
+
origin = get_origin(annotation)
|
|
86
|
+
# Direct identity check for typing.Union
|
|
87
|
+
if origin is typing.Union:
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
# Check for Python 3.10+ pipe syntax union
|
|
91
|
+
if hasattr(annotation, "__class__") and annotation.__class__.__name__ == "UnionType":
|
|
92
|
+
return True
|
|
93
|
+
|
|
94
|
+
# For Python 3.10+ unions detected via get_origin
|
|
95
|
+
return bool(
|
|
96
|
+
origin is not None
|
|
97
|
+
and hasattr(origin, "__class__")
|
|
98
|
+
and origin.__class__.__name__ == "UnionType"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def is_optional_type(annotation: Any) -> bool:
|
|
103
|
+
"""Check if a type annotation is an Optional type (T | None)."""
|
|
104
|
+
if not is_union_type(annotation):
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
args = get_args(annotation)
|
|
108
|
+
return type(None) in args
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def is_set_type(annotation: Any) -> bool:
|
|
112
|
+
"""Check if the annotation represents a set type."""
|
|
113
|
+
if annotation is set:
|
|
114
|
+
return True
|
|
115
|
+
origin = get_origin(annotation)
|
|
116
|
+
return origin is set
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def is_sequence_type(annotation: Any) -> bool:
|
|
120
|
+
"""Check if an annotation represents a sequence type (except sets)."""
|
|
121
|
+
origin = get_origin(annotation)
|
|
122
|
+
if origin in (list, tuple, Sequence):
|
|
123
|
+
return True
|
|
124
|
+
|
|
125
|
+
return annotation in (list, tuple, Sequence)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def get_with_default(obj: Any, field_name: str, field_info: fieldz.Field) -> Any: # noqa: PLR0911
|
|
129
|
+
"""Get field value with appropriate default if it's missing."""
|
|
130
|
+
# Get the raw value
|
|
131
|
+
value = getattr(obj, field_name, None)
|
|
132
|
+
|
|
133
|
+
# If value isn't MISSING, return it as is
|
|
134
|
+
if value != fieldz.Field.MISSING:
|
|
135
|
+
return value
|
|
136
|
+
|
|
137
|
+
# If we don't have field info, get it
|
|
138
|
+
if field_info is None:
|
|
139
|
+
for field in fieldz.fields(obj.__class__):
|
|
140
|
+
if field.name == field_name:
|
|
141
|
+
field_info = field
|
|
142
|
+
break
|
|
143
|
+
|
|
144
|
+
# If we have field info, use it to determine appropriate default
|
|
145
|
+
if field_info is not None:
|
|
146
|
+
field_type = field_info.type
|
|
147
|
+
|
|
148
|
+
# Handle Union types
|
|
149
|
+
if is_union_type(field_type):
|
|
150
|
+
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
151
|
+
if int in types:
|
|
152
|
+
return 0
|
|
153
|
+
if float in types:
|
|
154
|
+
return 0.0
|
|
155
|
+
if str in types:
|
|
156
|
+
return ""
|
|
157
|
+
if bool in types:
|
|
158
|
+
return False
|
|
159
|
+
if types and isinstance(types[0], type):
|
|
160
|
+
if issubclass(types[0], int):
|
|
161
|
+
return 0
|
|
162
|
+
if issubclass(types[0], float):
|
|
163
|
+
return 0.0
|
|
164
|
+
if issubclass(types[0], str):
|
|
165
|
+
return ""
|
|
166
|
+
if issubclass(types[0], bool):
|
|
167
|
+
return False
|
|
168
|
+
|
|
169
|
+
# Handle basic types
|
|
170
|
+
if isinstance(field_type, type):
|
|
171
|
+
if issubclass(field_type, int):
|
|
172
|
+
return 0
|
|
173
|
+
if issubclass(field_type, float):
|
|
174
|
+
return 0.0
|
|
175
|
+
if issubclass(field_type, str):
|
|
176
|
+
return ""
|
|
177
|
+
if issubclass(field_type, bool):
|
|
178
|
+
return False
|
|
179
|
+
if (
|
|
180
|
+
issubclass(field_type, list)
|
|
181
|
+
or issubclass(field_type, set)
|
|
182
|
+
or issubclass(field_type, tuple)
|
|
183
|
+
):
|
|
184
|
+
return []
|
|
185
|
+
|
|
186
|
+
# Default fallback for unknown types
|
|
187
|
+
return None
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def is_dataclass_like(annotation: Any) -> bool:
|
|
191
|
+
"""Check if a type is a dataclass-like object (Pydantic model, attrs, etc.)."""
|
|
192
|
+
if not isinstance(annotation, type):
|
|
193
|
+
return False
|
|
194
|
+
try:
|
|
195
|
+
fields = fieldz.fields(annotation)
|
|
196
|
+
# If we get fields, it's a dataclass-like object
|
|
197
|
+
return len(fields) > 0
|
|
198
|
+
except Exception: # noqa: BLE001
|
|
199
|
+
# If fieldz can't handle it, it's not a dataclass-like object
|
|
200
|
+
return False
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def create_default_instance(model_class: type) -> Any:
|
|
204
|
+
"""Create a default instance of a model with default values for required fields."""
|
|
205
|
+
# Create an empty dict to collect required values
|
|
206
|
+
default_values = {}
|
|
207
|
+
|
|
208
|
+
# Get all fields
|
|
209
|
+
for field in fieldz.fields(model_class):
|
|
210
|
+
field_name = field.name
|
|
211
|
+
|
|
212
|
+
# Check if the field already has a default value
|
|
213
|
+
has_default = False
|
|
214
|
+
if field.default != fieldz.Field.MISSING:
|
|
215
|
+
default_values[field_name] = field.default
|
|
216
|
+
has_default = True
|
|
217
|
+
elif field.default_factory != fieldz.Field.MISSING:
|
|
218
|
+
try:
|
|
219
|
+
default_values[field_name] = field.default_factory() # pyright: ignore
|
|
220
|
+
has_default = True
|
|
221
|
+
except Exception: # noqa: BLE001
|
|
222
|
+
# If default_factory fails, fall back to type-based defaults
|
|
223
|
+
pass
|
|
224
|
+
|
|
225
|
+
if not has_default:
|
|
226
|
+
default_values[field_name] = get_default_value(field.type)
|
|
227
|
+
|
|
228
|
+
return model_class(**default_values)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def get_default_value(field_type: Any): # noqa: PLR0911
|
|
232
|
+
if is_union_type(field_type):
|
|
233
|
+
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
234
|
+
if int in types:
|
|
235
|
+
return 0
|
|
236
|
+
if float in types:
|
|
237
|
+
return 0.0
|
|
238
|
+
if str in types:
|
|
239
|
+
return ""
|
|
240
|
+
if bool in types:
|
|
241
|
+
return False
|
|
242
|
+
if isinstance(field_type, type):
|
|
243
|
+
if issubclass(field_type, str):
|
|
244
|
+
return ""
|
|
245
|
+
if issubclass(field_type, int):
|
|
246
|
+
return 0
|
|
247
|
+
if issubclass(field_type, float):
|
|
248
|
+
return 0.0
|
|
249
|
+
if issubclass(field_type, bool):
|
|
250
|
+
return False
|
|
251
|
+
if is_dataclass_like(field_type):
|
|
252
|
+
return create_default_instance(field_type)
|
|
253
|
+
return None # or field_type()?
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def get_description(field: fieldz.Field) -> str | None:
|
|
257
|
+
if "description" in field.metadata:
|
|
258
|
+
return field.metadata["description"]
|
|
259
|
+
if hasattr(field.native_field, "description"):
|
|
260
|
+
return field.native_field.description # type: ignore
|
|
261
|
+
return None
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def add_new_item(items_list: list, item_type: Any) -> None:
|
|
265
|
+
"""Add a new item to a list based on its type."""
|
|
266
|
+
if is_dataclass_like(item_type):
|
|
267
|
+
new_item = create_default_instance(item_type)
|
|
268
|
+
if new_item is not None:
|
|
269
|
+
items_list.append(new_item)
|
|
270
|
+
# For basic types, add appropriate default values
|
|
271
|
+
elif item_type is str:
|
|
272
|
+
items_list.append("")
|
|
273
|
+
elif item_type is int:
|
|
274
|
+
items_list.append(0)
|
|
275
|
+
elif item_type is float:
|
|
276
|
+
items_list.append(0.0)
|
|
277
|
+
elif item_type is bool:
|
|
278
|
+
items_list.append(False)
|
|
279
|
+
elif is_union_type(item_type):
|
|
280
|
+
# For union types, use the first non-None type
|
|
281
|
+
types = [t for t in get_args(item_type) if t is not type(None)]
|
|
282
|
+
if int in types:
|
|
283
|
+
items_list.append(0)
|
|
284
|
+
elif float in types:
|
|
285
|
+
items_list.append(0.0)
|
|
286
|
+
elif str in types:
|
|
287
|
+
items_list.append("")
|
|
288
|
+
elif bool in types:
|
|
289
|
+
items_list.append(False)
|
|
290
|
+
else:
|
|
291
|
+
items_list.append(None)
|
|
292
|
+
else:
|
|
293
|
+
# For unknown types, add None
|
|
294
|
+
items_list.append(None)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def get_inner_type(field_info: Any) -> Any:
|
|
298
|
+
annotation = field_info.get("type") or field_info.get("annotation")
|
|
299
|
+
try:
|
|
300
|
+
return get_args(annotation)[0] # Get type of set items
|
|
301
|
+
except (IndexError, TypeError):
|
|
302
|
+
return Any
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def get_field(model_class, field_name: str) -> fieldz.Field:
|
|
306
|
+
field = next(
|
|
307
|
+
(f for f in fieldz.fields(model_class) if f.name == field_name),
|
|
308
|
+
None,
|
|
309
|
+
)
|
|
310
|
+
if field is None:
|
|
311
|
+
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
312
|
+
raise ValueError(error_msg)
|
|
313
|
+
return field
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def get_union_type_options(annotation: Any) -> list[tuple[Any, str]]:
|
|
317
|
+
"""Analyze a union type and extract individual types with their descriptions.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
annotation: Type annotation to analyze
|
|
321
|
+
|
|
322
|
+
Returns:
|
|
323
|
+
List of tuples containing (resolved_type, type_description)
|
|
324
|
+
"""
|
|
325
|
+
type_options = []
|
|
326
|
+
|
|
327
|
+
# Process each union member
|
|
328
|
+
for arg in get_args(annotation):
|
|
329
|
+
# Handle Annotated type that contains a union
|
|
330
|
+
if get_origin(arg) is Annotated:
|
|
331
|
+
annotated_args = get_args(arg)
|
|
332
|
+
if not annotated_args:
|
|
333
|
+
continue
|
|
334
|
+
|
|
335
|
+
# Get the inner type (first arg of Annotated)
|
|
336
|
+
inner = annotated_args[0]
|
|
337
|
+
|
|
338
|
+
# Handle pipe-syntax union (Python 3.10+)
|
|
339
|
+
if inner.__class__.__name__ == "UnionType":
|
|
340
|
+
# Extract each union member - use __args__ attribute for pipe unions
|
|
341
|
+
for union_member in inner.__args__:
|
|
342
|
+
description = (
|
|
343
|
+
union_member.__name__
|
|
344
|
+
if hasattr(union_member, "__name__")
|
|
345
|
+
else str(union_member)
|
|
346
|
+
)
|
|
347
|
+
type_options.append((union_member, description))
|
|
348
|
+
elif is_union_type(inner):
|
|
349
|
+
# Handle traditional Union types
|
|
350
|
+
for union_member in get_args(inner):
|
|
351
|
+
description = (
|
|
352
|
+
union_member.__name__
|
|
353
|
+
if hasattr(union_member, "__name__")
|
|
354
|
+
else str(union_member)
|
|
355
|
+
)
|
|
356
|
+
type_options.append((union_member, description))
|
|
357
|
+
else:
|
|
358
|
+
# Single type in Annotated
|
|
359
|
+
description = inner.__name__ if hasattr(inner, "__name__") else str(inner)
|
|
360
|
+
type_options.append((inner, description))
|
|
361
|
+
|
|
362
|
+
# Handle Literal types
|
|
363
|
+
elif is_literal_type(arg):
|
|
364
|
+
values = ", ".join(
|
|
365
|
+
f'"{v}"' if isinstance(v, str) else str(v) for v in get_args(arg)
|
|
366
|
+
)
|
|
367
|
+
description = f"Literal[{values}]"
|
|
368
|
+
type_options.append((arg, description))
|
|
369
|
+
|
|
370
|
+
# Handle None type
|
|
371
|
+
elif arg is type(None):
|
|
372
|
+
type_options.append((arg, "None"))
|
|
373
|
+
|
|
374
|
+
# Handle regular types
|
|
375
|
+
else:
|
|
376
|
+
description = arg.__name__ if hasattr(arg, "__name__") else str(arg)
|
|
377
|
+
type_options.append((arg, description))
|
|
378
|
+
|
|
379
|
+
return type_options
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
if __name__ == "__main__":
|
|
383
|
+
from pydantic import BaseModel
|
|
384
|
+
|
|
385
|
+
class SubModel(BaseModel):
|
|
386
|
+
"""Test submodel."""
|
|
387
|
+
|
|
388
|
+
name: str
|
|
389
|
+
value: int | float
|
|
390
|
+
active: bool = True
|
|
391
|
+
|
|
392
|
+
print(create_default_instance(SubModel))
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
"""Primitive type handlers for Pydantic form fields."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from collections.abc import Sequence
|
|
6
|
-
from typing import Any, Literal, get_args, get_origin
|
|
7
|
-
|
|
8
|
-
import fieldz
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def is_literal_type(annotation: Any) -> bool:
|
|
12
|
-
"""Check if a type annotation is a Literal type."""
|
|
13
|
-
# Check directly against the origin or special attribute
|
|
14
|
-
return (
|
|
15
|
-
get_origin(annotation) is Literal
|
|
16
|
-
or getattr(annotation, "__origin__", None) is Literal
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def is_union_type(annotation: Any) -> bool:
|
|
21
|
-
"""Check if a type annotation is a Union type."""
|
|
22
|
-
origin = get_origin(annotation)
|
|
23
|
-
# Check if it's Union or Optional (which is Union[T, None])
|
|
24
|
-
return origin is not None and (
|
|
25
|
-
origin.__name__ == "Union" if hasattr(origin, "__name__") else False
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def is_optional_type(annotation: Any) -> bool:
|
|
30
|
-
"""Check if a type annotation is an Optional type (T | None)."""
|
|
31
|
-
if not is_union_type(annotation):
|
|
32
|
-
return False
|
|
33
|
-
|
|
34
|
-
args = get_args(annotation)
|
|
35
|
-
return type(None) in args
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def is_set_type(annotation: Any) -> bool:
|
|
39
|
-
"""Check if the annotation represents a set type."""
|
|
40
|
-
if annotation is set:
|
|
41
|
-
return True
|
|
42
|
-
origin = get_origin(annotation)
|
|
43
|
-
return origin is set
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def is_sequence_type(annotation: Any) -> bool:
|
|
47
|
-
"""Check if an annotation represents a sequence type (except sets)."""
|
|
48
|
-
origin = get_origin(annotation)
|
|
49
|
-
if origin in (list, tuple, Sequence):
|
|
50
|
-
return True
|
|
51
|
-
|
|
52
|
-
return annotation in (list, tuple, Sequence)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def get_with_default(obj: Any, field_name: str, field_info: Any = None) -> Any: # noqa: PLR0911
|
|
56
|
-
"""Get field value with appropriate default if it's missing."""
|
|
57
|
-
# Get the raw value
|
|
58
|
-
value = getattr(obj, field_name, None)
|
|
59
|
-
|
|
60
|
-
# If value isn't MISSING, return it as is
|
|
61
|
-
if value != "MISSING":
|
|
62
|
-
return value
|
|
63
|
-
|
|
64
|
-
# If we don't have field info, get it
|
|
65
|
-
if field_info is None:
|
|
66
|
-
for field in fieldz.fields(obj.__class__):
|
|
67
|
-
if field.name == field_name:
|
|
68
|
-
field_info = field
|
|
69
|
-
break
|
|
70
|
-
|
|
71
|
-
# If we have field info, use it to determine appropriate default
|
|
72
|
-
if field_info is not None:
|
|
73
|
-
field_type = field_info.type
|
|
74
|
-
|
|
75
|
-
# Handle Union types
|
|
76
|
-
if is_union_type(field_type):
|
|
77
|
-
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
78
|
-
if int in types:
|
|
79
|
-
return 0
|
|
80
|
-
if float in types:
|
|
81
|
-
return 0.0
|
|
82
|
-
if str in types:
|
|
83
|
-
return ""
|
|
84
|
-
if bool in types:
|
|
85
|
-
return False
|
|
86
|
-
if types and isinstance(types[0], type):
|
|
87
|
-
if issubclass(types[0], int):
|
|
88
|
-
return 0
|
|
89
|
-
if issubclass(types[0], float):
|
|
90
|
-
return 0.0
|
|
91
|
-
if issubclass(types[0], str):
|
|
92
|
-
return ""
|
|
93
|
-
if issubclass(types[0], bool):
|
|
94
|
-
return False
|
|
95
|
-
|
|
96
|
-
# Handle basic types
|
|
97
|
-
if isinstance(field_type, type):
|
|
98
|
-
if issubclass(field_type, int):
|
|
99
|
-
return 0
|
|
100
|
-
if issubclass(field_type, float):
|
|
101
|
-
return 0.0
|
|
102
|
-
if issubclass(field_type, str):
|
|
103
|
-
return ""
|
|
104
|
-
if issubclass(field_type, bool):
|
|
105
|
-
return False
|
|
106
|
-
if (
|
|
107
|
-
issubclass(field_type, list)
|
|
108
|
-
or issubclass(field_type, set)
|
|
109
|
-
or issubclass(field_type, tuple)
|
|
110
|
-
):
|
|
111
|
-
return []
|
|
112
|
-
|
|
113
|
-
# Default fallback for unknown types
|
|
114
|
-
return None
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
def is_dataclass_like(annotation: Any) -> bool:
|
|
118
|
-
"""Check if a type is a dataclass-like object (Pydantic model, attrs, etc.)."""
|
|
119
|
-
if not isinstance(annotation, type):
|
|
120
|
-
return False
|
|
121
|
-
try:
|
|
122
|
-
fields = fieldz.fields(annotation)
|
|
123
|
-
# If we get fields, it's a dataclass-like object
|
|
124
|
-
return len(fields) > 0
|
|
125
|
-
except Exception: # noqa: BLE001
|
|
126
|
-
# If fieldz can't handle it, it's not a dataclass-like object
|
|
127
|
-
return False
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
def create_default_instance(model_class: type) -> Any:
|
|
131
|
-
"""Create a default instance of a model with default values for required fields."""
|
|
132
|
-
# Create an empty dict to collect required values
|
|
133
|
-
default_values = {}
|
|
134
|
-
|
|
135
|
-
# Get all fields
|
|
136
|
-
for field in fieldz.fields(model_class):
|
|
137
|
-
field_name = field.name
|
|
138
|
-
|
|
139
|
-
# Check if the field already has a default value
|
|
140
|
-
has_default = False
|
|
141
|
-
if hasattr(field, "default") and field.default != "MISSING":
|
|
142
|
-
# Use the field's default value
|
|
143
|
-
default_values[field_name] = field.default
|
|
144
|
-
has_default = True
|
|
145
|
-
elif hasattr(field, "default_factory") and field.default_factory != "MISSING":
|
|
146
|
-
try:
|
|
147
|
-
# Use the field's default factory
|
|
148
|
-
default_values[field_name] = field.default_factory() # pyright: ignore
|
|
149
|
-
has_default = True
|
|
150
|
-
except Exception: # noqa: BLE001
|
|
151
|
-
# If default_factory fails, fall back to type-based defaults
|
|
152
|
-
pass
|
|
153
|
-
|
|
154
|
-
# If the field doesn't have a default, create one based on type
|
|
155
|
-
if not has_default:
|
|
156
|
-
field_type = field.type
|
|
157
|
-
|
|
158
|
-
# Handle union types
|
|
159
|
-
if is_union_type(field_type):
|
|
160
|
-
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
161
|
-
if int in types:
|
|
162
|
-
default_values[field_name] = 0
|
|
163
|
-
elif float in types:
|
|
164
|
-
default_values[field_name] = 0.0
|
|
165
|
-
elif str in types:
|
|
166
|
-
default_values[field_name] = ""
|
|
167
|
-
elif bool in types:
|
|
168
|
-
default_values[field_name] = False
|
|
169
|
-
continue
|
|
170
|
-
|
|
171
|
-
# Set type-appropriate default values based on Python type
|
|
172
|
-
if isinstance(field_type, type):
|
|
173
|
-
if issubclass(field_type, str):
|
|
174
|
-
default_values[field_name] = ""
|
|
175
|
-
elif issubclass(field_type, int):
|
|
176
|
-
default_values[field_name] = 0
|
|
177
|
-
elif issubclass(field_type, float):
|
|
178
|
-
default_values[field_name] = 0.0
|
|
179
|
-
elif issubclass(field_type, bool):
|
|
180
|
-
default_values[field_name] = False
|
|
181
|
-
elif is_dataclass_like(field_type):
|
|
182
|
-
# For nested models, recursively create default instances
|
|
183
|
-
default_values[field_name] = create_default_instance(field_type)
|
|
184
|
-
|
|
185
|
-
return model_class(**default_values)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
def get_description(field: fieldz.Field) -> str | None:
|
|
189
|
-
if "description" in field.metadata:
|
|
190
|
-
return field.metadata["description"]
|
|
191
|
-
if hasattr(field.native_field, "description"):
|
|
192
|
-
return field.native_field.description # type: ignore
|
|
193
|
-
return None
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
def add_new_item(items_list: list, item_type: Any) -> None:
|
|
197
|
-
"""Add a new item to a list based on its type."""
|
|
198
|
-
if is_dataclass_like(item_type):
|
|
199
|
-
# For dataclass-like types, create a default instance
|
|
200
|
-
new_item = create_default_instance(item_type)
|
|
201
|
-
if new_item is not None:
|
|
202
|
-
items_list.append(new_item)
|
|
203
|
-
# For basic types, add appropriate default values
|
|
204
|
-
elif item_type is str:
|
|
205
|
-
items_list.append("")
|
|
206
|
-
elif item_type is int:
|
|
207
|
-
items_list.append(0)
|
|
208
|
-
elif item_type is float:
|
|
209
|
-
items_list.append(0.0)
|
|
210
|
-
elif item_type is bool:
|
|
211
|
-
items_list.append(False)
|
|
212
|
-
elif is_union_type(item_type):
|
|
213
|
-
# For union types, use the first non-None type
|
|
214
|
-
types = [t for t in get_args(item_type) if t is not type(None)]
|
|
215
|
-
if int in types:
|
|
216
|
-
items_list.append(0)
|
|
217
|
-
elif float in types:
|
|
218
|
-
items_list.append(0.0)
|
|
219
|
-
elif str in types:
|
|
220
|
-
items_list.append("")
|
|
221
|
-
elif bool in types:
|
|
222
|
-
items_list.append(False)
|
|
223
|
-
else:
|
|
224
|
-
items_list.append(None)
|
|
225
|
-
else:
|
|
226
|
-
# For unknown types, add None
|
|
227
|
-
items_list.append(None)
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
def get_inner_type(field_info: Any) -> Any:
|
|
231
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
232
|
-
try:
|
|
233
|
-
return get_args(annotation)[0] # Get type of set items
|
|
234
|
-
except (IndexError, TypeError):
|
|
235
|
-
return Any
|
|
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.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{streambricks-0.2.12 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/type_renderers.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|