From 42144f90cfaee26822145123a4e96e5e66dffb87 Mon Sep 17 00:00:00 2001 From: Hana Joo Date: Tue, 26 Nov 2024 09:59:45 -0800 Subject: [PATCH] Avoid checking types of Callable when both type objects are pointing to the same reference. This is a small check which would speed up cases drastically such as recursive generics with unions, since the type is equal but Pytype holds the expanded union type and attempts to go in deep to figure out each individual type matches. PiperOrigin-RevId: 700376520 --- pytype/matcher.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pytype/matcher.py b/pytype/matcher.py index da2107155..870f4139e 100644 --- a/pytype/matcher.py +++ b/pytype/matcher.py @@ -1550,6 +1550,11 @@ def _match_callable_args_against_callable( ): # TODO(mdemello): Unify with _match_signature_args_against_callable() param_match = self._get_param_matcher(other_type) + # Checking type against the same type should happen quite often. This check + # helps avoiding near-infinite type expansion for recursive generic types + # which are identical. + if left is other_type: + return subst for i in range(left.num_args): left_arg = left.formal_type_parameters[i] right_arg = other_type.formal_type_parameters[i]