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

Incorrectly formats textwrap.dedent #570

Closed
muppetjones opened this issue Oct 17, 2018 · 5 comments
Closed

Incorrectly formats textwrap.dedent #570

muppetjones opened this issue Oct 17, 2018 · 5 comments

Comments

@muppetjones
Copy link

Howdy! Sorry you're having trouble. To expedite your experience,
provide some basics for me:

Operating system: OSX
Python version: 3.4, 3.5, 3.6
Black version: 18.9b0
Does also happen on master: yes

  • given (I'm actually pretty happy with this)
            dedent("""
                Some text that
                is split across multiple lines.
            """)
  • after black
            dedent(
                """
            Some text that
            is split across multiple lines.
        """
            )
  • after black (on the playground w/o supporting indents)
dedent(
    """
                Some text that
                is split across multiple lines.
            """
)
  • Minimal expected
            dedent(
                """
                    Some text that
                    is split across multiple lines.
                """
            )
@asottile
Copy link
Contributor

asottile commented Nov 5, 2018

This is the only correct formatting, your suggested formatting changes the contents of the string:

Taking the two examples above (I've dedented the xamples so it's more obvious what's different)

dedent("""
    Some text that
    is split across multiple lines.
""")

dedent(
    """
    Some text that
    is split across multiple lines.
    """
)

And then taking the strings from those:

>>> x = """
...     Some text that
...     is split across multiple lines.
... """
>>> y = """
...     Some text that
...     is split across multiple lines.
...     """
>>> x
'\n    Some text that\n    is split across multiple lines.\n'
>>> y
'\n    Some text that\n    is split across multiple lines.\n    '
>>> x == y
False
>>> [(i, l1, l2) for i, (l1, l2) in enumerate(zip(x.split('\n'), y.split('\n'))) if l1 != l2]
[(3, '', '    ')]

@sorcio
Copy link

sorcio commented Jan 25, 2019

Would it make sense to special-case dedent(""" .... """)? Usually the reason I want to use dedent() with a multi-line literal is because I want a specific formatting style, and I'd dare say that if this is good style for Black:

x = """
    My multi-line
    text that I want to
    dedent.
"""

then this should also be good style:

x = dedent("""
    My multi-line
    text that I want to
    dedent.
""")

@zsol
Copy link
Collaborator

zsol commented Jan 31, 2019

Black won't ever change your strings this way.

@zsol zsol closed this as completed Jan 31, 2019
@sorcio
Copy link

sorcio commented Jan 31, 2019

@zsol the last example I provided didn't involve any change to the string. On the contrary, I'm just suggesting Black should not to try to do fancy formatting when it encounters a dedent("""multiline literal"""). Treat it as a special kind of literal, maybe? So a block like the one above should be left unchanged. Let me know if you want me to open a separate issue to discuss this suggestion.

@basnijholt
Copy link

basnijholt commented May 3, 2019

I don't understand why this is closed @zsol. Black seems to break my code here:

import textwrap

def make_sbatch(name, cores, executable="run_learner.py", env="py37_min"):
    job_script = textwrap.dedent(
        f"""\
        #!/bin/bash
        #SBATCH --job-name {name}
        #SBATCH --ntasks {cores}
        #SBATCH --output {name}.out
        #SBATCH --no-requeue

        export MKL_NUM_THREADS=1
        export OPENBLAS_NUM_THREADS=1
        export OMP_NUM_THREADS=1

        export MPI4PY_MAX_WORKERS=$SLURM_NTASKS
        srun -n $SLURM_NTASKS --mpi=pmi2 ~/miniconda3/envs/{env}/bin/python3 -m mpi4py.futures {executable}
        """
    )
    return job_script

print(make_sbatch("test", 1))

It removes the \ and this means the shebang won't be on the first line anymore and thus results in a broken shell script.

Edit: I see #256 is still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants