Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Another micro-optimization; postpone enabling in self-check
  • Loading branch information
ilevkivskyi committed Feb 20, 2026
commit f6966e14e21a565b522768b9df29cc434366dfbe
25 changes: 16 additions & 9 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,20 @@ def update_from_options(self, frames: list[Frame]) -> bool:
keys = list(set(keys))
for key in keys:
current_value = self._get(key)
resulting_values = [f.types.get(key, current_value) for f in frames]
all_resulting_values = [f.types.get(key, current_value) for f in frames]
# Keys can be narrowed using two different semantics. The new semantics
# is enabled for plain variables when bind_all is true, and it allows
# variable types to be widened using subsequent assignments. This is
# tricky to support for instance attributes (primarily due to deferrals),
# so we don't use it for them.
old_semantics = not self.bind_all or extract_var_from_literal_hash(key) is None
if old_semantics and any(x is None for x in resulting_values):
if old_semantics and any(x is None for x in all_resulting_values):
# We didn't know anything about key before
# (current_value must be None), and we still don't
# know anything about key in at least one possible frame.
continue

resulting_values = [x for x in resulting_values if x is not None]
resulting_values = [x for x in all_resulting_values if x is not None]

if all_reachable and all(not x.from_assignment for x in resulting_values):
# Do not synthesize a new type if we encountered a conditional block
Expand All @@ -335,19 +335,26 @@ def update_from_options(self, frames: list[Frame]) -> bool:
# or `isinstance` does not change the type of the value.
continue

current_type = resulting_values[0]
assert current_type is not None
type = current_type.type
seen_types = set()
resulting_types = []
for rv in resulting_values:
assert rv is not None
if rv.type in seen_types:
continue
resulting_types.append(rv.type)
seen_types.add(rv.type)

type = resulting_types[0]
declaration_type = get_proper_type(self.declarations.get(key))
if isinstance(declaration_type, AnyType):
# At this point resulting values can't contain None, see continue above
if not all(is_same_type(type, t.type) for t in resulting_values[1:]):
if not all(is_same_type(type, t) for t in resulting_types[1:]):
type = AnyType(TypeOfAny.from_another_any, source_any=declaration_type)
else:
possible_types = []
for t in resulting_values:
for t in resulting_types:
assert t is not None
possible_types.append(t.type)
possible_types.append(t)
if len(possible_types) == 1:
# This is to avoid calling get_proper_type() unless needed, as this may
# interfere with our (hacky) TypeGuard support.
Expand Down
2 changes: 1 addition & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def visit_sequence_pattern(self, o: SequencePattern) -> PatternType:
if len(inner_types) - 1 > required_patterns and star_position is None:
return self.early_non_match()
elif isinstance(current_type, AnyType):
inner_type = AnyType(TypeOfAny.from_another_any, current_type)
inner_type: Type = AnyType(TypeOfAny.from_another_any, current_type)
inner_types = [inner_type] * len(o.patterns)
elif isinstance(current_type, Instance) and self.chk.type_is_iterable(current_type):
inner_type = self.chk.iterable_item_type(current_type, o)
Expand Down
2 changes: 0 additions & 2 deletions mypy_bootstrap.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ strict = True
warn_unused_ignores = False
show_traceback = True
always_true = MYPYC
local_partial_types = True
allow_redefinition_new = True

[mypy-mypy.visitor]
# See docstring for NodeVisitor for motivation.
Expand Down
1 change: 0 additions & 1 deletion mypy_self_check.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ enable_incomplete_feature = PreciseTupleTypes
show_error_code_links = True
warn_unreachable = True
sqlite_cache = True
allow_redefinition_new = True