Visitar URL original
`--line-ranges` fails to parse when an @overload function is inside a class · Issue #4818 · psf/black · GitHub
Skip to content

--line-ranges fails to parse when an @overload function is inside a class #4818

Closed as not planned

Description

@jericjan

Describe the bug

When an @overload function is inside a class, the parser bugs out only when using the --line-ranges argument. This bug does not happen when the @overload function is outside of a class however.

To Reproduce

Take this code:

from typing import Union, overload


class Test:

    @overload
    def test(self, a: str, b: str) -> str: ...

    @overload
    def test(self, a: str, b: int) -> int: ...

    def test(self, a: str, b: Union[str, int]):
        print(a)
        return b

Run with args --line-ranges 12-13:

2025-10-30 12:12:18.172 [info] error: cannot format test.py: Cannot parse for target version Python 3.13: 6:17:     @overload    def test(self, a: str, b: str) -> str: ...

Oh no! 💥 💔 💥
1 file failed to reformat.

Run with args --line-ranges 6-7:

2025-10-30 12:15:51.626 [info] All done! ✨ 🍰 ✨
1 file left unchanged.

Run with no args:

2025-10-30 12:13:14.575 [info] All done! ✨ 🍰 ✨
1 file left unchanged.

Expected behavior
It should have the same output as running without args.

Environment

  • Black's version: 25.9.0
  • OS and Python version: Windows 10, Python 3.9.2

Additional context

#4669 might be worth mentioning, since it's a similar variant of this bug.

Activity

  1. MeGaGiGaGon commented on Oct 30, 2025

    @MeGaGiGaGon
    Collaborator

    I can't reproduce on latest, please double check that you are actually running 25.9.0, instead of 25.1.0. I can reproduce on 25.1.0, but I believe I fully fixed this in #4670. More specifically, does running black --version for you give 25.9.0?

    Console logs of non-reproduction

    I'm also on Windows 10, so I think this should be an accurate repro attempt.

    PS D:\python_projects> echo @'
    from typing import Union, overload
    
    
    class Test:
    
        @overload
        def test(self, a: str, b: str) -> str: ...
    
        @overload
        def test(self, a: str, b: int) -> int: ...
    
        def test(self, a: str, b: Union[str, int]):
            print(a)
            return b
    '@ > issue.py
    PS D:\python_projects> uvx --python 3.9.2 black@25.1.0 --line-ranges 12-13 .\issue.py
    error: cannot format issue.py: Cannot parse: 6:17:     @overload    def test(self, a: str, b: str) -> str: ...
    
    Oh no! 💥 💔 💥
    1 file failed to reformat.
    PS D:\python_projects> uvx --python 3.9.2 black@25.9.0 --line-ranges 12-13 .\issue.py
    All done! ✨ 🍰 ✨
    1 file left unchanged.

    Edit: I also tried a couple more things just to see if I could repro, but still couldn't. I tried scanning the line range through the file from 0-16, and repeated on unstable, but still couldn't reproduce.

  2. jericjan commented on Oct 30, 2025

    @jericjan
    Author

    Oh, I found the culprit. An oversight on my part. The Black Formatter extension for VSCode was using the bundled version for Black, which was still on 25.1.0. The logs were also misleading, saying that it ran .venv\Scripts\python.exe -m black while the traceback showed the file location of the bundled version.

    I set this in VSCode's settings.json and it finally worked:

    "black-formatter.path": [
      "black"
    ]
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions