google / google/yapf

DEDENT_CLOSING_BRACKETS does not work if return type has bracket

Open
#1,012 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

I believe this is a bug, but would appreciate being corrected if it is not. Using `yapf` version 0.32.0, with the following options (see below for full `pyproject.toml`:
```
column_limit = 80
dedent_closing_brackets = true
split_all_comma_separated_values = true
```

I'm formatting this function:
```python
def really_really_very_long_method_name(foo: list, bar: list, baz: list) -> list[int]:
pass
```

Expecting this:
```python
def really_really_very_long_method_name(
foo: list,
bar: list,
baz: list
) -> list[int]:
pass

```

but getting this:

```python
def really_really_very_long_method_name(foo: list,
bar: list,
baz: list) -> list[int]:
pass
```

I believe the issue is with the square brackets contained in the return type `list[int]`. This issue also does not occur if we add a comma to the last argument. I've also tried turning off that heuristic to no avail.

### More information

Test cases and full pyproject.toml
Full test cases (`main.py`):

```python
def really_really_very_long_method_name(foo: list, bar: list, baz: list) -> list[int]:
pass

def really_really_very_long_method_name_1(foo: list, bar: list, b: list[int]) -> list:
pass

def really_really_very_long_method_name_2(foo: list, bar: list, b: list) -> list:
pass

def really_really_very_long_method_name_3(foo: list, bar: list, bazbaz: list) -> list:
pass
```

Expected results:
```python
def really_really_very_long_method_name(
foo: list,
bar: list,
baz: list
) -> list[int]:
pass

def really_really_very_long_method_name_1(
foo: list,
bar: list,
b: list[int]
) -> list:
pass

def really_really_very_long_method_name_2(
foo: list,
bar: list,
b: list
) -> list:
pass

def really_really_very_long_method_name_3(
foo: list,
bar: list,
bazbaz: list
) -> list:
pass
```

Actual results:

```python
def really_really_very_long_method_name(foo: list,
bar: list,
baz: list) -> list[int]:
pass

def really_really_very_long_method_name_1(
foo: list,
bar: list,
b: list[int]
) -> list:
pass

def really_really_very_long_method_name_2(
foo: list,
bar: list,
b: list
) -> list:
pass

def really_really_very_long_method_name_3(
foo: list,
bar: list,
bazbaz: list
) -> list:
pass
```

entire `pyproject.toml`:
```toml
[tool.poetry]
name = "python-template"
version = "0.1.0"
description = ""
authors = ["Your Name "]

[tool.poetry.dependencies]
python = ">=3.8.0,<3.9"
numpy = "^1.22.2"
replit = "^3.2.4"
yapf = "^0.32.0"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.yapf]
column_limit = 80
dedent_closing_brackets = true
split_all_comma_separated_values = true
```

### Online repro

https://replit.com/@radusuciu/yapf-DEDENTCLOSINGBRACKETS-repro#main.py

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.