Skip to content

Multiline statements not upgraded for f-strings (Py 3.6+) #178

Closed

Description

@taldcroft

It looks like multiline statements are not being upgraded for f-strings. For example this is not changed:

val = 1
'{}'.format(
    val)

But the one below does get changed:

val = 1
'{}'.format(val)

Is this expected?

Activity

  1. asottile commented on Jul 5, 2019

    @asottile
    Owner

    Please search the issue tracker next time! #55 #49

  2. taldcroft commented on Jul 5, 2019

    @taldcroft
    Author

    Sorry I assumed this would be an open issue and didn't see it. Given that this is the third repeat of the issue maybe some less timid option might be handy? It's easy enough for users to fix line length post facto with automated tools.

  3. asottile commented on Jul 5, 2019

    @asottile
    Owner

    PRs welcome :)

  4. taldcroft commented on Jul 5, 2019

    @taldcroft
    Author

    @asottile - this package is generally way beyond my level, but I had a look anyway and did spot the nicely labeled timid restrictions. A couple of them look relevant, in particular the bit about not making an f-string longer. But no joy, commenting out the break did not help, and the print statements show for my example code that these clauses are never being reached:

    diff --git a/pyupgrade.py b/pyupgrade.py
    index dd5f23c..5120bd6 100644
    --- a/pyupgrade.py
    +++ b/pyupgrade.py
    @@ -1636,7 +1636,8 @@ def _fix_py3_plus(contents_text):  # type: (str) -> str
                 while tokens[j].src != node.attr:
                     # timid: if we see a parenthesis here, skip it
                     if tokens[j].src == ')':
    -                    return
    +                    print('Not timid!!')
    +                    # return
                     j += 1
                 tokens[i:j + 1] = [new_token]
     
    @@ -1812,7 +1813,8 @@ class FindSimpleFormats(ast.NodeVisitor):
                         candidate, _, _ = name.partition('.')
                         # timid: could make the f-string longer
                         if candidate and candidate in seen:
    -                        break
    +                        print('Not timid!')
    +                        # break
                         # timid: bracketed
                         elif '[' in name:
                             break

    Any hints? pyupgrade is an awesome package, so it would be nice if it could go the full distance for the hundreds of files that need fixing in astropy.

  5. asottile commented on Jul 5, 2019

    @asottile
    Owner

    there's a few things that aren't yet handled:

    • implicit string joins
    • parenthesized strings that are then .format(...)ed
    • repeated names in format strings

    The first two need some new code written for them (similar to the stuff that's happening for the py27 format strings, basically a state machine to collect all the separate STRING tokens that are implicit-joined). I ~eventually want something like this in tokenize-rt (since there's currently code for this both here and in future-fstrings that does the same parsing).

    The second should (?) have been covered by the second part of the patch

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions