MCPcopy Index your code
hub / github.com/python/mypy / visit_class_pattern

Method visit_class_pattern

mypy/checkpattern.py:567–714  ·  view source on GitHub ↗
(self, o: ClassPattern)

Source from the content-addressed store, hash-verified

565 return result
566
567 def visit_class_pattern(self, o: ClassPattern) -> PatternType:
568 current_type = get_proper_type(self.type_context[-1])
569
570 #
571 # Check class type
572 #
573 type_info = o.class_ref.node
574 if isinstance(type_info, TypeAlias) and not type_info.no_args:
575 self.msg.fail(message_registry.CLASS_PATTERN_GENERIC_TYPE_ALIAS, o)
576 return self.early_non_match()
577
578 typ = self.chk.expr_checker.accept(o.class_ref)
579 type_ranges = self.get_class_pattern_type_ranges(typ, o)
580 if type_ranges is None:
581 return self.early_non_match()
582 typ = UnionType.make_union([t.item for t in type_ranges])
583
584 new_type, rest_type = self.chk.conditional_types_with_intersection(
585 current_type, type_ranges, o, default=current_type
586 )
587 if is_uninhabited(new_type):
588 return self.early_non_match()
589 # TODO: Do I need this?
590 narrowed_type = narrow_declared_type(current_type, new_type)
591
592 #
593 # Convert positional to keyword patterns
594 #
595 keyword_pairs: list[tuple[str | None, Pattern]] = []
596 match_arg_set: set[str] = set()
597
598 captures: dict[Expression, Type] = {}
599
600 if len(o.positionals) != 0:
601 if self.should_self_match(typ):
602 if len(o.positionals) > 1:
603 self.msg.fail(message_registry.CLASS_PATTERN_TOO_MANY_POSITIONAL_ARGS, o)
604 pattern_type = self.accept(o.positionals[0], narrowed_type)
605 if not is_uninhabited(pattern_type.type):
606 return PatternType(
607 pattern_type.type,
608 join_types(rest_type, pattern_type.rest_type),
609 pattern_type.captures,
610 )
611 captures = pattern_type.captures
612 else:
613 with self.msg.filter_errors() as local_errors:
614 match_args_type = analyze_member_access(
615 "__match_args__",
616 typ,
617 o,
618 is_lvalue=False,
619 is_super=False,
620 is_operator=False,
621 original_type=typ,
622 chk=self.chk,
623 )
624 has_local_errors = local_errors.has_new_errors()

Callers

nothing calls this directly

Calls 15

early_non_matchMethod · 0.95
acceptMethod · 0.95
should_self_matchMethod · 0.95
update_type_mapMethod · 0.95
get_proper_typeFunction · 0.90
narrow_declared_typeFunction · 0.90
join_typesFunction · 0.90
analyze_member_accessFunction · 0.90
AnyTypeClass · 0.90
UninhabitedTypeClass · 0.90
isinstanceFunction · 0.85

Tested by

no test coverage detected