Skip to content

Stop python-dotenv warning about flexmeasures.cfg on startup - #2845

Merged
Flix6x merged 6 commits into
mainfrom
fix/cfg-dotenv-warning
Nov 10, 2026
Merged

Flix6x merged 6 commits into
mainfrom
fix/cfg-dotenv-warning

Conversation

@Flix6x

@Flix6x Flix6x commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Description

Since v1.1.0, every container of the Docker image logs WARNING: python-dotenv could not parse statement starting at line 1 on startup.

  • get_flexmeasures_env() looked up FLEXMEASURES_ENV in flexmeasures.cfg with dotenv_values(). That file is Python, so python-dotenv warns about any line that is not a plain KEY = value assignment, such as from datetime import timedelta on line 1. Spaces around = are fine for dotenv; imports and other statements are not.
  • The bug is old (Drop need for .env #1759), but it only showed up now: between Speed up container startup and app boot (Docker, gunicorn, altair) #2512 and Docker image: read the mounted config again, restore gunicorn's logs, and let pip install into the venv #2811 the image did not find the config mounted at /app/instance, so there was nothing to parse. Docker image: read the mounted config again, restore gunicorn's logs, and let pip install into the venv #2811 (in v1.1.0) fixed that.
  • Now the cfg is parsed with ast, without running it. Only a top-level string literal assigned to FLEXMEASURES_ENV counts. The last top-level assignment wins, as it does when the file runs, so a later expression undoes an earlier literal. An expression is no longer mistaken for a string.
  • The file is parsed as bytes, so a byte order mark (as some editors save) or a coding declaration works, as it does when the file runs.
  • A cfg that exists but cannot be read or parsed, or that sets FLEXMEASURES_ENV to something other than a string literal, now logs a warning instead of silently falling back to the default environment. A missing cfg stays silent.
  • The cfg is now only read when no FLEXMEASURES_ENV environment variable is set and the app is not in testing mode.
  • Added changelog item in documentation/changelog.rst

How to test

pytest flexmeasures/utils/tests/test_config_utils.py

  • test_flexmeasures_env_from_a_python_cfg_raises_no_dotenv_warning fails on main with the very warning from the logs (dotenv.main, "python-dotenv could not parse statement starting at line %s"), and passes here.
  • test_read_flexmeasures_env_from_cfg covers plain and annotated assignments, last-one-wins (also when the last one is an expression), expressions, an absent setting, a file that is not Python, and a BOM with Windows line endings.
  • test_read_flexmeasures_env_from_cfg_warns_when_it_cannot_tell and ..._does_not_warn_about_a_plain_cfg cover the warnings.
  • Each fix was checked to turn its test red when undone.

Further Improvements

python-dotenv stays a dependency: the flask CLI uses it to read .flaskenv.
This could go into a v1.1.1 patch release.

Related Items

Regression surfaced by #2811.

🤖 Generated with Claude Code

Flix6x and others added 2 commits November 9, 2026 12:43
…v file

The config file is Python, so python-dotenv warned "could not parse statement" about any line that is not a plain assignment, such as an import on its first line.
Since #2811 the Docker image finds its mounted config again, so every container logged this warning on startup.
Now the file is parsed with ast, without running it, and only a top-level string literal counts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@read-the-docs-community

read-the-docs-community Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Comment thread flexmeasures/utils/config_utils.py Resolved
@Flix6x Flix6x self-assigned this Nov 10, 2026
Flix6x and others added 3 commits November 10, 2026 13:35
…e API

The generate-openapi-specs hook stamped it with the development version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… mark

Parsing the decoded text kept the BOM, so the parse failed and the app silently ran in production; parsing the bytes honours a BOM and a coding declaration, as running the file does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… let its last assignment win

A cfg that cannot be read or parsed, or that sets FLEXMEASURES_ENV to an expression, used to fall back to the default environment silently.
A later assignment of an expression now also undoes an earlier string literal, as it does when the file runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x added bug Something isn't working installation labels Nov 10, 2026
@Flix6x Flix6x added this to the 1.2.0 milestone Nov 10, 2026
Signed-off-by: F.N. Claessen <claessen@seita.nl>

# Conflicts:
#	documentation/changelog.rst
@Flix6x
Flix6x merged commit 4c442ec into main Nov 10, 2026
11 checks passed
@Flix6x
Flix6x deleted the fix/cfg-dotenv-warning branch November 10, 2026 13:06
@Flix6x Flix6x modified the milestones: 1.2.0, 1.1.1 Nov 11, 2026
Flix6x added a commit that referenced this pull request Nov 11, 2026
The v1.1.1 patch release backports #2855, #2861, #2838, #2829, #2851, #2845, #2859, #2850 and #2860 to the 1.1.x branch.
Move their changelog bullets out of the v1.2.0 section into a new v1.1.1 section, worded and ordered as on 1.1.x,
so the same fixes are not listed as unreleased once 1.1.1 ships.
The bullet for #2861 takes the 1.1.x wording, which adds that Python 3.10 keeps the earlier Pillow, dnspython and filelock.
The API change log needs no split: v3.0-35 holds only #2859, which ships in v1.1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Flix6x added a commit that referenced this pull request Nov 11, 2026
…on startup (#2845)

(cherry picked from commit 4c442ec, leaving out its changelog entry)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Flix6x added a commit that referenced this pull request Nov 11, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working installation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant