2626from typing_extensions import TypeAlias as _TypeAlias
2727
2828import mypy .checkexpr
29- from mypy import errorcodes as codes , message_registry , nodes , operators
29+ from mypy import errorcodes as codes , join , message_registry , nodes , operators
3030from mypy .binder import ConditionalTypeBinder , Frame , get_declaration
3131from mypy .checkmember import (
3232 MemberContext ,
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 ,
@@ -680,6 +681,11 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
680681 inner_type = get_proper_type (inner_type )
681682 outer_type : CallableType | None = None
682683 if inner_type is not None and not isinstance (inner_type , AnyType ):
684+ if isinstance (inner_type , TypeType ):
685+ if isinstance (inner_type .item , Instance ):
686+ inner_type = expand_type_by_instance (
687+ type_object_type (inner_type .item .type , self .named_type ), inner_type .item
688+ )
683689 if isinstance (inner_type , CallableType ):
684690 outer_type = inner_type
685691 elif isinstance (inner_type , Instance ):
@@ -698,6 +704,21 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
698704 )
699705 if isinstance (inner_call , CallableType ):
700706 outer_type = inner_call
707+ elif isinstance (inner_type , UnionType ):
708+ union_type = make_simplified_union (inner_type .items )
709+ if isinstance (union_type , UnionType ):
710+ items = []
711+ for item in union_type .items :
712+ callable_item = self .extract_callable_type (item , ctx )
713+ if callable_item is None :
714+ break
715+ items .append (callable_item )
716+ else :
717+ joined_type = get_proper_type (join .join_type_list (items ))
718+ if isinstance (joined_type , CallableType ):
719+ outer_type = joined_type
720+ else :
721+ return self .extract_callable_type (union_type , ctx )
701722 if outer_type is None :
702723 self .msg .not_callable (inner_type , ctx )
703724 return outer_type
@@ -1003,7 +1024,7 @@ def _visit_func_def(self, defn: FuncDef) -> None:
10031024 """Type check a function definition."""
10041025 self .check_func_item (defn , name = defn .name )
10051026 if defn .info :
1006- if not defn .is_dynamic () and not defn . is_overload and not defn .is_decorated :
1027+ if not defn .is_overload and not defn .is_decorated :
10071028 # If the definition is the implementation for an
10081029 # overload, the legality of the override has already
10091030 # been typechecked, and decorated methods will be
@@ -1912,9 +1933,17 @@ def check_method_override(
19121933 Return a list of base classes which contain an attribute with the method name.
19131934 """
19141935 # Check against definitions in base classes.
1936+ check_override_compatibility = defn .name not in (
1937+ "__init__" ,
1938+ "__new__" ,
1939+ "__init_subclass__" ,
1940+ "__post_init__" ,
1941+ ) and (self .options .check_untyped_defs or not defn .is_dynamic ())
19151942 found_method_base_classes : list [TypeInfo ] = []
19161943 for base in defn .info .mro [1 :]:
1917- result = self .check_method_or_accessor_override_for_base (defn , base )
1944+ result = self .check_method_or_accessor_override_for_base (
1945+ defn , base , check_override_compatibility
1946+ )
19181947 if result is None :
19191948 # Node was deferred, we will have another attempt later.
19201949 return None
@@ -1923,7 +1952,10 @@ def check_method_override(
19231952 return found_method_base_classes
19241953
19251954 def check_method_or_accessor_override_for_base (
1926- self , defn : FuncDef | OverloadedFuncDef | Decorator , base : TypeInfo
1955+ self ,
1956+ defn : FuncDef | OverloadedFuncDef | Decorator ,
1957+ base : TypeInfo ,
1958+ check_override_compatibility : bool ,
19271959 ) -> bool | None :
19281960 """Check if method definition is compatible with a base class.
19291961
@@ -1944,10 +1976,8 @@ def check_method_or_accessor_override_for_base(
19441976 if defn .is_final :
19451977 self .check_if_final_var_override_writable (name , base_attr .node , defn )
19461978 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
1979+ if check_override_compatibility :
1980+ # Check compatibility of the override signature
19511981 # (__init__, __new__, __init_subclass__ are special).
19521982 if self .check_method_override_for_base_with_name (defn , name , base ):
19531983 return None
@@ -2374,7 +2404,7 @@ def visit_class_def(self, defn: ClassDef) -> None:
23742404 self .allow_abstract_call = old_allow_abstract_call
23752405 # TODO: Apply the sig to the actual TypeInfo so we can handle decorators
23762406 # that completely swap out the type. (e.g. Callable[[Type[A]], Type[B]])
2377- if typ .defn .type_vars :
2407+ if typ .defn .type_vars and typ . defn . type_args is None :
23782408 for base_inst in typ .bases :
23792409 for base_tvar , base_decl_tvar in zip (
23802410 base_inst .args , base_inst .type .defn .type_vars
@@ -2396,6 +2426,7 @@ def visit_class_def(self, defn: ClassDef) -> None:
23962426 self .check_protocol_variance (defn )
23972427 if not defn .has_incompatible_baseclass and defn .info .is_enum :
23982428 self .check_enum (defn )
2429+ infer_class_variances (defn .info )
23992430
24002431 def check_final_deletable (self , typ : TypeInfo ) -> None :
24012432 # These checks are only for mypyc. Only perform some checks that are easier
@@ -2566,6 +2597,9 @@ def check_protocol_variance(self, defn: ClassDef) -> None:
25662597 if they are actually covariant/contravariant, since this may break
25672598 transitivity of subtyping, see PEP 544.
25682599 """
2600+ if defn .type_args is not None :
2601+ # Using new-style syntax (PEP 695), so variance will be inferred
2602+ return
25692603 info = defn .info
25702604 object_type = Instance (info .mro [- 1 ], [])
25712605 tvars = info .defn .type_vars
@@ -3412,8 +3446,8 @@ def check_final(self, s: AssignmentStmt | OperatorAssignmentStmt | AssignmentExp
34123446 if (
34133447 lv .node .final_unset_in_class
34143448 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.
3449+ and not self .is_stub # It is OK to skip initializer in stub files.
3450+ and
34173451 # Avoid extra error messages, if there is no type in Final[...],
34183452 # then we already reported the error about missing r.h.s.
34193453 isinstance (s , AssignmentStmt )
0 commit comments