streambricks 0.2.13__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.13 → streambricks-0.2.14}/PKG-INFO +1 -1
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/__init__.py +1 -1
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/main.py +137 -61
- streambricks-0.2.14/src/streambricks/widgets/type_helpers.py +392 -0
- streambricks-0.2.13/src/streambricks/widgets/type_helpers.py +0 -242
- {streambricks-0.2.13 → streambricks-0.2.14}/.copier-answers.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.github/FUNDING.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.github/copilot-instructions.md +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.github/dependabot.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.github/workflows/build.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.github/workflows/documentation.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.gitignore +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/.pre-commit-config.yaml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/LICENSE +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/README.md +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/docs/.empty +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/duties.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/mkdocs.yml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/overrides/_dummy.txt +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/pyproject.toml +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/helpers.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/py.typed +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/state.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/__init__.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/bind_kwargs.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/model_selector.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/__init__.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/src/streambricks/widgets/multi_select.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/tests/__init__.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/tests/conftest.py +0 -0
- {streambricks-0.2.13 → streambricks-0.2.14}/tests/test_widgets.py +0 -0
|
@@ -31,7 +31,9 @@ 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,
|
|
@@ -78,24 +80,55 @@ def render_union_field( # noqa: PLR0911
|
|
|
78
80
|
) -> Any:
|
|
79
81
|
"""Render a field that can accept multiple types."""
|
|
80
82
|
annotation = field_info.get("type") or field_info.get("annotation")
|
|
81
|
-
|
|
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]
|
|
82
94
|
type_key = f"{key}_type"
|
|
83
|
-
type_names = [
|
|
84
|
-
t.__name__ if hasattr(t, "__name__") else str(t) for t in possible_types
|
|
85
|
-
]
|
|
86
95
|
selected_type_name = st.selectbox(
|
|
87
96
|
f"Type for {label or key}",
|
|
88
|
-
options=
|
|
97
|
+
options=type_descriptions,
|
|
98
|
+
index=default_index,
|
|
89
99
|
key=type_key,
|
|
90
100
|
disabled=disabled,
|
|
91
101
|
help=help,
|
|
92
102
|
)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
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]
|
|
96
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
|
|
97
129
|
modified_field_info = field_info.copy()
|
|
98
130
|
modified_field_info["type"] = selected_type
|
|
131
|
+
|
|
99
132
|
typed_value: Any = None
|
|
100
133
|
if value is not None:
|
|
101
134
|
try:
|
|
@@ -133,7 +166,7 @@ def render_union_field( # noqa: PLR0911
|
|
|
133
166
|
if selected_type is bool and not isinstance(result, bool):
|
|
134
167
|
return bool(result)
|
|
135
168
|
except (ValueError, TypeError) as e:
|
|
136
|
-
error_msg = f"Cannot convert {result} to {
|
|
169
|
+
error_msg = f"Cannot convert {result} to {selected_type_name}: {e!s}"
|
|
137
170
|
st.error(error_msg)
|
|
138
171
|
if selected_type is int:
|
|
139
172
|
return 0
|
|
@@ -460,7 +493,7 @@ def wrap_as_optional_field(renderer: WidgetFunc) -> WidgetFunc:
|
|
|
460
493
|
return optional_wrapper
|
|
461
494
|
|
|
462
495
|
|
|
463
|
-
def render_model_instance_field(
|
|
496
|
+
def render_model_instance_field( # noqa: PLR0911
|
|
464
497
|
*,
|
|
465
498
|
key: str,
|
|
466
499
|
value: Any = None,
|
|
@@ -474,52 +507,57 @@ def render_model_instance_field(
|
|
|
474
507
|
if model_class is None:
|
|
475
508
|
error_msg = f"Model class not provided for field {key}"
|
|
476
509
|
raise ValueError(error_msg)
|
|
477
|
-
|
|
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
|
|
510
|
+
if is_union_type(model_class):
|
|
487
511
|
if value is None:
|
|
488
|
-
# For Union types, try
|
|
512
|
+
# For Union types, try each possible model type
|
|
489
513
|
possible_types = get_args(model_class)
|
|
490
514
|
for possible_type in possible_types:
|
|
491
515
|
if is_dataclass_like(possible_type):
|
|
492
516
|
try:
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
except Exception: # noqa: BLE001
|
|
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}")
|
|
497
522
|
continue
|
|
498
523
|
|
|
499
524
|
if value is None:
|
|
500
|
-
|
|
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
|
+
)
|
|
501
537
|
return None
|
|
502
538
|
# Initialize value if none for non-union types
|
|
503
539
|
elif value is None:
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
st.error(error_msg)
|
|
511
|
-
return 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
|
|
512
546
|
|
|
513
547
|
st.markdown(f"**{label or key}**")
|
|
514
548
|
if help:
|
|
515
549
|
st.caption(help)
|
|
516
550
|
|
|
517
551
|
with st.expander("Edit", expanded=True):
|
|
552
|
+
if value is None:
|
|
553
|
+
st.error("Cannot render model: creation failed")
|
|
554
|
+
return None
|
|
555
|
+
|
|
518
556
|
updated_value = {}
|
|
519
557
|
try:
|
|
520
558
|
# Use the actual class of the value instance, not the field type
|
|
521
559
|
actual_class = value.__class__
|
|
522
|
-
for field in fieldz.fields(actual_class):
|
|
560
|
+
for field in fieldz.fields(actual_class, parse_annotated=True):
|
|
523
561
|
field_name = field.name
|
|
524
562
|
field_value = get_with_default(value, field_name, field)
|
|
525
563
|
field_help = get_description(field)
|
|
@@ -615,7 +653,7 @@ def render_model_readonly[T](
|
|
|
615
653
|
|
|
616
654
|
with st.container():
|
|
617
655
|
# Get all fields from the model
|
|
618
|
-
for field in fieldz.fields(model_class):
|
|
656
|
+
for field in fieldz.fields(model_class, parse_annotated=True):
|
|
619
657
|
field_name = field.name
|
|
620
658
|
if field_name in excluded_fields:
|
|
621
659
|
continue
|
|
@@ -703,7 +741,7 @@ def display_model_readonly(value, key=None):
|
|
|
703
741
|
for field in fieldz.fields(model_class):
|
|
704
742
|
field_name = field.name
|
|
705
743
|
field_value = getattr(value, field_name, None)
|
|
706
|
-
if field_value ==
|
|
744
|
+
if field_value == fieldz.Field.MISSING:
|
|
707
745
|
field_value = get_with_default(value, field_name, field)
|
|
708
746
|
sub_key = f"{key}_{field_name}" if key else field_name
|
|
709
747
|
cols = st.columns([0.3, 0.7])
|
|
@@ -715,10 +753,7 @@ def display_model_readonly(value, key=None):
|
|
|
715
753
|
|
|
716
754
|
def render_model_field(model_class, field_name, value=None, container=st):
|
|
717
755
|
"""Render a field from a model using a compact layout."""
|
|
718
|
-
field =
|
|
719
|
-
if field is None:
|
|
720
|
-
error_msg = f"Field {field_name} not found in {model_class.__name__}"
|
|
721
|
-
raise ValueError(error_msg)
|
|
756
|
+
field = get_field(model_class, field_name)
|
|
722
757
|
field_info = {"name": field.name, "type": field.type, "default": field.default}
|
|
723
758
|
if hasattr(field.native_field, "json_schema_extra"):
|
|
724
759
|
dct = field.native_field.json_schema_extra or {} # type: ignore
|
|
@@ -772,7 +807,6 @@ def render_model_form(
|
|
|
772
807
|
result = {}
|
|
773
808
|
field_groups: dict[str, Any] = {}
|
|
774
809
|
for field in fieldz.fields(model_class):
|
|
775
|
-
# Check if field has a category defined
|
|
776
810
|
category = "General"
|
|
777
811
|
if "category" in field.metadata:
|
|
778
812
|
category = field.metadata["category"]
|
|
@@ -789,34 +823,61 @@ def render_model_form(
|
|
|
789
823
|
for i, (_group_name, fields) in enumerate(field_groups.items()):
|
|
790
824
|
with tabs[i]:
|
|
791
825
|
for field in fields:
|
|
792
|
-
|
|
793
|
-
if field_name in excluded_fields:
|
|
826
|
+
if field.name in excluded_fields:
|
|
794
827
|
continue
|
|
795
|
-
current_value = get_with_default(instance,
|
|
796
|
-
result[
|
|
797
|
-
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
|
|
798
831
|
)
|
|
799
832
|
else:
|
|
800
833
|
# Single category, render fields directly
|
|
801
|
-
for field in fieldz.fields(model_class):
|
|
802
|
-
|
|
803
|
-
if field_name in excluded_fields:
|
|
834
|
+
for field in fieldz.fields(model_class, parse_annotated=True):
|
|
835
|
+
if field.name in excluded_fields:
|
|
804
836
|
continue
|
|
805
|
-
current_value = get_with_default(instance,
|
|
806
|
-
result[
|
|
807
|
-
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
|
|
808
840
|
)
|
|
809
841
|
|
|
810
842
|
return fieldz.replace(instance, **result)
|
|
811
843
|
|
|
812
844
|
|
|
813
845
|
if __name__ == "__main__":
|
|
814
|
-
from typing import Literal
|
|
846
|
+
from typing import Annotated, Literal
|
|
815
847
|
|
|
816
|
-
|
|
848
|
+
import pydantic
|
|
817
849
|
|
|
818
850
|
from streambricks.helpers import run
|
|
819
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
|
+
|
|
820
881
|
class SubModel(BaseModel):
|
|
821
882
|
"""Test submodel."""
|
|
822
883
|
|
|
@@ -825,36 +886,51 @@ if __name__ == "__main__":
|
|
|
825
886
|
active: bool = True
|
|
826
887
|
|
|
827
888
|
class TestModel(BaseModel):
|
|
828
|
-
"""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."""
|
|
829
894
|
|
|
830
895
|
status: int | str | bool = 2
|
|
831
896
|
"""A field that can be either int, str, or bool."""
|
|
897
|
+
|
|
832
898
|
boolean: bool = True
|
|
833
899
|
"""Optional text field."""
|
|
900
|
+
|
|
834
901
|
long_text: str | None = "test " * 40
|
|
835
902
|
"""Long text."""
|
|
836
903
|
|
|
837
904
|
optional_int: int | None = None
|
|
838
905
|
"""An optional integer that can be None."""
|
|
906
|
+
|
|
839
907
|
optional_string: str | None = None
|
|
840
908
|
"""An optional string that can be None."""
|
|
909
|
+
|
|
841
910
|
optional_model: SubModel | None = None
|
|
842
911
|
"""An optional nested model that can be None."""
|
|
843
912
|
|
|
844
|
-
tags: list[str] = Field(default_factory=list)
|
|
913
|
+
tags: list[str] = pydantic.Field(default_factory=list)
|
|
845
914
|
"""A list of string tags."""
|
|
846
|
-
|
|
915
|
+
|
|
916
|
+
numbers: list[int | float] = pydantic.Field(default_factory=list)
|
|
847
917
|
"""A list of numbers (int or float)"""
|
|
848
|
-
|
|
918
|
+
|
|
919
|
+
settings: list[SubModel] = pydantic.Field(default_factory=list)
|
|
849
920
|
"""A list of nested models"""
|
|
850
|
-
|
|
921
|
+
|
|
922
|
+
priorities: list[Literal["Low", "Medium", "High"]] = pydantic.Field(
|
|
923
|
+
default_factory=list
|
|
924
|
+
)
|
|
851
925
|
"""A list of priority levels."""
|
|
852
926
|
|
|
853
927
|
def demo():
|
|
854
928
|
st.title("Pydantic Form Demo")
|
|
855
929
|
if "model" not in st.session_state:
|
|
856
930
|
st.session_state.model = TestModel(status=2)
|
|
857
|
-
|
|
931
|
+
|
|
932
|
+
st.session_state.model = render_model_form(st.session_state.model)
|
|
933
|
+
|
|
858
934
|
with st.expander("Current Model State", expanded=True):
|
|
859
935
|
st.json(st.session_state.model.model_dump_json(indent=2))
|
|
860
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,242 +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 Annotated, Any, Literal, get_args, get_origin
|
|
7
|
-
|
|
8
|
-
import fieldz
|
|
9
|
-
|
|
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
|
-
|
|
18
|
-
def is_literal_type(annotation: Any) -> bool:
|
|
19
|
-
"""Check if a type annotation is a Literal type."""
|
|
20
|
-
# Check directly against the origin or special attribute
|
|
21
|
-
return (
|
|
22
|
-
get_origin(annotation) is Literal
|
|
23
|
-
or getattr(annotation, "__origin__", None) is Literal
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def is_union_type(annotation: Any) -> bool:
|
|
28
|
-
"""Check if a type annotation is a Union type."""
|
|
29
|
-
origin = get_origin(annotation)
|
|
30
|
-
# Check if it's Union or Optional (which is Union[T, None])
|
|
31
|
-
return origin is not None and (
|
|
32
|
-
origin.__name__ == "Union" if hasattr(origin, "__name__") else False
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def is_optional_type(annotation: Any) -> bool:
|
|
37
|
-
"""Check if a type annotation is an Optional type (T | None)."""
|
|
38
|
-
if not is_union_type(annotation):
|
|
39
|
-
return False
|
|
40
|
-
|
|
41
|
-
args = get_args(annotation)
|
|
42
|
-
return type(None) in args
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def is_set_type(annotation: Any) -> bool:
|
|
46
|
-
"""Check if the annotation represents a set type."""
|
|
47
|
-
if annotation is set:
|
|
48
|
-
return True
|
|
49
|
-
origin = get_origin(annotation)
|
|
50
|
-
return origin is set
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def is_sequence_type(annotation: Any) -> bool:
|
|
54
|
-
"""Check if an annotation represents a sequence type (except sets)."""
|
|
55
|
-
origin = get_origin(annotation)
|
|
56
|
-
if origin in (list, tuple, Sequence):
|
|
57
|
-
return True
|
|
58
|
-
|
|
59
|
-
return annotation in (list, tuple, Sequence)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def get_with_default(obj: Any, field_name: str, field_info: Any = None) -> Any: # noqa: PLR0911
|
|
63
|
-
"""Get field value with appropriate default if it's missing."""
|
|
64
|
-
# Get the raw value
|
|
65
|
-
value = getattr(obj, field_name, None)
|
|
66
|
-
|
|
67
|
-
# If value isn't MISSING, return it as is
|
|
68
|
-
if value != "MISSING":
|
|
69
|
-
return value
|
|
70
|
-
|
|
71
|
-
# If we don't have field info, get it
|
|
72
|
-
if field_info is None:
|
|
73
|
-
for field in fieldz.fields(obj.__class__):
|
|
74
|
-
if field.name == field_name:
|
|
75
|
-
field_info = field
|
|
76
|
-
break
|
|
77
|
-
|
|
78
|
-
# If we have field info, use it to determine appropriate default
|
|
79
|
-
if field_info is not None:
|
|
80
|
-
field_type = field_info.type
|
|
81
|
-
|
|
82
|
-
# Handle Union types
|
|
83
|
-
if is_union_type(field_type):
|
|
84
|
-
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
85
|
-
if int in types:
|
|
86
|
-
return 0
|
|
87
|
-
if float in types:
|
|
88
|
-
return 0.0
|
|
89
|
-
if str in types:
|
|
90
|
-
return ""
|
|
91
|
-
if bool in types:
|
|
92
|
-
return False
|
|
93
|
-
if types and isinstance(types[0], type):
|
|
94
|
-
if issubclass(types[0], int):
|
|
95
|
-
return 0
|
|
96
|
-
if issubclass(types[0], float):
|
|
97
|
-
return 0.0
|
|
98
|
-
if issubclass(types[0], str):
|
|
99
|
-
return ""
|
|
100
|
-
if issubclass(types[0], bool):
|
|
101
|
-
return False
|
|
102
|
-
|
|
103
|
-
# Handle basic types
|
|
104
|
-
if isinstance(field_type, type):
|
|
105
|
-
if issubclass(field_type, int):
|
|
106
|
-
return 0
|
|
107
|
-
if issubclass(field_type, float):
|
|
108
|
-
return 0.0
|
|
109
|
-
if issubclass(field_type, str):
|
|
110
|
-
return ""
|
|
111
|
-
if issubclass(field_type, bool):
|
|
112
|
-
return False
|
|
113
|
-
if (
|
|
114
|
-
issubclass(field_type, list)
|
|
115
|
-
or issubclass(field_type, set)
|
|
116
|
-
or issubclass(field_type, tuple)
|
|
117
|
-
):
|
|
118
|
-
return []
|
|
119
|
-
|
|
120
|
-
# Default fallback for unknown types
|
|
121
|
-
return None
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def is_dataclass_like(annotation: Any) -> bool:
|
|
125
|
-
"""Check if a type is a dataclass-like object (Pydantic model, attrs, etc.)."""
|
|
126
|
-
if not isinstance(annotation, type):
|
|
127
|
-
return False
|
|
128
|
-
try:
|
|
129
|
-
fields = fieldz.fields(annotation)
|
|
130
|
-
# If we get fields, it's a dataclass-like object
|
|
131
|
-
return len(fields) > 0
|
|
132
|
-
except Exception: # noqa: BLE001
|
|
133
|
-
# If fieldz can't handle it, it's not a dataclass-like object
|
|
134
|
-
return False
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def create_default_instance(model_class: type) -> Any:
|
|
138
|
-
"""Create a default instance of a model with default values for required fields."""
|
|
139
|
-
# Create an empty dict to collect required values
|
|
140
|
-
default_values = {}
|
|
141
|
-
|
|
142
|
-
# Get all fields
|
|
143
|
-
for field in fieldz.fields(model_class):
|
|
144
|
-
field_name = field.name
|
|
145
|
-
|
|
146
|
-
# Check if the field already has a default value
|
|
147
|
-
has_default = False
|
|
148
|
-
if hasattr(field, "default") and field.default != "MISSING":
|
|
149
|
-
# Use the field's default value
|
|
150
|
-
default_values[field_name] = field.default
|
|
151
|
-
has_default = True
|
|
152
|
-
elif hasattr(field, "default_factory") and field.default_factory != "MISSING":
|
|
153
|
-
try:
|
|
154
|
-
# Use the field's default factory
|
|
155
|
-
default_values[field_name] = field.default_factory() # pyright: ignore
|
|
156
|
-
has_default = True
|
|
157
|
-
except Exception: # noqa: BLE001
|
|
158
|
-
# If default_factory fails, fall back to type-based defaults
|
|
159
|
-
pass
|
|
160
|
-
|
|
161
|
-
# If the field doesn't have a default, create one based on type
|
|
162
|
-
if not has_default:
|
|
163
|
-
field_type = field.type
|
|
164
|
-
|
|
165
|
-
# Handle union types
|
|
166
|
-
if is_union_type(field_type):
|
|
167
|
-
types = [t for t in get_args(field_type) if t is not type(None)]
|
|
168
|
-
if int in types:
|
|
169
|
-
default_values[field_name] = 0
|
|
170
|
-
elif float in types:
|
|
171
|
-
default_values[field_name] = 0.0
|
|
172
|
-
elif str in types:
|
|
173
|
-
default_values[field_name] = ""
|
|
174
|
-
elif bool in types:
|
|
175
|
-
default_values[field_name] = False
|
|
176
|
-
continue
|
|
177
|
-
|
|
178
|
-
# Set type-appropriate default values based on Python type
|
|
179
|
-
if isinstance(field_type, type):
|
|
180
|
-
if issubclass(field_type, str):
|
|
181
|
-
default_values[field_name] = ""
|
|
182
|
-
elif issubclass(field_type, int):
|
|
183
|
-
default_values[field_name] = 0
|
|
184
|
-
elif issubclass(field_type, float):
|
|
185
|
-
default_values[field_name] = 0.0
|
|
186
|
-
elif issubclass(field_type, bool):
|
|
187
|
-
default_values[field_name] = False
|
|
188
|
-
elif is_dataclass_like(field_type):
|
|
189
|
-
# For nested models, recursively create default instances
|
|
190
|
-
default_values[field_name] = create_default_instance(field_type)
|
|
191
|
-
|
|
192
|
-
return model_class(**default_values)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
def get_description(field: fieldz.Field) -> str | None:
|
|
196
|
-
if "description" in field.metadata:
|
|
197
|
-
return field.metadata["description"]
|
|
198
|
-
if hasattr(field.native_field, "description"):
|
|
199
|
-
return field.native_field.description # type: ignore
|
|
200
|
-
return None
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
def add_new_item(items_list: list, item_type: Any) -> None:
|
|
204
|
-
"""Add a new item to a list based on its type."""
|
|
205
|
-
if is_dataclass_like(item_type):
|
|
206
|
-
# For dataclass-like types, create a default instance
|
|
207
|
-
new_item = create_default_instance(item_type)
|
|
208
|
-
if new_item is not None:
|
|
209
|
-
items_list.append(new_item)
|
|
210
|
-
# For basic types, add appropriate default values
|
|
211
|
-
elif item_type is str:
|
|
212
|
-
items_list.append("")
|
|
213
|
-
elif item_type is int:
|
|
214
|
-
items_list.append(0)
|
|
215
|
-
elif item_type is float:
|
|
216
|
-
items_list.append(0.0)
|
|
217
|
-
elif item_type is bool:
|
|
218
|
-
items_list.append(False)
|
|
219
|
-
elif is_union_type(item_type):
|
|
220
|
-
# For union types, use the first non-None type
|
|
221
|
-
types = [t for t in get_args(item_type) if t is not type(None)]
|
|
222
|
-
if int in types:
|
|
223
|
-
items_list.append(0)
|
|
224
|
-
elif float in types:
|
|
225
|
-
items_list.append(0.0)
|
|
226
|
-
elif str in types:
|
|
227
|
-
items_list.append("")
|
|
228
|
-
elif bool in types:
|
|
229
|
-
items_list.append(False)
|
|
230
|
-
else:
|
|
231
|
-
items_list.append(None)
|
|
232
|
-
else:
|
|
233
|
-
# For unknown types, add None
|
|
234
|
-
items_list.append(None)
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
def get_inner_type(field_info: Any) -> Any:
|
|
238
|
-
annotation = field_info.get("type") or field_info.get("annotation")
|
|
239
|
-
try:
|
|
240
|
-
return get_args(annotation)[0] # Get type of set items
|
|
241
|
-
except (IndexError, TypeError):
|
|
242
|
-
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.13 → streambricks-0.2.14}/src/streambricks/widgets/model_widget/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{streambricks-0.2.13 → 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
|