Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid the use of a context manager in hot code path #14331

Merged
merged 2 commits into from
Dec 21, 2022
Merged
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
14 changes: 6 additions & 8 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,8 +1583,7 @@ def infer_arg_types_in_empty_context(self, args: list[Expression]) -> list[Type]
res.append(arg_type)
return res

@contextmanager
def allow_unions(self, type_context: Type) -> Iterator[None]:
def infer_more_unions_for_recursive_type(self, type_context: Type) -> bool:
# This is a hack to better support inference for recursive types.
# When the outer context for a function call is known to be recursive,
# we solve type constraints inferred from arguments using unions instead
Expand All @@ -1594,10 +1593,7 @@ def allow_unions(self, type_context: Type) -> Iterator[None]:
old = type_state.infer_unions
if has_recursive_types(type_context):
type_state.infer_unions = True
try:
yield
finally:
type_state.infer_unions = old
return old

def infer_arg_types_in_context(
self,
Expand All @@ -1618,8 +1614,10 @@ def infer_arg_types_in_context(
for i, actuals in enumerate(formal_to_actual):
for ai in actuals:
if not arg_kinds[ai].is_star():
with self.allow_unions(callee.arg_types[i]):
res[ai] = self.accept(args[ai], callee.arg_types[i])
arg_type = callee.arg_types[i]
old = self.infer_more_unions_for_recursive_type(arg_type)
res[ai] = self.accept(args[ai], arg_type)
type_state.infer_unions = old

# Fill in the rest of the argument types.
for i, t in enumerate(res):
Expand Down