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 29, 2020
1 parent de0657e commit d1b69f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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 f'{model.__module__}__{model.__qualname__}'.replace('.', '__')


def field_type_schema(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
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 d1b69f0

Please sign in to comment.