Skip to content

Commit

Permalink
[cherry-pick] Fix mypy plugin for 1.4.0 (#5927) (#5928)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jun 1, 2023
1 parent b9a316e commit ca10302
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions changes/5928-cdce8p.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix mypy plugin for v1.4.0
23 changes: 20 additions & 3 deletions pydantic/mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,32 @@ def add_construct_method(self, fields: List['PydanticModelField']) -> None:
obj_type = ctx.api.named_type(f'{BUILTINS_NAME}.object')
self_tvar_name = '_PydanticBaseModel' # Make sure it does not conflict with other names in the class
tvar_fullname = ctx.cls.fullname + '.' + self_tvar_name
tvd = TypeVarDef(self_tvar_name, tvar_fullname, -1, [], obj_type)
self_tvar_expr = TypeVarExpr(self_tvar_name, tvar_fullname, [], obj_type)
if MYPY_VERSION_TUPLE >= (1, 4):
tvd = TypeVarType(
self_tvar_name,
tvar_fullname,
-1,
[],
obj_type,
AnyType(TypeOfAny.from_omitted_generics), # type: ignore[arg-type]
)
self_tvar_expr = TypeVarExpr(
self_tvar_name,
tvar_fullname,
[],
obj_type,
AnyType(TypeOfAny.from_omitted_generics), # type: ignore[arg-type]
)
else:
tvd = TypeVarDef(self_tvar_name, tvar_fullname, -1, [], obj_type)
self_tvar_expr = TypeVarExpr(self_tvar_name, tvar_fullname, [], obj_type)
ctx.cls.info.names[self_tvar_name] = SymbolTableNode(MDEF, self_tvar_expr)

# Backward-compatible with TypeVarDef from Mypy 0.910.
if isinstance(tvd, TypeVarType):
self_type = tvd
else:
self_type = TypeVarType(tvd) # type: ignore[call-arg]
self_type = TypeVarType(tvd)

add_method(
ctx,
Expand Down

0 comments on commit ca10302

Please sign in to comment.