Skip to content

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

Closed

Description

@ilya-palachev
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 = []

    Activity

    1. ilya-palachev commented on Feb 18, 2020

      ilya-palachevmannequin
      MannequinAuthor

      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)
    2. tirkarthi commented on Feb 18, 2020

      @tirkarthi
      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

    3. ilya-palachev commented on Feb 19, 2020

      ilya-palachevmannequin
      MannequinAuthor

      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.

    4. tirkarthi commented on Feb 19, 2020

      @tirkarthi
      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]

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      topic-2to3type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions