You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
2to3 fix_apply tries to fix user-defined apply function calls #83851
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.
I'm testing the quality of 2to3 fixers and found this issue.
I know that it's possible to switch this fixer off, but it doesn't seem to be a proper solution because any other bug could have the same answer.
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.
Activity
ilya-palachev commentedon Feb 18, 2020
Consider the following code:
2to3 suggests to fix it as follows:
tirkarthi commentedon Feb 18, 2020
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
ilya-palachev commentedon Feb 19, 2020
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.
I'm testing the quality of 2to3 fixers and found this issue.
I know that it's possible to switch this fixer off, but it doesn't seem to be a proper solution because any other bug could have the same answer.
tirkarthi commentedon Feb 19, 2020
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.
RefactoringTool: Refactored /tmp/foo.py
--- /tmp/foo.py (original)
+++ /tmp/foo.py (refactored)
@@ -1,4 +1,4 @@
def filter(func, iterable):
pass
-filter(lambda x: x % 2 == 0, range(10))
+[x for x in range(10) if x % 2 == 0]