Skip to content

Commit 5b0195d

Browse files
committed
refactor(models): use unwrapped type for validate_type and collect all Annotated metadata
1 parent ab4b266 commit 5b0195d

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

src/anthropic/_models.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,7 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]
501501
If the given value does not match the expected type then it is returned as-is.
502502
"""
503503

504-
# store a reference to the original type we were given before we extract any inner
505-
# types so that we can properly resolve forward references in `TypeAliasType` annotations
506-
original_type = type_
507-
508-
# unwrap `Annotated[T, ...]` -> `T` and `TypeAliasType` -> `T.__value__`
509-
# while collecting metadata from `Annotated`
510-
#
511-
# we allow `object` as the input type because otherwise, passing things like
512-
# `Literal['value']` will be reported as a type error by type checkers
513-
type_ = cast("type[object]", type_)
514-
504+
meta: tuple[Any, ...]
515505
if metadata is not None:
516506
meta = tuple(metadata)
517507
else:
@@ -523,22 +513,18 @@ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]
523513
continue
524514

525515
if is_annotated_type(type_):
526-
if not meta:
527-
meta = get_args(type_)[1:]
528-
516+
meta = meta + get_args(type_)[1:]
529517
type_ = cast("type[object]", extract_type_arg(type_, 0))
530518
continue
531519

532-
533-
534520
# we need to use the origin class for any types that are subscripted generics
535521
# e.g. Dict[str, object]
536522
origin = get_origin(type_) or type_
537523
args = get_args(type_)
538524

539525
if is_union(origin):
540526
try:
541-
return validate_type(type_=cast("type[object]", original_type or type_), value=value)
527+
return validate_type(type_=cast("type[object]", type_), value=value)
542528
except Exception:
543529
pass
544530

0 commit comments

Comments
 (0)