Skip to content

Conversation

@Exgene
Copy link
Contributor

@Exgene Exgene commented Oct 5, 2025

Fix type parameter expansion when function parameters have trailing comma

Fixes #4740

Problem

When a function with type parameters has a trailing comma in its parameter list, Black incorrectly expanded the type parameter brackets instead of the function parameter list:

Before:

def func1[T: (int, str)](a,): ...

# Formatted to (incorrect):
def func1[
    T: (int, str)
](a,): ...

After:

def func1[T: (int, str)](a,): ...

# Formats to (correct):
def func1[T: (int, str)](
    a,
): ...

Root Cause

The issue was in left_hand_split() which processes both LSQB (square brackets for type parameters) and LPAR (parentheses for function parameters) as potential split points. When a trailing comma forced a split, it would sometimes choose the type parameter brackets instead of the function parameter parentheses.

Solution

Modified left_hand_split() to avoid selecting parentheses ( as split points when they occur inside type parameter brackets [...]. This ensures type parameters stay on the head line and the split happens at the function parameter list where the trailing comma actually exists.

Closes #4740

I need some reference on how these test cases work so if you could help me with that it would be amazing and ill add the relevant test cases for it

@tusharsadhwani
Copy link
Collaborator

Use this contributing basics section to setup the project: https://black.readthedocs.io/en/latest/contributing/the_basics.html#technicalities

and then run pytest.

For adding the new tests, follow the "Testing" section on the same page.

@tusharsadhwani
Copy link
Collaborator

Also please add a simple explanation entry in the changelog, take a peek at merged PRs to get an idea.

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

Use this contributing basics section to setup the project: https://black.readthedocs.io/en/latest/contributing/the_basics.html#technicalities

and then run pytest.

For adding the new tests, follow the "Testing" section on the same page.

Oh no i have done the testing setup and ran the tests no regressions seem to be found, I just didn't understand how the test cases quite worked so i wanted references on that to add for this particular case

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

Also please add a simple explanation entry in the changelog, take a peek at merged PRs to get an idea.

Yeah sure on it, Thanks

@tusharsadhwani
Copy link
Collaborator

From the doc:

Normally, tests should be created as files in the tests/data/cases directory. These files consist of up to three parts:
[...]

Create a new file to create a new test. You can start with just the unformatted and formatted code snippet from your PR description, and then expand from there to cover more cases.

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

From the doc:

Normally, tests should be created as files in the tests/data/cases directory. These files consist of up to three parts:
[...]

Create a new file to create a new test. You can start with just the unformatted and formatted code snippet from your PR description, and then expand from there to cover more cases.

Yeah i got it, Didn't really see the section which explains how it works, Also i don't think this minor change requires an entry in the changes.md?

@tusharsadhwani
Copy link
Collaborator

If that docs section felt like it's lacking context you can edit and improve it in another PR, happy to review.

Any change that has a user visible effect is a significant enough change to warrant a changelog entry IMO.

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

If that docs section felt like it's lacking context you can edit and improve it in another PR, happy to review.

Any change that has a user visible effect is a significant enough change to warrant a changelog entry IMO.

Yes added basic test cases and updated the changelog to reflect the issue to be fixed should be okay now, Thanks!

@tusharsadhwani
Copy link
Collaborator

Make sure not to format the test file, you seem to have changed the test file more than you intended to.

Also you should add a few more complex test cases that are adjacent to this bug, to ensure we handle those well too and don't regress in the future. Single or many item tuples for example, and maybe nested tuples.

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

Make sure not to format the test file, you seem to have changed the test file more than you intended to.

Also you should add a few more complex test cases that are adjacent to this bug, to ensure we handle those well too and don't regress in the future. Single or many item tuples for example, and maybe nested tuples.

Oh right im dumb i ran the formatter on the test files, Will revert and add more test cases and try them

@Exgene
Copy link
Contributor Author

Exgene commented Oct 5, 2025

Updated with multiple tests as well and made the required changes, Let me know if any other changes are required

@MeGaGiGaGon
Copy link
Collaborator

Thanks for the PR, I'm surprised it was that simple of a change. One more thing before this is good to go, since my previous fix for this already fixed the crash, and this code bug does not cause a crash (as far as I can tell), this will have to go in preview to prevent changing the stable style. #4764 is an example of how to do that, but the basic process is:

  1. Add a new enum variant to Preview in mode.py
  2. Use if Preview.<variant name> in self.mode to gate the new behavior behind preview
  3. Move tests to a new preview specific file starting with # flags: --preview
  4. Update future_style.md with a short description of the new mode changes
  5. Update black.schema.json with the new preview name

Sorry that it's a bit of a headache to do, but we've recently been a bit loose with the stability policy, and I've been trying to make sure we follow it more closely.

For a bit more info, here's a link to the stability policy.

@Exgene
Copy link
Contributor Author

Exgene commented Oct 6, 2025

Thanks for the PR, I'm surprised it was that simple of a change. One more thing before this is good to go, since my previous fix for this already fixed the crash, and this code bug does not cause a crash (as far as I can tell), this will have to go in preview to prevent changing the stable style. #4764 is an example of how to do that, but the basic process is:

  1. Add a new enum variant to Preview in mode.py
  2. Use if Preview.<variant name> in self.mode to gate the new behavior behind preview
  3. Move tests to a new preview specific file starting with # flags: --preview
  4. Update future_style.md with a short description of the new mode changes
  5. Update black.schema.json with the new preview name

Sorry that it's a bit of a headache to do, but we've recently been a bit loose with the stability policy, and I've been trying to make sure we follow it more closely.

For a bit more info, here's a link to the stability policy.

Yeah no problem thats honestly fair, Ill do that thanks!

@Exgene
Copy link
Contributor Author

Exgene commented Oct 6, 2025

Thanks for the PR, I'm surprised it was that simple of a change. One more thing before this is good to go, since my previous fix for this already fixed the crash, and this code bug does not cause a crash (as far as I can tell), this will have to go in preview to prevent changing the stable style. #4764 is an example of how to do that, but the basic process is:

1. Add a new enum variant to `Preview` in `mode.py`

2. Use `if Preview.<variant name> in self.mode` to gate the new behavior behind preview

3. Move tests to a new preview specific file starting with `# flags: --preview`

4. Update `future_style.md` with a short description of the new mode changes

5. Update `black.schema.json` with the new preview name

Sorry that it's a bit of a headache to do, but we've recently been a bit loose with the stability policy, and I've been trying to make sure we follow it more closely.

For a bit more info, here's a link to the stability policy.

How do i gate this cause i can't really access the self.mode in linegen.py where the only change is outside the class i think?

@Exgene
Copy link
Contributor Author

Exgene commented Oct 6, 2025

@MeGaGiGaGon Let me know if this works

@Exgene
Copy link
Contributor Author

Exgene commented Oct 6, 2025

@MeGaGiGaGon Yes done

@Exgene
Copy link
Contributor Author

Exgene commented Oct 6, 2025

Oh right it won't working on python version below 3.12 right i should update that for the test cases

@MeGaGiGaGon MeGaGiGaGon merged commit 8203030 into psf:main Oct 6, 2025
1 of 2 checks passed
luketainton pushed a commit to luketainton/roboluke-tasks that referenced this pull request Nov 10, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [black](https://github.com/psf/black) ([changelog](https://github.com/psf/black/blob/main/CHANGES.md)) | `<25.9.1,>=25.9.0` -> `<25.11.1,>=25.11.0` | [![age](https://developer.mend.io/api/mc/badges/age/pypi/black/25.11.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/black/25.9.0/25.11.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>psf/black (black)</summary>

### [`v25.11.0`](https://github.com/psf/black/blob/HEAD/CHANGES.md#25110)

[Compare Source](psf/black@25.9.0...25.11.0)

##### Highlights

- Enable base 3.14 support ([#&#8203;4804](psf/black#4804))
- Add support for the new Python 3.14 t-string syntax introduced by PEP 750 ([#&#8203;4805](psf/black#4805))

##### Stable style

- Fix bug where comments between `# fmt: off` and `# fmt: on` were reformatted ([#&#8203;4811](psf/black#4811))
- Comments containing fmt directives now preserve their exact formatting instead of
  being normalized ([#&#8203;4811](psf/black#4811))

##### Preview style

- Move `multiline_string_handling` from `--unstable` to `--preview` ([#&#8203;4760](psf/black#4760))
- Fix bug where module docstrings would be treated as normal strings if preceded by
  comments ([#&#8203;4764](psf/black#4764))
- Fix bug where python 3.12 generics syntax split line happens weirdly ([#&#8203;4777](psf/black#4777))
- Standardize type comments to form `# type: <value>` ([#&#8203;4645](psf/black#4645))
- Fix `fix_fmt_skip_in_one_liners` preview feature to respect `# fmt: skip` for compound
  statements with semicolon-separated bodies ([#&#8203;4800](psf/black#4800))

##### Configuration

- Add `no_cache` option to control caching behavior. ([#&#8203;4803](psf/black#4803))

##### Packaging

- Releases now include arm64 Linux binaries ([#&#8203;4773](psf/black#4773))

##### Output

- Write unchanged content to stdout when excluding formatting from stdin using pipes
  ([#&#8203;4610](psf/black#4610))

##### *Blackd*

- Implemented BlackDClient. This simple python client allows to easily send formatting
  requests to blackd ([#&#8203;4774](psf/black#4774))

##### Integrations

- Enable 3.14 base CI ([#&#8203;4804](psf/black#4804))
- Enhance GitHub Action `psf/black` to support the `required-version` major-version-only
  "stability" format when using pyproject.toml ([#&#8203;4770](psf/black#4770))
- Improve error message for vim plugin users. It now handles independently vim version
- Vim: Warn on unsupported Vim and Python versions independently ([#&#8203;4772](psf/black#4772))
- Vim: Print the import paths when importing black fails ([#&#8203;4675](psf/black#4675))
- Vim: Fix handling of virtualenvs that have a different Python version ([#&#8203;4675](psf/black#4675))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi4yLjAiLCJ1cGRhdGVkSW5WZXIiOiI0Mi4yLjAiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbInR5cGUvZGVwZW5kZW5jaWVzIl19-->

Reviewed-on: https://git.tainton.uk/repos/roboluke/pulls/393
Co-authored-by: renovate[bot] <[email protected]>
Co-committed-by: renovate[bot] <[email protected]>
luketainton pushed a commit to luketainton/epage that referenced this pull request Nov 10, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [black](https://github.com/psf/black) ([changelog](https://github.com/psf/black/blob/main/CHANGES.md)) | `<25.9.1,>=25.9.0` -> `<25.11.1,>=25.11.0` | [![age](https://developer.mend.io/api/mc/badges/age/pypi/black/25.11.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/black/25.9.0/25.11.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>psf/black (black)</summary>

### [`v25.11.0`](https://github.com/psf/black/blob/HEAD/CHANGES.md#25110)

[Compare Source](psf/black@25.9.0...25.11.0)

##### Highlights

- Enable base 3.14 support ([#&#8203;4804](psf/black#4804))
- Add support for the new Python 3.14 t-string syntax introduced by PEP 750 ([#&#8203;4805](psf/black#4805))

##### Stable style

- Fix bug where comments between `# fmt: off` and `# fmt: on` were reformatted ([#&#8203;4811](psf/black#4811))
- Comments containing fmt directives now preserve their exact formatting instead of
  being normalized ([#&#8203;4811](psf/black#4811))

##### Preview style

- Move `multiline_string_handling` from `--unstable` to `--preview` ([#&#8203;4760](psf/black#4760))
- Fix bug where module docstrings would be treated as normal strings if preceded by
  comments ([#&#8203;4764](psf/black#4764))
- Fix bug where python 3.12 generics syntax split line happens weirdly ([#&#8203;4777](psf/black#4777))
- Standardize type comments to form `# type: <value>` ([#&#8203;4645](psf/black#4645))
- Fix `fix_fmt_skip_in_one_liners` preview feature to respect `# fmt: skip` for compound
  statements with semicolon-separated bodies ([#&#8203;4800](psf/black#4800))

##### Configuration

- Add `no_cache` option to control caching behavior. ([#&#8203;4803](psf/black#4803))

##### Packaging

- Releases now include arm64 Linux binaries ([#&#8203;4773](psf/black#4773))

##### Output

- Write unchanged content to stdout when excluding formatting from stdin using pipes
  ([#&#8203;4610](psf/black#4610))

##### *Blackd*

- Implemented BlackDClient. This simple python client allows to easily send formatting
  requests to blackd ([#&#8203;4774](psf/black#4774))

##### Integrations

- Enable 3.14 base CI ([#&#8203;4804](psf/black#4804))
- Enhance GitHub Action `psf/black` to support the `required-version` major-version-only
  "stability" format when using pyproject.toml ([#&#8203;4770](psf/black#4770))
- Improve error message for vim plugin users. It now handles independently vim version
- Vim: Warn on unsupported Vim and Python versions independently ([#&#8203;4772](psf/black#4772))
- Vim: Print the import paths when importing black fails ([#&#8203;4675](psf/black#4675))
- Vim: Fix handling of virtualenvs that have a different Python version ([#&#8203;4675](psf/black#4675))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi4yLjAiLCJ1cGRhdGVkSW5WZXIiOiI0Mi4yLjAiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbInR5cGUvZGVwZW5kZW5jaWVzIl19-->

Reviewed-on: https://git.tainton.uk/repos/epage/pulls/176
Co-authored-by: renovate[bot] <[email protected]>
Co-committed-by: renovate[bot] <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: fails to properly format constrained generics with new 3.12 syntax

3 participants