This commit is contained in:
Iliyan Angelov
2025-12-01 06:50:10 +02:00
parent 91f51bc6fe
commit 62c1fe5951
4682 changed files with 544807 additions and 31208 deletions

View File

@@ -57,6 +57,7 @@ from mypy.types import (
Type,
TypeOfAny,
TypeType,
TypeVarId,
TypeVarType,
UnionType,
get_proper_type,
@@ -65,7 +66,7 @@ from mypy.typevars import fill_typevars
from mypy.util import get_unique_redefinition_name
from mypy.version import __version__ as mypy_version
from pydantic.utils import is_valid_field
from pydantic.v1.utils import is_valid_field
try:
from mypy.types import TypeVarDef # type: ignore[attr-defined]
@@ -208,14 +209,14 @@ class PydanticPlugin(Plugin):
default_factory_type = default_factory_type.items()[0] # type: ignore[operator]
if isinstance(default_factory_type, CallableType):
ret_type = default_factory_type.ret_type
# mypy doesn't think `ret_type` has `args`, you'd think mypy should know,
# add this check in case it varies by version
args = getattr(ret_type, 'args', None)
if args:
if all(isinstance(arg, TypeVarType) for arg in args):
# Looks like the default factory is a type like `list` or `dict`, replace all args with `Any`
ret_type.args = tuple(default_any_type for _ in args) # type: ignore[attr-defined]
ret_type = get_proper_type(default_factory_type.ret_type)
if (
isinstance(ret_type, Instance)
and ret_type.args
and all(isinstance(arg, TypeVarType) for arg in ret_type.args)
):
# Looks like the default factory is a type like `list` or `dict`, replace all args with `Any`
ret_type = ret_type.copy_modified(args=[default_any_type] * len(ret_type.args))
return ret_type
return default_any_type
@@ -498,7 +499,11 @@ class PydanticModelTransformer:
tvd = TypeVarType(
self_tvar_name,
tvar_fullname,
-1,
(
TypeVarId(-1, namespace=ctx.cls.fullname + '.construct')
if MYPY_VERSION_TUPLE >= (1, 11)
else TypeVarId(-1)
),
[],
obj_type,
AnyType(TypeOfAny.from_omitted_generics), # type: ignore[arg-type]
@@ -858,9 +863,9 @@ def add_method(
arg_kinds.append(arg.kind)
function_type = ctx.api.named_type(f'{BUILTINS_NAME}.function')
signature = CallableType(arg_types, arg_kinds, arg_names, return_type, function_type)
if tvar_def:
signature.variables = [tvar_def]
signature = CallableType(
arg_types, arg_kinds, arg_names, return_type, function_type, variables=[tvar_def] if tvar_def else None
)
func = FuncDef(name, args, Block([PassStmt()]))
func.info = info