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
2to3 fix_apply tries to fix user-defined apply function calls #83851
Comments
Consider the following code: def apply(a, b):
print(a)
print(b)
apply(1, 1) 2to3 suggests to fix it as follows: --- a.py (original)
+++ a.py (refactored)
@@ -2,4 +2,4 @@
print(a)
print(b)
-apply(1, 1)
+(1)(*1) |
apply was a builtin in Python 2 and not sure 2to3 can differentiate between user defined functions that shadow builtins. https://docs.python.org/3.8/library/2to3.html#2to3fixer-apply . Removes usage of apply(). For example apply(function, *args, **kwargs) is converted to function(*args, **kwargs). You can skip the apply fixer: 2to3 -x apply /tmp/bar.py |
The problem is that the code is valid both for Python2 and Python3 (for Python3, there is even no builtin shadowing, because there is no apply builtin actually), and fix_apply breaks it. |
The fixers are supposed to be executed on Python 2 files where apply was a builtin and was shadowed. So from the context of the fixer it tries to make the modification and it cannot distinguish that it's a builtin or user-defined call. In Python 3 the apply function can be defined by the user and 2to3 fixer doesn't make sense to be executed on Python 3 files. filter is another example where the call is transformed into a list comprehension by 2to3 but by the issue it shouldn't be done because filter is a user-defined function though it's a builtin in Python 2. def filter(func, iterable):
pass
filter(lambda x: x % 2 == 0, range(10)) RefactoringTool: Refactored /tmp/foo.py -filter(lambda x: x % 2 == 0, range(10)) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: