Skip to content

AI spam#6060

Closed
JSap0914 wants to merge 1 commit into
pallets:mainfrom
JSap0914:fix/from-prefixed-env-non-dict-intermediate
Closed

AI spam#6060
JSap0914 wants to merge 1 commit into
pallets:mainfrom
JSap0914:fix/from-prefixed-env-non-dict-intermediate

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

Config.from_prefixed_env silently crashes with an opaque TypeError when a flat env var and a nested env var share the same config key prefix.

Root cause: env vars are loaded in sorted() order, so FLASK_FOO=bar is always processed before FLASK_FOO__X=1. After the flat key sets self["FOO"] = "bar", the nested traversal does:

current = self["FOO"]   # "bar" (str)
current["X"] = value    # TypeError: 'str' object does not support item assignment

The internal TypeError gives no hint which env var caused the conflict.

Fix

Add an isinstance check before descending into the intermediate dict. When the existing value is not a dict, raise a ValueError that names the conflicting environment variable.

Verification

$ pytest tests/test_config.py -v -k prefixed
tests/test_config.py::test_from_prefixed_env PASSED
tests/test_config.py::test_from_prefixed_env_custom_prefix PASSED
tests/test_config.py::test_from_prefixed_env_nested PASSED
tests/test_config.py::test_from_prefixed_env_non_dict_intermediate PASSED
4 passed in 0.07s

AI-assisted contribution.

…dict

When a flat env var (e.g. FLASK_FOO=bar) is processed before a nested
env var with the same prefix (e.g. FLASK_FOO__X=1) -- which is
guaranteed by sorted() order -- the traversal code does:

    current = self['FOO']   # 'bar' (str)
    current['X'] = 1        # TypeError: 'str' object does not support
                            #            item assignment

Replace the opaque TypeError with a ValueError that names the
conflicting environment variable, making the misconfiguration obvious
to the caller.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@davidism davidism closed this Jun 17, 2026
@davidism davidism changed the title raise ValueError in from_prefixed_env when intermediate key is not a dict AI spam Jun 17, 2026
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.

3 participants