Skip to content

Commit

Permalink
Fix performance in union subtyping (#15104)
Browse files Browse the repository at this point in the history
This is a performance optimisation for subtyping between two unions that
are largely the same.

Fixes #14034

This makes @adriangb's example in
#14034 (comment)
finish basically instantly. I could add it as a unit test?

Type checking pydantic core is still not fast — takes like four or five
minutes with uncompiled mypy — but at least it's now feasible. I think
there's room for doing some optimisation in make_simplified_union that
would improve this.
  • Loading branch information
hauntsaninja authored and wesleywright committed Apr 24, 2023
1 parent 320b883 commit d887e9c
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,9 @@ def visit_union_type(self, left: UnionType) -> bool:

for item in _flattened(self.right.relevant_items()):
p_item = get_proper_type(item)
if isinstance(p_item, LiteralType):
fast_check.add(p_item)
elif isinstance(p_item, Instance):
if p_item.last_known_value is None:
fast_check.add(p_item)
else:
fast_check.add(p_item.last_known_value)
fast_check.add(p_item)
if isinstance(p_item, Instance) and p_item.last_known_value is not None:
fast_check.add(p_item.last_known_value)

for item in left.relevant_items():
p_item = get_proper_type(item)
Expand Down

0 comments on commit d887e9c

Please sign in to comment.