From 82a26fb9a07b525f40994d12c806f067d7ca797f Mon Sep 17 00:00:00 2001 From: Sid13SST <220563227+Sid13SST@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:06:03 +0000 Subject: [PATCH] Report the right line for unparsable statements after blank lines The parser's binding for an invalid statement starts right after the previous statement, so its `original.line` points at the first of any blank or whitespace-only lines before it. The "could not parse statement starting at line N" warning used that line, so for A=1 B='oops it reported line 2 instead of line 4. Skip the newlines in the binding's leading whitespace when computing the line for the warning. `Original.line` itself is unchanged, since `set_key`/`unset_key` rely on the original chunk including those lines. --- src/dotenv/main.py | 16 ++++++++++++++-- tests/test_main.py | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 5faa7f0e..48830941 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -9,7 +9,7 @@ from contextlib import contextmanager from typing import IO, Dict, Iterable, Iterator, Mapping, Optional, Tuple, Union -from .parser import Binding, parse_stream +from .parser import Binding, Original, _newline, parse_stream from .variables import parse_variables # A type alias for a string path to be used for the paths in this file. @@ -29,12 +29,24 @@ def _load_dotenv_disabled() -> bool: return value in {"1", "true", "t", "yes", "y"} +def _statement_line(original: Original) -> int: + """ + Return the line on which the statement in `original` starts. + + `original.line` is where the chunk of text starts, which includes any + blank lines before the statement itself. + """ + string = original.string + leading = string[: len(string) - len(string.lstrip())] + return original.line + len(_newline.findall(leading)) + + def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding]: for mapping in mappings: if mapping.error: logger.warning( "python-dotenv could not parse statement starting at line %s", - mapping.original.line, + _statement_line(mapping.original), ) yield mapping diff --git a/tests/test_main.py b/tests/test_main.py index 930ab171..e04bc993 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -931,6 +931,29 @@ def test_dotenv_values_file_stream(dotenv_path): assert result == {"a": "b"} +@pytest.mark.parametrize( + "string,line", + [ + ("x y", 1), + ("a=b\nx y", 2), + ("a=b\n\n\nx y\n", 4), + ("# comment\n\n \nx y\n", 4), + ("a=b\r\n\r\nx y\r\n", 3), + ("a=b\r\rx y\r", 3), + ("a=b\n\nc='unterminated\nd=e\n", 3), + ], +) +def test_dotenv_values_invalid_line_warning_reports_statement_line(string, line): + logger = logging.getLogger("dotenv.main") + + with mock.patch.object(logger, "warning") as mock_warning: + dotenv.dotenv_values(stream=io.StringIO(string)) + + mock_warning.assert_called_once_with( + "python-dotenv could not parse statement starting at line %s", line + ) + + @pytest.mark.parametrize( "string,expected", [