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

2to3 fix_apply tries to fix user-defined apply function calls #83851

Closed
ilya-palachev mannequin opened this issue Feb 18, 2020 · 4 comments
Closed

2to3 fix_apply tries to fix user-defined apply function calls #83851

ilya-palachev mannequin opened this issue Feb 18, 2020 · 4 comments
Labels
topic-2to3 type-bug An unexpected behavior, bug, or error

Comments

@ilya-palachev
Copy link
Mannequin

ilya-palachev mannequin commented Feb 18, 2020

BPO 39670
Nosy @tirkarthi, @ilya-palachev
Superseder
  • bpo-45544: Close 2to3 issues and list them here
  • 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:

    assignee = None
    closed_at = <Date 2021-10-20.22:55:58.117>
    created_at = <Date 2020-02-18.09:10:39.642>
    labels = ['type-bug', 'expert-2to3']
    title = '2to3 fix_apply tries to fix user-defined apply function calls'
    updated_at = <Date 2021-10-20.22:55:58.116>
    user = 'https://github.com/ilya-palachev'

    bugs.python.org fields:

    activity = <Date 2021-10-20.22:55:58.116>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-20.22:55:58.117>
    closer = 'iritkatriel'
    components = ['2to3 (2.x to 3.x conversion tool)']
    creation = <Date 2020-02-18.09:10:39.642>
    creator = 'ilya'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39670
    keywords = []
    message_count = 4.0
    messages = ['362178', '362182', '362243', '362246']
    nosy_count = 2.0
    nosy_names = ['xtreak', 'ilya']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = '45544'
    type = 'behavior'
    url = 'https://bugs.python.org/issue39670'
    versions = []

    @ilya-palachev
    Copy link
    Mannequin Author

    ilya-palachev mannequin commented Feb 18, 2020

    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)

    @ilya-palachev ilya-palachev mannequin added topic-2to3 type-bug An unexpected behavior, bug, or error labels Feb 18, 2020
    @tirkarthi
    Copy link
    Member

    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
    Copy link
    Mannequin Author

    ilya-palachev mannequin commented Feb 19, 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

    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
    Copy link
    Member

    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]

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-2to3 type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants