Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 089c820

Browse files
stainless-app[bot]stainless-bot
authored andcommittedJan 9, 2025
fix: correctly handle deserialising cls fields (#2002)
1 parent 36548f8 commit 089c820

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎src/openai/_models.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,14 @@ def __str__(self) -> str:
204204
@classmethod
205205
@override
206206
def construct( # pyright: ignore[reportIncompatibleMethodOverride]
207-
cls: Type[ModelT],
207+
__cls: Type[ModelT],
208208
_fields_set: set[str] | None = None,
209209
**values: object,
210210
) -> ModelT:
211-
m = cls.__new__(cls)
211+
m = __cls.__new__(__cls)
212212
fields_values: dict[str, object] = {}
213213

214-
config = get_model_config(cls)
214+
config = get_model_config(__cls)
215215
populate_by_name = (
216216
config.allow_population_by_field_name
217217
if isinstance(config, _ConfigProtocol)
@@ -221,7 +221,7 @@ def construct( # pyright: ignore[reportIncompatibleMethodOverride]
221221
if _fields_set is None:
222222
_fields_set = set()
223223

224-
model_fields = get_model_fields(cls)
224+
model_fields = get_model_fields(__cls)
225225
for name, field in model_fields.items():
226226
key = field.alias
227227
if key is None or (key not in values and populate_by_name):

‎tests/test_models.py

+10
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,13 @@ class Model(BaseModel):
844844
assert m.alias == "foo"
845845
assert isinstance(m.union, str)
846846
assert m.union == "bar"
847+
848+
849+
@pytest.mark.skipif(not PYDANTIC_V2, reason="TypeAliasType is not supported in Pydantic v1")
850+
def test_field_named_cls() -> None:
851+
class Model(BaseModel):
852+
cls: str
853+
854+
m = construct_type(value={"cls": "foo"}, type_=Model)
855+
assert isinstance(m, Model)
856+
assert isinstance(m.cls, str)

0 commit comments

Comments
 (0)
Please sign in to comment.