# PEP8 style: hanging indentation and closing brackets

**URL:** <https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618>\
**Category:** Data Management\
**Tags:** pep8\
**Created:** [March 24, 2016, 3:38pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618 "2016-03-24T15:38:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwv](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@mwv](https://www.rubin.community/u/mwv)\
**Post date:** [March 24, 2016, 3:38pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/1 "2016-03-24T15:38:10Z")

</div>

Which of the following three are preferred under our interpretation of PEP8?

To short-circuit the discussion: I naturally write #1. In part because I want to be able to minimize number of lines changed when adding and removing things from lists.

I’m OK with #2.

But only #3 passes PEP8 flake8 (with the RFC config file).

1. 

```auto
        kwargs = {
            'good_mag_limit': good_mag_limit,
            'medianAstromscatterRef': medianAstromscatterRef,
            'medianPhotoscatterRef': medianPhotoscatterRef,
            'matchRef': matchRef,
            }

```

1. 

```auto
        kwargs = {'good_mag_limit': good_mag_limit,
                  'medianAstromscatterRef': medianAstromscatterRef,
                  'medianPhotoscatterRef': medianPhotoscatterRef,
                  'matchRef': matchRef,
                 }

```

1. 

```auto
        kwargs = {'good_mag_limit': good_mag_limit,
                  'medianAstromscatterRef': medianAstromscatterRef,
                  'medianPhotoscatterRef': medianPhotoscatterRef,
                  'matchRef': matchRef}

```

The key issue is where to put the closing bracket. PEP8 is unhappy with the ways I think make the most sense.

Links of interest:  
[https://jira.lsstcorp.org/browse/RFC-162](https://jira.lsstcorp.org/browse/RFC-162)

> **[PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#indentation)**
>
> The official home of the Python Programming Language

---

<div class="post-metadata">

**Author:** ![jbosch](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jbosch/32/12_2.png) [@jbosch](https://www.rubin.community/u/jbosch)\
**Post date:** [March 24, 2016, 3:56pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/2 "2016-03-24T15:56:10Z")

</div>

I personally prefer (1), and I tend to use something similar for function calls or definitions that extend beyond one line. I think it’s the only one of these options that scales to the next level (when one of the dict values extends beyond one line, or you have a nested dict).

I prefer to put the closing brace at the same indentation as the variable definition, but I don’t care about that all that much:

```auto
    kwargs = {
        ...
    }

```

---

<div class="post-metadata">

**Author:** ![jsick](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jsick/32/672_2.png) [@jsick](https://www.rubin.community/u/jsick)\
**Post date:** [March 24, 2016, 3:58pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/3 "2016-03-24T15:58:48Z")

</div>

Option #3 is absolutely what I’d use most of the time as a PEP8 purist.

If the names of your key value pairs are long then you can also do

```auto
kwargs = {
    'good_mag_limit': good_mag_limit,
    'medianAstromscatterRef': medianAstromscatterRef,
    'medianPhotoscatterRef': medianPhotoscatterRef,
    'matchRef': matchRef,
}

```

as @jbosch suggests. That’s also PEP8 compliant.

---

<div class="post-metadata">

**Author:** ![price](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/price/32/488_2.png) [@price](https://www.rubin.community/u/price)\
**Post date:** [March 24, 2016, 3:59pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/4 "2016-03-24T15:59:29Z")

</div>

I think @jbosch likes:

1. 

```
kwargs = {
    'good_mag_limit': good_mag_limit,
    'medianAstromscatterRef': medianAstromscatterRef,
    'medianPhotoscatterRef': medianPhotoscatterRef,
    'matchRef': matchRef,
}

```

But that’s different from 1.

---

<div class="post-metadata">

**Author:** ![timj](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/timj/32/10_2.png) [@timj](https://www.rubin.community/u/timj)\
**Post date:** [March 24, 2016, 4:01pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/5 "2016-03-24T16:01:44Z")

</div>

Agreed. +1 on (4). This is one of those things that `flake8` can handle but is not enabled by default.

---

<div class="post-metadata">

**Author:** ![price](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/price/32/488_2.png) [@price](https://www.rubin.community/u/price)\
**Post date:** [March 24, 2016, 4:04pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/6 "2016-03-24T16:04:43Z")

</div>

I’ve no great preference over where the closing brace goes (except emacs naturally does option #1 for me and it’s a pain to fix it). But I really don’t like the following:

1. 

```
kwargs = {
    'good_mag_limit': good_mag_limit,
    'medianAstromscatterRef': medianAstromscatterRef,
    'medianPhotoscatterRef': medianPhotoscatterRef,
    'matchRef': matchRef,
} # for any placement of this brace
```

---

<div class="post-metadata">

**Author:** ![swinbank](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/swinbank/32/24_2.png) [@swinbank](https://www.rubin.community/u/swinbank)\
**Post date:** [March 24, 2016, 4:05pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/7 "2016-03-24T16:05:12Z")

</div>

> [@timj](#):
>
> Agreed. +1 on (4).

👍. Failing that, (3) (the pure PEP-8 option).

---

<div class="post-metadata">

**Author:** ![mwv](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@mwv](https://www.rubin.community/u/mwv)\
**Post date:** [March 24, 2016, 6:41pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/8 "2016-03-24T18:41:58Z")

</div>

Thanks, all. I take the adopted answer as (4):

```auto
kwargs = {
    'good_mag_limit': good_mag_limit,
    'medianAstromscatterRef': medianAstromscatterRef,
    'medianPhotoscatterRef': medianPhotoscatterRef,
    'matchRef': matchRef,
}

```

---

<div class="post-metadata">

**Author:** ![parejkoj](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/parejkoj/32/138_2.png) [@parejkoj](https://www.rubin.community/u/parejkoj)\
**Post date:** [March 24, 2016, 8:08pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/9 "2016-03-24T20:08:51Z")

</div>

Wait, where does the indentation there go? I though it was supposed to align with the top brace (a la 2/3)?

---

<div class="post-metadata">

**Author:** ![jsick](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jsick/32/672_2.png) [@jsick](https://www.rubin.community/u/jsick)\
**Post date:** [March 24, 2016, 8:15pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/10 "2016-03-24T20:15:04Z")

</div>

In case #4 you’re allowed to indent by 4 spaces from the first line of a multi-line statement.

---

<div class="post-metadata">

**Author:** ![parejkoj](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/parejkoj/32/138_2.png) [@parejkoj](https://www.rubin.community/u/parejkoj)\
**Post date:** [March 24, 2016, 8:50pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/11 "2016-03-24T20:50:34Z")

</div>

By “allowed” here, you mean “required”?

I’m coming at this from a view that the PEP8 “# Aligned with opening delimiter.” style is correct and what we want.

---

<div class="post-metadata">

**Author:** ![jsick](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/jsick/32/672_2.png) [@jsick](https://www.rubin.community/u/jsick)\
**Post date:** [March 24, 2016, 8:53pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/12 "2016-03-24T20:53:41Z")

</div>

PEP 8 is fine with either #3 or #4. Both are useful. I tend to use #3 first, and fall back to #4 if I need the extra horizontal space.

---

<div class="post-metadata">

**Author:** ![parejkoj](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/parejkoj/32/138_2.png) [@parejkoj](https://www.rubin.community/u/parejkoj)\
**Post date:** [March 24, 2016, 9:09pm UTC](https://www.rubin.community/t/pep8-style-hanging-indentation-and-closing-brackets/618/13 "2016-03-24T21:09:46Z")

</div>

Great. No complaints about that.
