Skip to content

Commit 8b8c355

Browse files
committed
fix dataclass_with_extra
1 parent de1530a commit 8b8c355

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/huggingface_hub/inference/_generated/types/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import inspect
1717
import json
18+
import types
1819
from dataclasses import asdict, dataclass
1920
from typing import Any, TypeVar, Union, get_args
2021

@@ -109,7 +110,9 @@ def parse_obj(cls: type[T], data: Union[bytes, str, list, dict]) -> Union[list[T
109110
else:
110111
expected_types = get_args(field_type)
111112
for expected_type in expected_types:
112-
if getattr(expected_type, "_name", None) == "List":
113+
if (
114+
isinstance(expected_type, types.GenericAlias) and expected_type.__origin__ is list
115+
) or getattr(expected_type, "_name", None) == "List":
113116
expected_type = get_args(expected_type)[
114117
0
115118
] # assume same type for all items in the list

tests/test_inference_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22
import json
3-
from typing import Optional, Union, get_args, get_origin
3+
from typing import List, Optional, Union, get_args, get_origin
44

55
import pytest
66

@@ -18,7 +18,7 @@ class DummyType(BaseInferenceType):
1818
@dataclass_with_extra
1919
class DummyNestedType(BaseInferenceType):
2020
item: DummyType
21-
items: list[DummyType]
21+
items: List[DummyType] # works both with List and list
2222
maybe_items: Optional[list[DummyType]] = None
2323

2424

@@ -97,6 +97,7 @@ def test_parse_nested_class():
9797
def test_all_fields_are_optional():
9898
# all fields are optional => silently accept None if server returns less data than expected
9999
instance = DummyNestedType.parse_obj({"maybe_items": [{}, DUMMY_AS_BYTES]})
100+
assert isinstance(instance, DummyNestedType)
100101
assert instance.item is None
101102
assert instance.items is None
102103
assert len(instance.maybe_items) == 2

0 commit comments

Comments
 (0)