Skip to content

Report the right line for unparsable statements after blank lines - #756

Open
Sid13SST wants to merge 1 commit into
theskumar:mainfrom
Sid13SST:fix/invalid-line-warning-number
Open

Sid13SST wants to merge 1 commit into
theskumar:mainfrom
Sid13SST:fix/invalid-line-warning-number

Conversation

@Sid13SST

Copy link
Copy Markdown

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:

A=1


B='oops

B='oops is on line 4, but the warning says line 2 (reproduced on unmodified main). The parser's record for each statement begins right after the previous one, so its original.line counts the leading blank lines.

Change: src/dotenv/main.py gets 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 left Original.line alone on purpose, since set_key/unset_key rely on the original chunk including those lines.

Tests: new parametrized test_dotenv_values_invalid_line_warning_reports_statement_line in tests/test_main.py with 7 cases (\n, \r\n, \r, comment then blank lines, whitespace-only line, unterminated quote). Before the fix, pytest -k invalid_line_warning gives 5 failed, 2 passed. After, 7 passed. Full suite: 290 passed, 6 skipped. ruff check and ruff format --check pass; 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.

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

No deployments
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.

1 participant