Skip to content

Commit

Permalink
Fixed issue causing KeyError to be raised when building schema from m…
Browse files Browse the repository at this point in the history
…ultiple `BaseModel` with the same names declared in separate classes (pydantic#1912)
  • Loading branch information
jusexton committed Dec 2, 2020
1 parent de0657e commit c62ac53
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/1912-JSextonn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes.
2 changes: 1 addition & 1 deletion pydantic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def get_flat_models_from_models(models: Sequence[Type['BaseModel']]) -> TypeMode


def get_long_model_name(model: TypeModelOrEnum) -> str:
return f'{model.__module__}__{model.__name__}'.replace('.', '__')
return str(model)


def field_type_schema(
Expand Down
22 changes: 19 additions & 3 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,9 @@ class Baz(BaseModel):
Foo: 'Foo',
Bar: 'Bar',
Baz: 'Baz',
ModelA: 'pydantic_schema_test__modulea__modela__Model',
ModelB: 'pydantic_schema_test__moduleb__modelb__Model',
ModelC: 'pydantic_schema_test__modulec__modelc__Model',
ModelA: '<class \'pydantic_schema_test.modulea.modela.Model\'>',
ModelB: '<class \'pydantic_schema_test.moduleb.modelb.Model\'>',
ModelC: '<class \'pydantic_schema_test.modulec.modelc.Model\'>',
}


Expand Down Expand Up @@ -2097,3 +2097,19 @@ class Model(BaseModel):
'properties': {'a': {'title': 'A', 'type': 'string'}},
'required': ['a'],
}


def test_multiple_nested_model_declarations_do_not_raise_error():
class ModelOne(BaseModel):
class NestedModel(BaseModel):
a: float

nested: NestedModel

class ModelTwo(BaseModel):
class NestedModel(BaseModel):
b: float

nested: NestedModel

schema([ModelOne, ModelTwo])

0 comments on commit c62ac53

Please sign in to comment.