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

black adds unnecessary lines around triple-quoted blocks #462

Closed
webartifex opened this issue Aug 19, 2018 · 4 comments
Closed

black adds unnecessary lines around triple-quoted blocks #462

webartifex opened this issue Aug 19, 2018 · 4 comments
Labels
R: duplicate This issue or pull request already exists

Comments

@webartifex
Copy link

webartifex commented Aug 19, 2018

Using the rpy2 library to embed R code in a Python project, a common idiom is this:

import rpy2.robjects as robjects

robjects.r('''
loadRData <- function(fileName){
    #' Load an .rda file and return it as a r.DataFrame.
    load(fileName)
    get(ls()[ls() != "fileName"])
}
''')

Appying black -S to it, the code becomes:

robjects.r(
    '''
loadRData <- function(fileName){
    #' Load an .rda file and return it as a r.DataFrame.
    load(fileName)
    get(ls()[ls() != "fileName"])
}
'''
)

This might be an issue with other language embeddings as well.

My suggestion is that black does not change this idiom, i.e., the opening and closing parenthesis should be coalesced with the triple (single) quotes.

Operating system:
Python version: 3.6.5
Black version: 18.6b4
Does also happen on master: yes

EDIT: earlier versions did not show the whitespace correctly.

@asottile
Copy link
Contributor

To make a code block use triple backticks

    ```python
    print('like this!')
    ```

@JelleZijlstra JelleZijlstra changed the title rpy2 usage becomes weird black adds unnecessary lines around triple-quoted blocks Aug 22, 2018
@gwax
Copy link

gwax commented Aug 24, 2018

I have run into this with the similar textwrap.dedent idiom:

textwrap.dedent("""\
    Hello, I am
    a multiline string used with
    a common idiom
""")

black will convert this to:

textwrap.dedent(
    """\
    Hello, I am
    a multiline string used with
    a common idiom
"""
)

This is particularly bad in the case of another containing function:

sqlalchemy.text(textwrap.dedent("""\
    SELECT
        *
    FROM
        sometable
"""))

becomes:

sqlalchemy.text(
    textwrap.dedent(
        """\
    SELECT
        *
    FROM
        sometable
"""
    )
)

@zsol
Copy link
Collaborator

zsol commented Sep 7, 2018

I think this boils down to the same cause as #256

@zsol zsol added the T: style What do we want Blackened code to look like? label Sep 7, 2018
@ambv
Copy link
Collaborator

ambv commented Sep 26, 2018

I'll close this as a duplicate and we'll handle this use case as part of #256, too.

@ambv ambv closed this as completed Sep 26, 2018
@ambv ambv added R: duplicate This issue or pull request already exists and removed T: style What do we want Blackened code to look like? labels Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

5 participants