flake8-picky-parentheses


Nameflake8-picky-parentheses JSON
Version 0.5.5 PyPI version JSON
download
home_pageNone
Summaryflake8 plugin to nitpick about parenthesis, brackets, and braces
upload_time2024-03-22 14:11:51
maintainerNone
docs_urlNone
authorIvan Prychantovskyi, Rouven Bauer
requires_python>=3.7
licenseApache License 2.0
keywords flake8 plugin redundant superfluous extraneous unnecessary parentheses parenthesis parens brackets linter linting codestyle code style
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Picky Parentheses
=================

Picky Parentheses is a [flake8](https://github.com/pycqa/flake8) plugin that
nitpicks all things parentheses, brackets and braces.
The plugin has two components:
 1. A checker that warns about redundant parentheses (with some exceptions).
 2. A checker for parentheses, brackets, and braces alignment.
    This component is very opinionated but has its own error codes so you can
    easily disable it.


## Table of Contents

 * [Installation and Usage](#installation-and-usage)
 * [Error Codes](#error-codes)
 * [Details and Exceptions](#details-and-exceptions)
 * [Additional Notes](#additional-notes)


## Installation and Usage
This is a plugin for `flake8`. It supports Python 3.7 - 3.12.  
Refer to the documentation of `flake8` on how to run it on your code:
https://flake8.pycqa.org/en/latest/

Two common options are to either install the plugin and then run `flake8`:
```bash
pip install flake8-picky-parentheses

flake8 '<path/to/your/code>'
```

Or to let `flake8` fetch the plugin for you (requires `flake8 >= 5.0.0`):
```bash
flake8 --require-plugins flake8-picky-parentheses '<path/to/your/code>'
```

If you only want to run this plugin and bypass any other `flake8` checks, you
can use the `--select` option:
```bash
flake8 [other options] --select='PAR0,PAR1' '<path/to/your/code>'
```

Where `PAR0` is the code for the redundant parentheses checker and `PAR1` is
the code for the parentheses alignment checker.

If you, in turn want to disable the opinionated parentheses alignment checker,
you can use the `--ignore` or `--extend-ignore` option:
```bash
flake8 [other options] --extend-ignore='PAR1' '<path/to/your/code>'
```


## Error Codes
These are the error codes which you can get using this plugin:

| Code                | Brief Description                                                                           |
|---------------------|---------------------------------------------------------------------------------------------|
| [`PAR0xx`](#par0xx) | [Group] Redundant parentheses                                                               |
| [`PAR001`](#par001) | Redundant parentheses (general)                                                             |
| [`PAR002`](#par002) | Parentheses used for tuple unpacking                                                        |
|                     |                                                                                             |
| [`PAR1xx`](#par1xx) | [Group] (Opinionated) parentheses, brackets, braces not well-aligned                        |
| [`PAR101`](#par101) | Opening bracket is last, but closing is not on new line                                     |
| [`PAR102`](#par102) | Closing bracket has different indentation than the line with the opening bracket            |
| [`PAR103`](#par103) | Consecutive opening brackets at the end of the line must have consecutive closing brackets. |
| [`PAR104`](#par104) | Only operators and comments are allowed after a closing bracket on a new line               |

### `PAR0xx`
These are the error codes for the redundant parentheses checker.
#### `PAR001`
It means that you use redundant parentheses, and they do not help readability.
For example:
```python
# BAD
a = (("a", "b"))
```
#### `PAR002`
It means that you use parentheses for an unpacking expression. For example:
```python
# BAD
(a,) = "b"
```

#### `PAR1xx`
These are the error codes for the opinionated alignment checker.
#### `PAR101`
It means that the opening bracket is last in its line, but closing one is not
on a new line. For example:
```python
# BAD
if (
        a == b):
    c + d

# GOOD
if (
    a == b
):
    c + d

# BAD
a = [
    1, 2,
    3, 4]

# GOOD
a = [
    1, 2,
    3, 4
]

# GOOD
a = [1, 2,
     3, 4]
```
#### `PAR102`
It means that closing bracket is on new line, but there is a indentation
mismatch. For example:
```python
# BAD
if (
    a == b
        ):
    c + d

# GOOD
if (
    a == b
):
    c + d

# BAD
a = [
    1, 2,
    3, 4
    ]

# GOOD
a = [
    1, 2,
    3, 4
]
```

#### `PAR103`
It means that consecutive opening brackets at the end of a line must have
consecutive closing brackets.
```python
# BAD
answer = func((
    1, 2, 3, 4, 5,
    )
)

# GOOD
answer = func((
    1, 2, 3, 4, 5,
))
```

#### `PAR104`
Only operators and comments are allowed after a closing bracket on a new line.
```python
# BAD
a = func(
    1, 2, 3, 4, 5
) + 6

# GOOD
a = (
    func(
        1, 2, 3, 4, 5
    )
    + 6
)
```


## Details and Exceptions

The redundant parentheses checker uses Python's `tokenize` and `ast` module to
try to remove each pair of parentheses and see if the code still compiles and
yields the same AST (i.e., is semantically equivalent).
If it does, a flake (lint error) is reported. However, there are two notable
exceptions to this rule:
 1. Parentheses for tuple literals.
 2. A single pair or parentheses in expressions to highlight operator
    precedence.
    Even if these parentheses are redundant, they help to divide parts of
    expressions and show sequence of actions.
 3. Parts of slices.
 4. Multi-line<sup>[1)](#footnotes)</sup> expression, `if` and `for` parts in comprehensions.
 5. Multi-line<sup>[1)](#footnotes)</sup> keyword arguments or argument defaults.
 6. String concatenation over several lines in lists and tuples .


Exception type 1:
```python
a = ("a",)     # GOOD
a = "a",       # GOOD
a = ("a")      # BAD
a = (("a"),)   # BAD
a = (("a",))   # BAD
foo(("a",))    # GOOD
foo("a",)      # BAD
```

Exception type 2:
```python
a = (1 + 2) + 3            # GOOD
a = (1 + 2) % 3            # GOOD
a = 1 and (2 + 3)          # GOOD
a = (1 / 2) * 3            # GOOD
a = not (1 + 2)            # GOOD
a = (not 1) + 2            # GOOD
a = 1 + (2 if a else 3)    # GOOD
a = foo(*(a if b else c))  # GOOD
a = foo(*(a + b))          # GOOD
a = foo(**(a + b))         # GOOD
a = (1 + 2)                # BAD
a = 1 + (2)                # BAD
a = ((not 1)) + 2          # BAD
a = foo(*(a))              # BAD
a = foo(**(a))             # BAD
```

Exception type 3:
```python
foo[(1 + 2):10]    # GOOD
foo[1:(1 + 2)]     # GOOD
foo[1:5:(1 + 1)]   # GOOD
foo[:(-bar)]       # GOOD
foo[(1):]          # BAD
foo[:(1)]          # BAD
```

Exception type 4:
```python
# GOOD
a = (
    b for b in c
    if (
        some_thing == other_thing
        or whatever_but_long
    )
)

# GOOD
a = [
    b for b in c
    if (b
        in d)
]

# BAD
a = (
    b for b in c
    if (b in d)
)

# GOOD
a = (
    b for b in (c
                + d)
)

# BAD
a = (
    b for b in (c + d)
)

# GOOD
a = (
    (
        1
        + b
    )
    for b in c
)

# BAD
a = (
    (1 + b) for b in c
)

# GOOD
a = {
    (
        "foo%s"
        % b
    ): (
        b
        * 2
    )
    for b in c
}
```

Exception type 5:
```python
# GOOD
foo(bar=(a
         in b))

# BAD
foo(bar=(a in b))

# GOOD
def foo(bar=(a
             is b)):
    ...

# BAD
def foo(bar=(a is b)):
    ...
```

Exception type 6:

```python
# GOOD
[
    "a",
    (
        "b"
        "c"
    ),
    "d",
]

# This helps to avoid forgetting a comma at the end of a string spanning
# multiple lines. Compare with:
[
    "a",
    "b"
    "c"
    "d",
]
# Was the comma after "b" forgotten or was the string supposed to be "bc"?

# BAD
[
    (
        "a" "b"
    ),
]
```

### Footnotes:
1. Multi-line means that either
   * the expression spans multiple lines, e.g.,
     ```python
     (a
      + b)
     ```
   * or the first part of the expression is on a new line (e.g., if a name is very long), e.g.,
     ```python
     (
         veeeeeeeeeeery_looooooong_name
     )
     ```
     but also
     ```python
     (
         a
         + b
     )
     ```
   Multi-line expressions do **not** include
   ```python
   (a + b
   )
   ```

## Additional Notes

This plugin was developed to improve the code quality of Neo4j Python projects.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "flake8-picky-parentheses",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "flake8, plugin, redundant, superfluous, extraneous, unnecessary, parentheses, parenthesis, parens, brackets, linter, linting, codestyle, code style",
    "author": "Ivan Prychantovskyi, Rouven Bauer",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/eb/dc/6ac2d6c746eff44b011e07a71b8e078c33d0958c55069198eef03e1d4585/flake8-picky-parentheses-0.5.5.tar.gz",
    "platform": null,
    "description": "Picky Parentheses\n=================\n\nPicky Parentheses is a [flake8](https://github.com/pycqa/flake8) plugin that\nnitpicks all things parentheses, brackets and braces.\nThe plugin has two components:\n 1. A checker that warns about redundant parentheses (with some exceptions).\n 2. A checker for parentheses, brackets, and braces alignment.\n    This component is very opinionated but has its own error codes so you can\n    easily disable it.\n\n\n## Table of Contents\n\n * [Installation and Usage](#installation-and-usage)\n * [Error Codes](#error-codes)\n * [Details and Exceptions](#details-and-exceptions)\n * [Additional Notes](#additional-notes)\n\n\n## Installation and Usage\nThis is a plugin for `flake8`. It supports Python 3.7 - 3.12.  \nRefer to the documentation of `flake8` on how to run it on your code:\nhttps://flake8.pycqa.org/en/latest/\n\nTwo common options are to either install the plugin and then run `flake8`:\n```bash\npip install flake8-picky-parentheses\n\nflake8 '<path/to/your/code>'\n```\n\nOr to let `flake8` fetch the plugin for you (requires `flake8 >= 5.0.0`):\n```bash\nflake8 --require-plugins flake8-picky-parentheses '<path/to/your/code>'\n```\n\nIf you only want to run this plugin and bypass any other `flake8` checks, you\ncan use the `--select` option:\n```bash\nflake8 [other options] --select='PAR0,PAR1' '<path/to/your/code>'\n```\n\nWhere `PAR0` is the code for the redundant parentheses checker and `PAR1` is\nthe code for the parentheses alignment checker.\n\nIf you, in turn want to disable the opinionated parentheses alignment checker,\nyou can use the `--ignore` or `--extend-ignore` option:\n```bash\nflake8 [other options] --extend-ignore='PAR1' '<path/to/your/code>'\n```\n\n\n## Error Codes\nThese are the error codes which you can get using this plugin:\n\n| Code                | Brief Description                                                                           |\n|---------------------|---------------------------------------------------------------------------------------------|\n| [`PAR0xx`](#par0xx) | [Group] Redundant parentheses                                                               |\n| [`PAR001`](#par001) | Redundant parentheses (general)                                                             |\n| [`PAR002`](#par002) | Parentheses used for tuple unpacking                                                        |\n|                     |                                                                                             |\n| [`PAR1xx`](#par1xx) | [Group] (Opinionated) parentheses, brackets, braces not well-aligned                        |\n| [`PAR101`](#par101) | Opening bracket is last, but closing is not on new line                                     |\n| [`PAR102`](#par102) | Closing bracket has different indentation than the line with the opening bracket            |\n| [`PAR103`](#par103) | Consecutive opening brackets at the end of the line must have consecutive closing brackets. |\n| [`PAR104`](#par104) | Only operators and comments are allowed after a closing bracket on a new line               |\n\n### `PAR0xx`\nThese are the error codes for the redundant parentheses checker.\n#### `PAR001`\nIt means that you use redundant parentheses, and they do not help readability.\nFor example:\n```python\n# BAD\na = ((\"a\", \"b\"))\n```\n#### `PAR002`\nIt means that you use parentheses for an unpacking expression. For example:\n```python\n# BAD\n(a,) = \"b\"\n```\n\n#### `PAR1xx`\nThese are the error codes for the opinionated alignment checker.\n#### `PAR101`\nIt means that the opening bracket is last in its line, but closing one is not\non a new line. For example:\n```python\n# BAD\nif (\n        a == b):\n    c + d\n\n# GOOD\nif (\n    a == b\n):\n    c + d\n\n# BAD\na = [\n    1, 2,\n    3, 4]\n\n# GOOD\na = [\n    1, 2,\n    3, 4\n]\n\n# GOOD\na = [1, 2,\n     3, 4]\n```\n#### `PAR102`\nIt means that closing bracket is on new line, but there is a indentation\nmismatch. For example:\n```python\n# BAD\nif (\n    a == b\n        ):\n    c + d\n\n# GOOD\nif (\n    a == b\n):\n    c + d\n\n# BAD\na = [\n    1, 2,\n    3, 4\n    ]\n\n# GOOD\na = [\n    1, 2,\n    3, 4\n]\n```\n\n#### `PAR103`\nIt means that consecutive opening brackets at the end of a line must have\nconsecutive closing brackets.\n```python\n# BAD\nanswer = func((\n    1, 2, 3, 4, 5,\n    )\n)\n\n# GOOD\nanswer = func((\n    1, 2, 3, 4, 5,\n))\n```\n\n#### `PAR104`\nOnly operators and comments are allowed after a closing bracket on a new line.\n```python\n# BAD\na = func(\n    1, 2, 3, 4, 5\n) + 6\n\n# GOOD\na = (\n    func(\n        1, 2, 3, 4, 5\n    )\n    + 6\n)\n```\n\n\n## Details and Exceptions\n\nThe redundant parentheses checker uses Python's `tokenize` and `ast` module to\ntry to remove each pair of parentheses and see if the code still compiles and\nyields the same AST (i.e., is semantically equivalent).\nIf it does, a flake (lint error) is reported. However, there are two notable\nexceptions to this rule:\n 1. Parentheses for tuple literals.\n 2. A single pair or parentheses in expressions to highlight operator\n    precedence.\n    Even if these parentheses are redundant, they help to divide parts of\n    expressions and show sequence of actions.\n 3. Parts of slices.\n 4. Multi-line<sup>[1)](#footnotes)</sup> expression, `if` and `for` parts in comprehensions.\n 5. Multi-line<sup>[1)](#footnotes)</sup> keyword arguments or argument defaults.\n 6. String concatenation over several lines in lists and tuples .\n\n\nException type 1:\n```python\na = (\"a\",)     # GOOD\na = \"a\",       # GOOD\na = (\"a\")      # BAD\na = ((\"a\"),)   # BAD\na = ((\"a\",))   # BAD\nfoo((\"a\",))    # GOOD\nfoo(\"a\",)      # BAD\n```\n\nException type 2:\n```python\na = (1 + 2) + 3            # GOOD\na = (1 + 2) % 3            # GOOD\na = 1 and (2 + 3)          # GOOD\na = (1 / 2) * 3            # GOOD\na = not (1 + 2)            # GOOD\na = (not 1) + 2            # GOOD\na = 1 + (2 if a else 3)    # GOOD\na = foo(*(a if b else c))  # GOOD\na = foo(*(a + b))          # GOOD\na = foo(**(a + b))         # GOOD\na = (1 + 2)                # BAD\na = 1 + (2)                # BAD\na = ((not 1)) + 2          # BAD\na = foo(*(a))              # BAD\na = foo(**(a))             # BAD\n```\n\nException type 3:\n```python\nfoo[(1 + 2):10]    # GOOD\nfoo[1:(1 + 2)]     # GOOD\nfoo[1:5:(1 + 1)]   # GOOD\nfoo[:(-bar)]       # GOOD\nfoo[(1):]          # BAD\nfoo[:(1)]          # BAD\n```\n\nException type 4:\n```python\n# GOOD\na = (\n    b for b in c\n    if (\n        some_thing == other_thing\n        or whatever_but_long\n    )\n)\n\n# GOOD\na = [\n    b for b in c\n    if (b\n        in d)\n]\n\n# BAD\na = (\n    b for b in c\n    if (b in d)\n)\n\n# GOOD\na = (\n    b for b in (c\n                + d)\n)\n\n# BAD\na = (\n    b for b in (c + d)\n)\n\n# GOOD\na = (\n    (\n        1\n        + b\n    )\n    for b in c\n)\n\n# BAD\na = (\n    (1 + b) for b in c\n)\n\n# GOOD\na = {\n    (\n        \"foo%s\"\n        % b\n    ): (\n        b\n        * 2\n    )\n    for b in c\n}\n```\n\nException type 5:\n```python\n# GOOD\nfoo(bar=(a\n         in b))\n\n# BAD\nfoo(bar=(a in b))\n\n# GOOD\ndef foo(bar=(a\n             is b)):\n    ...\n\n# BAD\ndef foo(bar=(a is b)):\n    ...\n```\n\nException type 6:\n\n```python\n# GOOD\n[\n    \"a\",\n    (\n        \"b\"\n        \"c\"\n    ),\n    \"d\",\n]\n\n# This helps to avoid forgetting a comma at the end of a string spanning\n# multiple lines. Compare with:\n[\n    \"a\",\n    \"b\"\n    \"c\"\n    \"d\",\n]\n# Was the comma after \"b\" forgotten or was the string supposed to be \"bc\"?\n\n# BAD\n[\n    (\n        \"a\" \"b\"\n    ),\n]\n```\n\n### Footnotes:\n1. Multi-line means that either\n   * the expression spans multiple lines, e.g.,\n     ```python\n     (a\n      + b)\n     ```\n   * or the first part of the expression is on a new line (e.g., if a name is very long), e.g.,\n     ```python\n     (\n         veeeeeeeeeeery_looooooong_name\n     )\n     ```\n     but also\n     ```python\n     (\n         a\n         + b\n     )\n     ```\n   Multi-line expressions do **not** include\n   ```python\n   (a + b\n   )\n   ```\n\n## Additional Notes\n\nThis plugin was developed to improve the code quality of Neo4j Python projects.\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "flake8 plugin to nitpick about parenthesis, brackets, and braces",
    "version": "0.5.5",
    "project_urls": {
        "Changelog": "https://github.com/robsdedude/flake8-picky-parentheses/blob/master/CHANGELOG.md",
        "Homepage": "https://github.com/robsdedude/flake8-picky-parentheses",
        "Issue Tracker": "https://github.com/robsdedude/flake8-picky-parentheses/issues",
        "Source Code": "https://github.com/robsdedude/flake8-picky-parentheses"
    },
    "split_keywords": [
        "flake8",
        " plugin",
        " redundant",
        " superfluous",
        " extraneous",
        " unnecessary",
        " parentheses",
        " parenthesis",
        " parens",
        " brackets",
        " linter",
        " linting",
        " codestyle",
        " code style"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ebdc6ac2d6c746eff44b011e07a71b8e078c33d0958c55069198eef03e1d4585",
                "md5": "22bcaae3887456490b2e144df8a24f69",
                "sha256": "a0f31711fab93e90e2cef215ebb8aea081f75af5759f585dc59666315f1e76a0"
            },
            "downloads": -1,
            "filename": "flake8-picky-parentheses-0.5.5.tar.gz",
            "has_sig": false,
            "md5_digest": "22bcaae3887456490b2e144df8a24f69",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 26708,
            "upload_time": "2024-03-22T14:11:51",
            "upload_time_iso_8601": "2024-03-22T14:11:51.081788Z",
            "url": "https://files.pythonhosted.org/packages/eb/dc/6ac2d6c746eff44b011e07a71b8e078c33d0958c55069198eef03e1d4585/flake8-picky-parentheses-0.5.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-22 14:11:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "robsdedude",
    "github_project": "flake8-picky-parentheses",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "flake8-picky-parentheses"
}
        
Elapsed time: 8.40058s