Skip to content

Commit

Permalink
fix: python using the wrong key type for dictionaries (#1867)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Mar 11, 2024
1 parent fd56537 commit 15e406b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/generators/python/PythonConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const PythonDefaultTypeMapping: PythonTypeMapping = {
return unionTypes.join(' | ');
},
Dictionary({ constrainedModel }): string {
return `dict[${constrainedModel.value.type}, ${constrainedModel.value.type}]`;
return `dict[${constrainedModel.key.type}, ${constrainedModel.value.type}]`;
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/generators/python/PythonConstrainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('PythonConstrainer', () => {
'test',
undefined,
{},
'str'
'str2'
);
const model = new ConstrainedDictionaryModel(
'test',
Expand All @@ -237,7 +237,7 @@ describe('PythonConstrainer', () => {
constrainedModel: model,
...defaultOptions
});
expect(type).toEqual('dict[str, str]');
expect(type).toEqual('dict[str, str2]');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ exports[`PYTHON_PYDANTIC_PRESET should render pydantic for class 1`] = `
multi
line
description''', default=None, serialization_alias='prop')
additional_properties: Optional[dict[Any, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
additional_properties: Optional[dict[str, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
"
`;

exports[`PYTHON_PYDANTIC_PRESET should render union to support Python < 3.10 1`] = `
Array [
"class UnionTest(BaseModel):
union_test: Optional[Union[Union1, Union2]] = Field(default=None, serialization_alias='unionTest')
additional_properties: Optional[dict[Any, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
additional_properties: Optional[dict[str, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
",
"class Union1(BaseModel):
test_prop1: Optional[str] = Field(default=None, serialization_alias='testProp1')
additional_properties: Optional[dict[Any, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
additional_properties: Optional[dict[str, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
",
"class Union2(BaseModel):
test_prop2: Optional[str] = Field(default=None, serialization_alias='testProp2')
additional_properties: Optional[dict[Any, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
additional_properties: Optional[dict[str, Any]] = Field(default=None, serialization_alias='additionalProperties', exclude=True)
",
]
`;

0 comments on commit 15e406b

Please sign in to comment.