1414from  mypy .maptype  import  map_instance_to_supertype 
1515from  mypy .meet  import  narrow_declared_type 
1616from  mypy .messages  import  MessageBuilder 
17- from  mypy .nodes  import  ARG_POS , Context , Expression , NameExpr , TypeAlias , TypeInfo ,  Var 
17+ from  mypy .nodes  import  ARG_POS , Context , Expression , NameExpr , TypeAlias , Var 
1818from  mypy .options  import  Options 
1919from  mypy .patterns  import  (
2020    AsPattern ,
3737)
3838from  mypy .types  import  (
3939    AnyType ,
40+     FunctionLike ,
4041    Instance ,
4142    LiteralType ,
4243    NoneType ,
@@ -539,21 +540,13 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
539540        # Check class type 
540541        # 
541542        type_info  =  o .class_ref .node 
542-         if   type_info   is   None : 
543-              typ :  Type   =   AnyType ( TypeOfAny . from_error )
544-         elif  isinstance (type_info , TypeAlias ) and  not  type_info .no_args :
543+         typ   =   self . chk . expr_checker . accept ( o . class_ref ) 
544+         p_typ   =   get_proper_type ( typ )
545+         if  isinstance (type_info , TypeAlias ) and  not  type_info .no_args :
545546            self .msg .fail (message_registry .CLASS_PATTERN_GENERIC_TYPE_ALIAS , o )
546547            return  self .early_non_match ()
547-         elif  isinstance (type_info , TypeInfo ):
548-             typ  =  fill_typevars_with_any (type_info )
549-         elif  isinstance (type_info , TypeAlias ):
550-             typ  =  type_info .target 
551-         elif  (
552-             isinstance (type_info , Var )
553-             and  type_info .type  is  not   None 
554-             and  isinstance (get_proper_type (type_info .type ), AnyType )
555-         ):
556-             typ  =  type_info .type 
548+         elif  isinstance (p_typ , FunctionLike ) and  p_typ .is_type_obj ():
549+             typ  =  fill_typevars_with_any (p_typ .type_object ())
557550        elif  (
558551            isinstance (type_info , Var )
559552            and  type_info .type  is  not   None 
@@ -563,12 +556,13 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
563556            fallback  =  self .chk .named_type ("builtins.function" )
564557            any_type  =  AnyType (TypeOfAny .unannotated )
565558            typ  =  callable_with_ellipsis (any_type , ret_type = any_type , fallback = fallback )
566-         else :
567-             if  isinstance (type_info , Var ) and  type_info .type  is  not   None :
568-                 name  =  type_info .type .str_with_options (self .options )
569-             else :
570-                 name  =  type_info .name 
571-             self .msg .fail (message_registry .CLASS_PATTERN_TYPE_REQUIRED .format (name ), o )
559+         elif  not  isinstance (p_typ , AnyType ):
560+             self .msg .fail (
561+                 message_registry .CLASS_PATTERN_TYPE_REQUIRED .format (
562+                     typ .str_with_options (self .options )
563+                 ),
564+                 o ,
565+             )
572566            return  self .early_non_match ()
573567
574568        new_type , rest_type  =  self .chk .conditional_types_with_intersection (
@@ -707,6 +701,8 @@ def should_self_match(self, typ: Type) -> bool:
707701        typ  =  get_proper_type (typ )
708702        if  isinstance (typ , TupleType ):
709703            typ  =  typ .partial_fallback 
704+         if  isinstance (typ , AnyType ):
705+             return  False 
710706        if  isinstance (typ , Instance ) and  typ .type .get ("__match_args__" ) is  not   None :
711707            # Named tuples and other subtypes of builtins that define __match_args__ 
712708            # should not self match. 
0 commit comments