From 8e1452d466a65a01ce2321512f4dc36e3daf7735 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Sun, 15 Feb 2026 15:04:19 +0530 Subject: [PATCH 01/10] enhance: enhancement of the syntex error reporting --- src/black/__init__.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 6eece7eada2..7fb388f8290 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1223,10 +1223,27 @@ def _format_str_once( normalized_contents, _, newline_type = decode_bytes( src_contents.encode("utf-8"), mode, encoding_overwrite="utf-8" ) + try: + src_node = lib2to3_parse( + normalized_contents.lstrip(), target_versions=mode.target_versions, + ) + except InvalidInput as e: + msg = str(e) + try: + parts = msg.split(":") + lineno = int(parts[-3]) + col = int(parts[-2]) + except(ValueError, IndexError): + lineno, col = 1,1 + + # getting error line + lines = normalized_contents.splitlines() + line_content = lines[lineno-1] if 0 < lineno <= len(lines) else "" + + # use caret '^' to point where the error is\ + print(f"SyntexError is in line {lineno}, column {col}:\n {line_content}\n {'^'.rjust(col)}") + raise SystemExit(1) from None - src_node = lib2to3_parse( - normalized_contents.lstrip(), target_versions=mode.target_versions - ) dst_blocks: list[LinesBlock] = [] if mode.target_versions: From babcc3f607574e8ff49c94387bb719f0b9e79dc9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Feb 2026 09:49:45 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 7fb388f8290..f9f54e443f1 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1240,7 +1240,7 @@ def _format_str_once( lines = normalized_contents.splitlines() line_content = lines[lineno-1] if 0 < lineno <= len(lines) else "" - # use caret '^' to point where the error is\ + # use caret '^' to point where the error is\ print(f"SyntexError is in line {lineno}, column {col}:\n {line_content}\n {'^'.rjust(col)}") raise SystemExit(1) from None From 81c1e4367f9aae62b4a16d9e2735304d61c84ccf Mon Sep 17 00:00:00 2001 From: Sharfan Saleem Date: Sun, 15 Feb 2026 21:47:13 +0530 Subject: [PATCH 03/10] Update src/black/__init__.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/black/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index f9f54e443f1..77e170ec7a6 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1226,7 +1226,7 @@ def _format_str_once( try: src_node = lib2to3_parse( normalized_contents.lstrip(), target_versions=mode.target_versions, - ) + ) except InvalidInput as e: msg = str(e) try: From d98371912e2392cedf675ab0dfc7dbea4dac12ab Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Tue, 17 Feb 2026 13:48:31 +0530 Subject: [PATCH 04/10] remove the __init__.py exception code and add it in lib2to3_parse function --- src/black/__init__.py | 6 ++++++ src/black/parsing.py | 6 +++++- test.py | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test.py diff --git a/src/black/__init__.py b/src/black/__init__.py index 77e170ec7a6..08eb082b39e 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1223,6 +1223,7 @@ def _format_str_once( normalized_contents, _, newline_type = decode_bytes( src_contents.encode("utf-8"), mode, encoding_overwrite="utf-8" ) +<<<<<<< HEAD try: src_node = lib2to3_parse( normalized_contents.lstrip(), target_versions=mode.target_versions, @@ -1243,7 +1244,12 @@ def _format_str_once( # use caret '^' to point where the error is\ print(f"SyntexError is in line {lineno}, column {col}:\n {line_content}\n {'^'.rjust(col)}") raise SystemExit(1) from None +======= +>>>>>>> 77405eb (remove the __init__.py exception code and add it in lib2to3_parse function) + src_node = lib2to3_parse( + normalized_contents.lstrip(), target_versions=mode.target_versions + ) dst_blocks: list[LinesBlock] = [] if mode.target_versions: diff --git a/src/black/parsing.py b/src/black/parsing.py index 46c6d1d8f70..c05082ce480 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -80,9 +80,13 @@ def lib2to3_parse( faulty_line = lines[lineno - 1] except IndexError: faulty_line = "" + + caret_line = " " * (column - 1) + "^" + errors[grammar.version] = InvalidInput( - f"Cannot parse{tv_str}: {lineno}:{column}: {faulty_line}" + print(f"SyntexError is in line {lineno}, column {column}:\n {faulty_line}\n {'^'.rjust(column)}") ) + except TokenError as te: # In edge cases these are raised; and typically don't have a "faulty_line". diff --git a/test.py b/test.py new file mode 100644 index 00000000000..7b562971104 --- /dev/null +++ b/test.py @@ -0,0 +1,5 @@ +def my func() + pass + + +x = [1, 2, 3] From 27c5b62d0e9cb4924ca99f1f631678d62f94bc42 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Tue, 17 Feb 2026 13:57:04 +0530 Subject: [PATCH 05/10] remove the __init__.py exception code and add it in lib2to3_parse function --- src/black/__init__.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 08eb082b39e..6eece7eada2 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1223,29 +1223,6 @@ def _format_str_once( normalized_contents, _, newline_type = decode_bytes( src_contents.encode("utf-8"), mode, encoding_overwrite="utf-8" ) -<<<<<<< HEAD - try: - src_node = lib2to3_parse( - normalized_contents.lstrip(), target_versions=mode.target_versions, - ) - except InvalidInput as e: - msg = str(e) - try: - parts = msg.split(":") - lineno = int(parts[-3]) - col = int(parts[-2]) - except(ValueError, IndexError): - lineno, col = 1,1 - - # getting error line - lines = normalized_contents.splitlines() - line_content = lines[lineno-1] if 0 < lineno <= len(lines) else "" - - # use caret '^' to point where the error is\ - print(f"SyntexError is in line {lineno}, column {col}:\n {line_content}\n {'^'.rjust(col)}") - raise SystemExit(1) from None -======= ->>>>>>> 77405eb (remove the __init__.py exception code and add it in lib2to3_parse function) src_node = lib2to3_parse( normalized_contents.lstrip(), target_versions=mode.target_versions From 3c545c97fda7f7b47f5308c3374287a9a2bcfc62 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 08:28:06 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/parsing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index c05082ce480..5a5a56f651c 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -86,7 +86,7 @@ def lib2to3_parse( errors[grammar.version] = InvalidInput( print(f"SyntexError is in line {lineno}, column {column}:\n {faulty_line}\n {'^'.rjust(column)}") ) - + except TokenError as te: # In edge cases these are raised; and typically don't have a "faulty_line". From ffd73164daeda530602f754fcc9a1fae41bd72d4 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Mon, 23 Feb 2026 22:45:36 +0530 Subject: [PATCH 07/10] Remove invalid test file --- test.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index 7b562971104..00000000000 --- a/test.py +++ /dev/null @@ -1,5 +0,0 @@ -def my func() - pass - - -x = [1, 2, 3] From 6f721bf7cd67813629ccb9b4f9bb4221b97ea943 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Mon, 23 Feb 2026 23:10:24 +0530 Subject: [PATCH 08/10] fix error --- src/black/parsing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 5a5a56f651c..5b9c0d23eaa 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -81,8 +81,6 @@ def lib2to3_parse( except IndexError: faulty_line = "" - caret_line = " " * (column - 1) + "^" - errors[grammar.version] = InvalidInput( print(f"SyntexError is in line {lineno}, column {column}:\n {faulty_line}\n {'^'.rjust(column)}") ) From 41592ca20356c1ca2f5197552a5115ad1f1175a3 Mon Sep 17 00:00:00 2001 From: Mohamed Sharfan Date: Mon, 23 Feb 2026 23:19:00 +0530 Subject: [PATCH 09/10] fix errors --- src/black/parsing.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 5b9c0d23eaa..7362fdcf2c5 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -80,12 +80,14 @@ def lib2to3_parse( faulty_line = lines[lineno - 1] except IndexError: faulty_line = "" - - errors[grammar.version] = InvalidInput( - print(f"SyntexError is in line {lineno}, column {column}:\n {faulty_line}\n {'^'.rjust(column)}") - ) - - + caret_line = "^".rjust(column) + message = ( + f"SyntaxError is in line {lineno}, column {column}:\n" + f" {faulty_line}\n" + f" {caret_line}" + ) + errors[grammar.version] = InvalidInput(message) + except TokenError as te: # In edge cases these are raised; and typically don't have a "faulty_line". lineno, column = te.args[1] From f4666baf2520af330b7e94c0babf42ec6a853be2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Feb 2026 17:49:30 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/parsing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 7362fdcf2c5..aad4ae7b7f7 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -85,9 +85,9 @@ def lib2to3_parse( f"SyntaxError is in line {lineno}, column {column}:\n" f" {faulty_line}\n" f" {caret_line}" - ) + ) errors[grammar.version] = InvalidInput(message) - + except TokenError as te: # In edge cases these are raised; and typically don't have a "faulty_line". lineno, column = te.args[1]