This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 2to3 fix_apply tries to fix user-defined apply function calls
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder: Close 2to3 issues and list them here
View: 45544
Assigned To: Nosy List: ilya, xtreak
Priority: normal Keywords:

Created on 2020-02-18 09:10 by ilya, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg362178 - (view) Author: (ilya) Date: 2020-02-18 09:10
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)
msg362182 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-02-18 09:43
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
msg362243 - (view) Author: (ilya) Date: 2020-02-19 02:08
> 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.
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.
msg362246 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-02-19 03:46
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
--- /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]
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83851
2021-10-20 22:55:58iritkatrielsetstatus: open -> closed
resolution: wont fix
superseder: Close 2to3 issues and list them here
stage: resolved
2020-02-19 03:46:55xtreaksetmessages: + msg362246
2020-02-19 02:08:26ilyasetmessages: + msg362243
2020-02-18 09:43:02xtreaksetnosy: + xtreak
messages: + msg362182
2020-02-18 09:10:39ilyacreate