|
9 | 9 | from mypy.expandtype import expand_type |
10 | 10 | from mypy.maptype import map_instance_to_supertype |
11 | 11 | from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT, VARIANCE_NOT_READY, TypeInfo |
| 12 | +from mypy.disallow_str_iteration_state import ( |
| 13 | + disallow_str_iteration_state, |
| 14 | + STR_ITERATION_PROTOCOL_BASES, |
| 15 | +) |
12 | 16 | from mypy.state import state |
13 | 17 | from mypy.subtypes import ( |
14 | 18 | SubtypeContext, |
@@ -169,12 +173,24 @@ def join_instances_via_supertype(self, t: Instance, s: Instance) -> ProperType: |
169 | 173 | # The definition of "best" may evolve; for now it is the one with |
170 | 174 | # the longest MRO. Ties are broken by using the earlier base. |
171 | 175 |
|
| 176 | + should_skip_str_iteration_protocol_bases = ( |
| 177 | + disallow_str_iteration_state.disallow_str_iteration and t.type.has_base("builtins.str") |
| 178 | + ) |
| 179 | + |
172 | 180 | # Go over both sets of bases in case there's an explicit Protocol base. This is important |
173 | 181 | # to ensure commutativity of join (although in cases where both classes have relevant |
174 | 182 | # Protocol bases this maybe might still not be commutative) |
175 | 183 | base_types: dict[TypeInfo, None] = {} # dict to deduplicate but preserve order |
176 | 184 | for base in t.type.bases: |
| 185 | + if ( |
| 186 | + should_skip_str_iteration_protocol_bases |
| 187 | + and base.type.fullname in STR_ITERATION_PROTOCOL_BASES |
| 188 | + ): |
| 189 | + base_types[object_from_instance(t).type] = None |
| 190 | + continue |
| 191 | + |
177 | 192 | base_types[base.type] = None |
| 193 | + |
178 | 194 | for base in s.type.bases: |
179 | 195 | if base.type.is_protocol and is_subtype(t, base): |
180 | 196 | base_types[base.type] = None |
|
0 commit comments