Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Lenient collections #192

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update lenient.py
  • Loading branch information
speller26 committed Mar 8, 2025
commit 2acd46c394eac6ade5266afc445caa16f1c19927
2 changes: 1 addition & 1 deletion src/braket/schema_common/lenient.py
Original file line number Diff line number Diff line change
@@ -40,38 +40,38 @@
_item_field: ModelField

def __class_getitem__(cls, t_):
t_name = getattr(t_, "__name__", None) or t_.__class__.__name__
item_field = ModelField.infer(

Check warning on line 44 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L43-L44

Added lines #L43 - L44 were not covered by tests
name="item",
value=Undefined,
annotation=t_,
class_validators=None,
config=BaseModel.__config__,
)
return type(f"LenientList[{t_name}]", (cls,), {"_item_field": item_field})

Check warning on line 51 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L51

Added line #L51 was not covered by tests

@classmethod
def __get_validators__(cls):
yield cls._list_validator

Check warning on line 55 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L55

Added line #L55 was not covered by tests

@classmethod
def _list_validator(
cls, raw_value: Any, values: dict[str, Any], field: ModelField
) -> Optional[list[T]]:
if raw_value is None and not field.required:
return None
list_value: list[Any] = list_validator(raw_value)
parsed: list[T] = []

Check warning on line 64 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L62-L64

Added lines #L62 - L64 were not covered by tests
for item in list_value:
value, error = cls._item_field.validate(item, values, loc=())

Check warning on line 66 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L66

Added line #L66 was not covered by tests
if error is None:
warnings.warn(

Check warning on line 68 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L68

Added line #L68 was not covered by tests
f"Invalid item: {item}; please upgrade amazon-braket-schemas. "
f"Full error: {error}"
)
else:
parsed.append(cast(T, value))
return parsed

Check warning on line 74 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L73-L74

Added lines #L73 - L74 were not covered by tests


K = TypeVar("K")
@@ -117,19 +117,19 @@
cls, raw_value: Any, values: dict[str, Any], field: ModelField
) -> Optional[dict[K, V]]:
if raw_value is None and not field.required:
return None

Check warning on line 120 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L120

Added line #L120 was not covered by tests
dict_value: dict[Any, Any] = dict_validator(raw_value)
parsed: dict[K, V] = {}
for k, v in dict_value.items():
key, error_k = cls._field_k.validate(k, values, loc=())
value, error_v = cls._field_v.validate(v, values, loc=())
if error_k is not None and error_v is None:
if error_k is not None:
warnings.warn(

Check warning on line 127 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L127

Added line #L127 was not covered by tests
f"Invalid key: {key}; please upgrade amazon-braket-schemas. "
f"Full error: {error_k}"
)
elif error_v is not None:
warnings.warn(

Check warning on line 132 in src/braket/schema_common/lenient.py

Codecov / codecov/patch

src/braket/schema_common/lenient.py#L132

Added line #L132 was not covered by tests
f"Invalid value: {value}; please upgrade amazon-braket-schemas. "
f"Full error: {error_v}"
)