Skip to content

Commit f5d1c87

Browse files
committed
Fix crash when expanding root type in the mypy plugin
Backport of: #11735
1 parent c80bb35 commit f5d1c87

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pydantic/mypy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from collections.abc import Iterator
77
from configparser import ConfigParser
8-
from typing import Any, Callable, cast
8+
from typing import Any, Callable
99

1010
from mypy.errorcodes import ErrorCode
1111
from mypy.expandtype import expand_type, expand_type_by_instance
@@ -339,7 +339,10 @@ def expand_type(
339339
if include_root_type and isinstance(expanded_type, Instance) and is_root_model(expanded_type.type):
340340
# When a root model is used as a field, Pydantic allows both an instance of the root model
341341
# as well as instances of the `root` field type:
342-
root_type = cast(Type, expanded_type.type['root'].type)
342+
root_type = expanded_type.type['root'].type
343+
if root_type is None:
344+
# Happens if the hint for 'root' has unsolved forward references
345+
return expanded_type
343346
expanded_root_type = expand_type_by_instance(root_type, expanded_type)
344347
expanded_type = UnionType([expanded_type, expanded_root_type])
345348
return expanded_type

0 commit comments

Comments
 (0)