Skip to content

Commit

Permalink
Add a check on the existence of __qualname__ (#8642)
Browse files Browse the repository at this point in the history
Co-authored-by: anci3ntr0ck <hu_shuo@venustech.com.cn>
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 26, 2024
1 parent 3810c7b commit 57922ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -334,6 +334,7 @@ def inspect_namespace( # noqa C901
elif (
isinstance(value, type)
and value.__module__ == namespace['__module__']
and '__qualname__' in namespace
and value.__qualname__.startswith(namespace['__qualname__'])
):
# `value` is a nested type defined in this namespace; don't error
Expand Down
9 changes: 9 additions & 0 deletions tests/test_create_model.py
Expand Up @@ -601,3 +601,12 @@ class Y:
)
Z = create_model('Z', y=(module.Y, ...))
assert Z(y={'x': {}}).y is not None


def test_type_field_in_the_same_module():
class A:
pass

B = create_model('B', a_cls=(type, A))
b = B()
assert b.a_cls == A

0 comments on commit 57922ba

Please sign in to comment.