(self, o: OrPattern)
| 152 | return PatternType(typ, rest_type, type_map) |
| 153 | |
| 154 | def visit_or_pattern(self, o: OrPattern) -> PatternType: |
| 155 | current_type = self.type_context[-1] |
| 156 | |
| 157 | # |
| 158 | # Check all the subpatterns |
| 159 | # |
| 160 | pattern_types = [] |
| 161 | for pattern in o.patterns: |
| 162 | pattern_type = self.accept(pattern, current_type) |
| 163 | pattern_types.append(pattern_type) |
| 164 | if not is_uninhabited(pattern_type.type): |
| 165 | current_type = pattern_type.rest_type |
| 166 | |
| 167 | # |
| 168 | # Collect the final type |
| 169 | # |
| 170 | types = [] |
| 171 | for pattern_type in pattern_types: |
| 172 | if not is_uninhabited(pattern_type.type): |
| 173 | types.append(pattern_type.type) |
| 174 | |
| 175 | # |
| 176 | # Check the capture types |
| 177 | # |
| 178 | capture_types: dict[Var, list[tuple[Expression, Type]]] = defaultdict(list) |
| 179 | # Collect captures from the first subpattern |
| 180 | for expr, typ in pattern_types[0].captures.items(): |
| 181 | node = get_var(expr) |
| 182 | capture_types[node].append((expr, typ)) |
| 183 | |
| 184 | # Check if other subpatterns capture the same names |
| 185 | for i, pattern_type in enumerate(pattern_types[1:]): |
| 186 | vars = {get_var(expr) for expr, _ in pattern_type.captures.items()} |
| 187 | if capture_types.keys() != vars: |
| 188 | self.msg.fail(message_registry.OR_PATTERN_ALTERNATIVE_NAMES, o.patterns[i]) |
| 189 | for expr, typ in pattern_type.captures.items(): |
| 190 | node = get_var(expr) |
| 191 | capture_types[node].append((expr, typ)) |
| 192 | |
| 193 | captures: dict[Expression, Type] = {} |
| 194 | for capture_list in capture_types.values(): |
| 195 | typ = UninhabitedType() |
| 196 | for _, other in capture_list: |
| 197 | typ = make_simplified_union([typ, other]) |
| 198 | |
| 199 | captures[capture_list[0][0]] = typ |
| 200 | |
| 201 | union_type = make_simplified_union(types) |
| 202 | return PatternType(union_type, current_type, captures) |
| 203 | |
| 204 | def visit_value_pattern(self, o: ValuePattern) -> PatternType: |
| 205 | current_type = self.type_context[-1] |
nothing calls this directly
no test coverage detected