|
14 | 14 | from mypy.maptype import map_instance_to_supertype |
15 | 15 | from mypy.meet import narrow_declared_type |
16 | 16 | from 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 |
18 | 18 | from mypy.options import Options |
19 | 19 | from mypy.patterns import ( |
20 | 20 | AsPattern, |
|
37 | 37 | ) |
38 | 38 | from mypy.types import ( |
39 | 39 | AnyType, |
| 40 | + FunctionLike, |
40 | 41 | Instance, |
41 | 42 | LiteralType, |
42 | 43 | NoneType, |
@@ -538,27 +539,20 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType: |
538 | 539 | # Check class type |
539 | 540 | # |
540 | 541 | type_info = o.class_ref.node |
541 | | - if type_info is None: |
542 | | - typ: Type = AnyType(TypeOfAny.from_error) |
543 | | - elif isinstance(type_info, TypeAlias) and not type_info.no_args: |
| 542 | + typ = self.chk.expr_checker.accept(o.class_ref) |
| 543 | + p_typ = get_proper_type(typ) |
| 544 | + if isinstance(type_info, TypeAlias) and not type_info.no_args: |
544 | 545 | self.msg.fail(message_registry.CLASS_PATTERN_GENERIC_TYPE_ALIAS, o) |
545 | 546 | return self.early_non_match() |
546 | | - elif isinstance(type_info, TypeInfo): |
547 | | - typ = fill_typevars_with_any(type_info) |
548 | | - elif isinstance(type_info, TypeAlias): |
549 | | - typ = type_info.target |
550 | | - elif ( |
551 | | - isinstance(type_info, Var) |
552 | | - and type_info.type is not None |
553 | | - and isinstance(get_proper_type(type_info.type), AnyType) |
554 | | - ): |
555 | | - typ = type_info.type |
556 | | - else: |
557 | | - if isinstance(type_info, Var) and type_info.type is not None: |
558 | | - name = type_info.type.str_with_options(self.options) |
559 | | - else: |
560 | | - name = type_info.name |
561 | | - self.msg.fail(message_registry.CLASS_PATTERN_TYPE_REQUIRED.format(name), o) |
| 547 | + elif isinstance(p_typ, FunctionLike) and p_typ.is_type_obj(): |
| 548 | + typ = fill_typevars_with_any(p_typ.type_object()) |
| 549 | + elif not isinstance(p_typ, AnyType): |
| 550 | + self.msg.fail( |
| 551 | + message_registry.CLASS_PATTERN_TYPE_REQUIRED.format( |
| 552 | + typ.str_with_options(self.options) |
| 553 | + ), |
| 554 | + o, |
| 555 | + ) |
562 | 556 | return self.early_non_match() |
563 | 557 |
|
564 | 558 | new_type, rest_type = self.chk.conditional_types_with_intersection( |
@@ -697,6 +691,8 @@ def should_self_match(self, typ: Type) -> bool: |
697 | 691 | typ = get_proper_type(typ) |
698 | 692 | if isinstance(typ, TupleType): |
699 | 693 | typ = typ.partial_fallback |
| 694 | + if isinstance(typ, AnyType): |
| 695 | + return False |
700 | 696 | if isinstance(typ, Instance) and typ.type.get("__match_args__") is not None: |
701 | 697 | # Named tuples and other subtypes of builtins that define __match_args__ |
702 | 698 | # should not self match. |
|
0 commit comments