Skip to content

[mypyc] Avoid mutating the AST when processing singledispatch register calls #10779

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

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Changes from all commits
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
12 changes: 8 additions & 4 deletions mypyc/irbuild/prebuildvisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,25 @@ def visit_decorator(self, dec: Decorator) -> None:
# Property setters are not treated as decorated methods.
self.prop_setters.add(dec.func)
else:
decorators_to_store = dec.decorators.copy()
removed: List[int] = []
for i, d in enumerate(dec.decorators):
for i, d in enumerate(decorators_to_store):
impl = get_singledispatch_register_call_info(d, dec.func)
if impl is not None:
self.singledispatch_impls[impl.singledispatch_func].append(
(impl.dispatch_type, dec.func))
removed.append(i)
# calling register on a function that tries to dispatch based on type annotations
# raises a TypeError because compiled functions don't have an __annotations__
# attribute
for i in reversed(removed):
del dec.decorators[i]
del decorators_to_store[i]
# if the only decorators are register calls, we shouldn't treat this
# as a decorated function because there aren't any decorators to apply
if not dec.decorators:
if not decorators_to_store:
return

self.funcs_to_decorators[dec.func] = dec.decorators
self.funcs_to_decorators[dec.func] = decorators_to_store
super().visit_decorator(dec)

def visit_func_def(self, fdef: FuncItem) -> None:
Expand Down