Yapf: How to prevent YAPF from joining lines in dictionaries and lists?

Created on 24 Jan 2017  路  12Comments  路  Source: google/yapf

YAPF converts

DJANGO_TEMPLATES_OPTIONS = {
  "context_processors": []
}

to

DJANGO_TEMPLATES_OPTIONS = {"context_processors": []}

and I cannot find the option to disable such behavior.

Is it possible?

Most helpful comment

I would love to see this as well. A case I keep coming across:

class Status(models.Model):

    STATUS_CHOICES = (
        ('g', 'Green'),
        ('y', 'Yellow'),
        ('r', 'Red'),
    )
    ..

Becomes

class Status(models.Model):

     STATUS_CHOICES = (('g', 'Green'), ('y', 'Yellow'), ('r', 'Red'),)
     ..

Which is less readable in my opinion.

All 12 comments

Unfortunately, we don't have a knob for this. At least this specific case, where there is only one element in the dictionary / list. If you have two or more, you could get them on individual lines by terminating the list with a comma:

DJANGO_TEMPLATES_OPTIONS = {
    "context_processors": [],
    "context_processors": [],
}

Alternatively, you can disable formatting for the dictionary like this:

DJANGO_TEMPLATES_OPTIONS = {
    "context_processors": []
}  # yapf: disable

Thank you, for clarification, I suppose this shall be addressed somehow, don't you think so?

Let me know if this addresses your issue.

It is much better now, it works with lists, but it still does the same thing with tuples:

REST_FRAMEWORK = {
  # by default only authenticated users have access to our REST API
  "DEFAULT_PERMISSION_CLASSES": (
    "rest_framework.permissions.IsAuthenticated",
  ),
  # our REST API accept two types of authentication: session-based and token-based
  "DEFAULT_AUTHENTICATION_CLASSES": (
    "rest_framework.authentication.SessionAuthentication",
    "rest_framework.authentication.TokenAuthentication",
  ),
}

becomes

REST_FRAMEWORK = {
  # by default only authenticated users have access to our REST API
  "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated", ),
  # our REST API accept two types of authentication: session-based and token-based
  "DEFAULT_AUTHENTICATION_CLASSES": (
    "rest_framework.authentication.SessionAuthentication",
    "rest_framework.authentication.TokenAuthentication",
  ),
}

Yeah. Tuples are a bit trickier for this. I'll see what I can do to help here...

I would love to see this as well. A case I keep coming across:

class Status(models.Model):

    STATUS_CHOICES = (
        ('g', 'Green'),
        ('y', 'Yellow'),
        ('r', 'Red'),
    )
    ..

Becomes

class Status(models.Model):

     STATUS_CHOICES = (('g', 'Green'), ('y', 'Yellow'), ('r', 'Red'),)
     ..

Which is less readable in my opinion.

One more complain (sorry if this is not directly related to this issue, just say a word, I will move it to new one). This

redis = await create_subprocess_exec("Redis", "/usr/local/bin/redis-server",
                                     "--bind", str(host), "--port", str(redis_port))

becomes this:

  redis = await create_subprocess_exec(
    "Redis", "/usr/local/bin/redis-server", "--bind", str(host), "--port", str(redis_port)
  )

I would like to keep the first variant (where I align code to opening bracket and line brakes are set by some logic), but cannot find proper setting, is that possible?

@prokher I would probably create a different issue for that, but I think you might be looking for the Knob SPLIT_BEFORE_FIRST_ARGUMENT

@audiolion I took a look at SPLIT_BEFORE_FIRST_ARGUMENT. It is set to False in the configuration. But YAPF anyway make this snippet

        process = await asyncio.create_subprocess_exec(*cmd, env=env,
                                                       stdout=asyncio.subprocess.PIPE,
                                                       stderr=asyncio.subprocess.PIPE)

become this one:

        process = await asyncio.create_subprocess_exec(
          *cmd, env=env, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )

Any update on ensuring tuple elements are each on their own line?

@gwelymernans This still seems to be not quite working for me. Can I suggest a couple of proposals (happy to implement if you're amenable):

1) Add JOIN_PENALTY_* settings analogous to the split penalties
2) A settings PRESERVE_SPLITS_THAT_CONFORM_TO_STYLE that would simply maintain the original formatting as long it conforms to the style.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qv-pat picture qv-pat  路  3Comments

haphut picture haphut  路  3Comments

mattrichards picture mattrichards  路  3Comments

pankdm picture pankdm  路  8Comments

hayd picture hayd  路  8Comments