Repository navigation
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When python-dotenv cannot parse a line, it logs "could not parse statement starting at line N". If blank or whitespace-only lines sit just before the bad statement, N is wrong: it points at the first blank line instead of the statement. For example:
B='oopsis on line 4, but the warning says line 2 (reproduced on unmodifiedmain). The parser's record for each statement begins right after the previous one, so itsoriginal.linecounts the leading blank lines.Change:
src/dotenv/main.pygets a small helper,_statement_line, that skips the newlines in that leading whitespace using the parser's own line-break rule (\n,\r\n,\r). Only the warning uses it. I leftOriginal.linealone on purpose, sinceset_key/unset_keyrely on the original chunk including those lines.Tests: new parametrized
test_dotenv_values_invalid_line_warning_reports_statement_lineintests/test_main.pywith 7 cases (\n,\r\n,\r, comment then blank lines, whitespace-only line, unterminated quote). Before the fix,pytest -k invalid_line_warninggives 5 failed, 2 passed. After, 7 passed. Full suite: 290 passed, 6 skipped.ruff checkandruff format --checkpass; mypy on 3.13 and 3.15 is clean. mypy on 3.10 fails in the sandbox from a numpy stub, and fails the same way without this change. Only run on Python 3.13, Linux.I searched open and closed issues and PRs for line numbers and parse warnings and found no duplicate. I did not add a CHANGELOG entry because entries cite a PR number; I can add one if you want it.
AI disclosure: Claude (Opus 5.5, Anthropic), run through Claude Code, found the bug, wrote the fix and tests, and drafted this description. The test runs above were done by the assistant in a cloud sandbox. A person has not reviewed the diff line by line, and I will respond to review feedback.