Skip to content

Commit 5c5c79d

Browse files
fix(parsing): parse extra field types
1 parent 876c80a commit 5c5c79d

29 files changed

Lines changed: 105 additions & 30 deletions

src/cloudflare/_models.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,18 @@ def construct( # pyright: ignore[reportIncompatibleMethodOverride]
209209
else:
210210
fields_values[name] = field_get_default(field)
211211

212+
extra_field_type = _get_extra_fields_type(__cls)
213+
212214
_extra = {}
213215
for key, value in values.items():
214216
if key not in model_fields:
217+
parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
218+
215219
if PYDANTIC_V2:
216-
_extra[key] = value
220+
_extra[key] = parsed
217221
else:
218222
_fields_set.add(key)
219-
fields_values[key] = value
223+
fields_values[key] = parsed
220224

221225
object.__setattr__(m, "__dict__", fields_values)
222226

@@ -371,6 +375,23 @@ def _construct_field(value: object, field: FieldInfo, key: str) -> object:
371375
return construct_type(value=value, type_=type_, metadata=getattr(field, "metadata", None))
372376

373377

378+
def _get_extra_fields_type(cls: type[pydantic.BaseModel]) -> type | None:
379+
if not PYDANTIC_V2:
380+
# TODO
381+
return None
382+
383+
schema = cls.__pydantic_core_schema__
384+
if schema["type"] == "model":
385+
fields = schema["schema"]
386+
if fields["type"] == "model-fields":
387+
extras = fields.get("extras_schema")
388+
if extras and "cls" in extras:
389+
# mypy can't narrow the type
390+
return extras["cls"] # type: ignore[no-any-return]
391+
392+
return None
393+
394+
374395
def is_basemodel(type_: type) -> bool:
375396
"""Returns whether or not the given type is either a `BaseModel` or a union of `BaseModel`"""
376397
if is_union(type_):

src/cloudflare/types/radar/ai/inference/timeseries_groups/summary_model_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/ai/inference/timeseries_groups/summary_task_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/ai/timeseries_group_user_agent_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/as112/timeseries_group_query_type_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/as112/timeseries_group_response_codes_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/as112_timeseries_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -104,6 +104,7 @@ class AS112TimeseriesResponse(BaseModel):
104104
meta: Meta
105105
"""Metadata for the results."""
106106

107+
__pydantic_extra__: Dict[str, AS112TimeseriesResponseItem] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
107108
if TYPE_CHECKING:
108109
# Stub to indicate that arbitrary properties are accepted.
109110
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/attacks/layer3/timeseries_group_industry_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/attacks/layer3/timeseries_group_vector_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

src/cloudflare/types/radar/attacks/layer3/timeseries_group_vertical_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Dict, List
44
from datetime import datetime
55
from typing_extensions import Literal
66

@@ -97,6 +97,7 @@ class Meta(BaseModel):
9797
class Serie0(BaseModel):
9898
timestamps: List[datetime]
9999

100+
__pydantic_extra__: Dict[str, List[str]] = FieldInfo(init=False) # pyright: ignore[reportIncompatibleVariableOverride]
100101
if TYPE_CHECKING:
101102
# Stub to indicate that arbitrary properties are accepted.
102103
# To access properties that are not valid identifiers you can use `getattr`, e.g.

0 commit comments

Comments
 (0)