google / yapf

A formatter for Python files
Apache License 2.0
13.77k stars 891 forks source link

function args #421

Open LuisBL opened 7 years ago

LuisBL commented 7 years ago

I whould like to keep args indented like this:

git.project_members.create({'user_id': user.id,
                            'access_level': gitlab.DEVELOPER_ACCESS},
                           project_id=project.id)

But I get this:

git.project_members.create(
    {    
        'user_id': user.id,
        'access_level': gitlab.DEVELOPER_ACCESS
    },   
    project_id=project.id)

Is there a way with options to keep my favorit indentation for funtions args ?

bwendling commented 7 years ago

I don't think there is. The issue here is that the first element being a dictionary can cause the formatter to create really horrible looking code. (Think of your second example with the { still on the first line. It would indent the project_id argument over to the column that the { is in.) YAPF instead chooses to split before the dictionary (it does the same to function calls as the first argument).

git.project_members.create({
        'user_id': user.id,
        'access_level': gitlab.DEVELOPER_ACCESS
    },
                           project_id=project.id)

One exception may be that if the dictionary fit on a single line, then it probably should be output on a single line, but this won't help your code in general.

I'm not entirely sure how to proceed with this. If we don't split before the dictionary, the code will look horrible...