146146from mypy .state import state
147147from mypy .subtypes import (
148148 find_member ,
149+ infer_class_variances ,
149150 is_callable_compatible ,
150151 is_equivalent ,
151152 is_more_precise ,
@@ -1003,7 +1004,7 @@ def _visit_func_def(self, defn: FuncDef) -> None:
10031004 """Type check a function definition."""
10041005 self .check_func_item (defn , name = defn .name )
10051006 if defn .info :
1006- if not defn .is_dynamic () and not defn . is_overload and not defn .is_decorated :
1007+ if not defn .is_overload and not defn .is_decorated :
10071008 # If the definition is the implementation for an
10081009 # overload, the legality of the override has already
10091010 # been typechecked, and decorated methods will be
@@ -1912,9 +1913,17 @@ def check_method_override(
19121913 Return a list of base classes which contain an attribute with the method name.
19131914 """
19141915 # Check against definitions in base classes.
1916+ check_override_compatibility = defn .name not in (
1917+ "__init__" ,
1918+ "__new__" ,
1919+ "__init_subclass__" ,
1920+ "__post_init__" ,
1921+ ) and (self .options .check_untyped_defs or not defn .is_dynamic ())
19151922 found_method_base_classes : list [TypeInfo ] = []
19161923 for base in defn .info .mro [1 :]:
1917- result = self .check_method_or_accessor_override_for_base (defn , base )
1924+ result = self .check_method_or_accessor_override_for_base (
1925+ defn , base , check_override_compatibility
1926+ )
19181927 if result is None :
19191928 # Node was deferred, we will have another attempt later.
19201929 return None
@@ -1923,7 +1932,10 @@ def check_method_override(
19231932 return found_method_base_classes
19241933
19251934 def check_method_or_accessor_override_for_base (
1926- self , defn : FuncDef | OverloadedFuncDef | Decorator , base : TypeInfo
1935+ self ,
1936+ defn : FuncDef | OverloadedFuncDef | Decorator ,
1937+ base : TypeInfo ,
1938+ check_override_compatibility : bool ,
19271939 ) -> bool | None :
19281940 """Check if method definition is compatible with a base class.
19291941
@@ -1944,10 +1956,8 @@ def check_method_or_accessor_override_for_base(
19441956 if defn .is_final :
19451957 self .check_if_final_var_override_writable (name , base_attr .node , defn )
19461958 found_base_method = True
1947-
1948- # Check the type of override.
1949- if name not in ("__init__" , "__new__" , "__init_subclass__" , "__post_init__" ):
1950- # Check method override
1959+ if check_override_compatibility :
1960+ # Check compatibility of the override signature
19511961 # (__init__, __new__, __init_subclass__ are special).
19521962 if self .check_method_override_for_base_with_name (defn , name , base ):
19531963 return None
@@ -2374,7 +2384,7 @@ def visit_class_def(self, defn: ClassDef) -> None:
23742384 self .allow_abstract_call = old_allow_abstract_call
23752385 # TODO: Apply the sig to the actual TypeInfo so we can handle decorators
23762386 # that completely swap out the type. (e.g. Callable[[Type[A]], Type[B]])
2377- if typ .defn .type_vars :
2387+ if typ .defn .type_vars and typ . defn . type_args is None :
23782388 for base_inst in typ .bases :
23792389 for base_tvar , base_decl_tvar in zip (
23802390 base_inst .args , base_inst .type .defn .type_vars
@@ -2396,6 +2406,7 @@ def visit_class_def(self, defn: ClassDef) -> None:
23962406 self .check_protocol_variance (defn )
23972407 if not defn .has_incompatible_baseclass and defn .info .is_enum :
23982408 self .check_enum (defn )
2409+ infer_class_variances (defn .info )
23992410
24002411 def check_final_deletable (self , typ : TypeInfo ) -> None :
24012412 # These checks are only for mypyc. Only perform some checks that are easier
@@ -2566,6 +2577,9 @@ def check_protocol_variance(self, defn: ClassDef) -> None:
25662577 if they are actually covariant/contravariant, since this may break
25672578 transitivity of subtyping, see PEP 544.
25682579 """
2580+ if defn .type_args is not None :
2581+ # Using new-style syntax (PEP 695), so variance will be inferred
2582+ return
25692583 info = defn .info
25702584 object_type = Instance (info .mro [- 1 ], [])
25712585 tvars = info .defn .type_vars
@@ -3412,8 +3426,8 @@ def check_final(self, s: AssignmentStmt | OperatorAssignmentStmt | AssignmentExp
34123426 if (
34133427 lv .node .final_unset_in_class
34143428 and not lv .node .final_set_in_init
3415- and not self .is_stub
3416- and # It is OK to skip initializer in stub files.
3429+ and not self .is_stub # It is OK to skip initializer in stub files.
3430+ and
34173431 # Avoid extra error messages, if there is no type in Final[...],
34183432 # then we already reported the error about missing r.h.s.
34193433 isinstance (s , AssignmentStmt )
0 commit comments