Skip to content

2to3 replaces "arbitrary" variables #48673

Closed

Description

@loewis
mannequin
BPO 4423
Nosy @loewis, @benjaminp

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 2008-11-25.22:46:39.278>
created_at = <Date 2008-11-25.03:35:00.119>
labels = ['expert-2to3']
title = '2to3 replaces "arbitrary" variables'
updated_at = <Date 2008-11-26.19:30:24.553>
user = 'https://github.com/loewis'

bugs.python.org fields:

activity = <Date 2008-11-26.19:30:24.553>
actor = 'benjamin.peterson'
assignee = 'none'
closed = True
closed_date = <Date 2008-11-25.22:46:39.278>
closer = 'benjamin.peterson'
components = ['2to3 (2.x to 3.x conversion tool)']
creation = <Date 2008-11-25.03:35:00.119>
creator = 'loewis'
dependencies = []
files = []
hgrepos = []
issue_num = 4423
keywords = []
message_count = 11.0
messages = ['76394', '76395', '76396', '76397', '76435', '76438', '76440', '76447', '76476', '76482', '76483']
nosy_count = 2.0
nosy_names = ['loewis', 'benjamin.peterson']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue4423'
versions = []

Activity

  1. loewis commented on Nov 25, 2008

    loewismannequin
    MannequinAuthor

    In 2to3 (from the py3k branch, as of r67372) translates the fragment

    def f():
        commands = foo()
        commands.sort()

    to

    def f():
        commands = foo()
        subprocess.sort()
  2. benjaminp commented on Nov 25, 2008

    @benjaminp
    Contributor

    Yes, this a known issue. You can currently circumvent it by disabling
    the imports fixer.

  3. loewis commented on Nov 25, 2008

    loewismannequin
    MannequinAuthor

    Are there any plans to improve that? In the specific case, it would help
    if commands gets only replaced if an import of commands appears "in
    scope" (or, if that is too difficult, anywhere in the file). I think
    2to3 should create a list of all names that get imported anywhere, and
    compare candidates for renaming against this list.

    Also, I tried to work-around by just deleting "commands" from MAPPING
    (nobody uses the commands module, anyway); this unfortunately failed
    because PATTERN already had the name compiled in. It would be helpful if
    PATTERN was computed more lazily, e.g. by overriding compile_pattern.
    (I then worked around by regenerating PATTERN after monkey-patching
    MAPPING).

  4. benjaminp commented on Nov 25, 2008

    @benjaminp
    Contributor

    This bug certainly has been reported before, so I'll see if I can find
    time for it.

  5. benjaminp commented on Nov 25, 2008

    @benjaminp
    Contributor

    I'm not sure what you mean about MAPPING being compiled in. It is
    regenerated every time 2to3 is run.

    Anyway, I fixed the replacement problem in r67386.

  6. loewis commented on Nov 25, 2008

    loewismannequin
    MannequinAuthor

    I'm not sure what you mean about MAPPING being compiled in. It is
    regenerated every time 2to3 is run.

    This code does not work:

        from lib2to3.fixes import fix_imports
        del fix_imports.MAPPING['commands']

    when followed by an attempt to actually run the fixer. Instead, I need
    to write

        from lib2to3.fixes import fix_imports
        del fix_imports.MAPPING['commands']
        fix_imports.FixImports.PATTERN="|".join(fix_imports.build_pattern())

    Anyway, I fixed the replacement problem in r67386.

    Thanks! It still transforms

    def g():
        import commands
    def f():
        commands = foo()
        commands.sort()

    but I think this is ok; people just shouldn't write such code in the
    first place (and the resulting code does work correctly - just with
    a strangely-renamed local variable).

  7. benjaminp commented on Nov 25, 2008

    @benjaminp
    Contributor

    On Tue, Nov 25, 2008 at 5:10 PM, Martin v. Löwis <report@bugs.python.org> wrote:

    Martin v. Löwis <martin@v.loewis.de> added the comment:

    > I'm not sure what you mean about MAPPING being compiled in. It is
    > regenerated every time 2to3 is run.

    This code does not work:

    from lib2to3.fixes import fix_imports
    del fix_imports.MAPPING['commands']

    when followed by an attempt to actually run the fixer. Instead, I need
    to write

    from lib2to3.fixes import fix_imports
    del fix_imports.MAPPING['commands']
    fix_imports.FixImports.PATTERN="|".join(fix_imports.build_pattern())

    This is true of many fixers. What is the use case?

    > Anyway, I fixed the replacement problem in r67386.

    Thanks! It still transforms

    def g():
    import commands
    def f():
    commands = foo()
    commands.sort()

    but I think this is ok; people just shouldn't write such code in the
    first place (and the resulting code does work correctly - just with
    a strangely-renamed local variable).

    This also won't work:

    def f():
        commands.call() # This fails to be fixed.
    
    import commands

    Is that reasonable also?

    Scoping isn't something that 2to3 does well, and we'd probably have to
    add a whole symtable analyzer to fix a few obscure bugs like above.

  8. loewis commented on Nov 26, 2008

    loewismannequin
    MannequinAuthor

    This is true of many fixers. What is the use case?

    In this case, I wanted to work around the fixer working incorrectly
    for commands, but still have it continue to work for all the other
    modules it fixes. The context is the setup.py script of Django.

    Is that reasonable also?

    Sure! People will accept that they have to reorganize the source
    before 2to3 can work correctly. More precisely, if they follow the
    "convert once, cut the bridges" approach, they can just manually
    unfix the few places that 2to3 got wrong. If they follow the
    "single source" approach, they will accept that they have to
    reformulate.

  9. benjaminp commented on Nov 26, 2008

    @benjaminp
    Contributor

    On Wed, Nov 26, 2008 at 12:45 AM, Martin v. Löwis
    <report@bugs.python.org> wrote:

    Martin v. Löwis <martin@v.loewis.de> added the comment:

    > This is true of many fixers. What is the use case?

    In this case, I wanted to work around the fixer working incorrectly
    for commands, but still have it continue to work for all the other
    modules it fixes. The context is the setup.py script of Django.

    I wonder if a better way would be to configure the imports fixer
    through the options dict passed to RefactoringTool. i.e.

     RefactoringTool(..., {"fix_imports:dont_transform" : ["commands",
    "other_module"]})
  10. loewis commented on Nov 26, 2008

    loewismannequin
    MannequinAuthor

    I wonder if a better way would be to configure the imports fixer
    through the options dict passed to RefactoringTool. i.e.

    RefactoringTool(..., {"fix_imports:dont_transform" : ["commands",
    "other_module"]})

    That's very obscure, IMO. I didn't know that syntax existed, and
    I can't guess easily what it means (in general).

    In the specific case, it's also difficult to pass the options,
    since the distutils command doesn't support doing so.

  11. benjaminp commented on Nov 26, 2008

    @benjaminp
    Contributor

    Ok. The building of the pattern in fix_imports is changed to
    compile_pattern in r67404.

  12. 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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions