Repository navigation
Conversation
_preprocess_dict split each entry with str.split(key_value_delimiter, 1) and unpacked the result into key, val. When an entry lacked the delimiter (a trailing delimiter, an empty entry, or a bare key) the split returned a single-element list and the unpacking raised a bare ValueError, which is not an EnvValidationError and so bypassed the library's error handling entirely. Iterate explicitly and raise marshmallow.ValidationError for any entry missing the key-value delimiter, so malformed input is reported through the normal EnvValidationError path.
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.
Problem
env.dictraises an uncaughtValueError(rather than the library'sEnvValidationError) whenever an entry is missing the key-value delimiter.This includes very common inputs such as a trailing delimiter:
The same happens for an empty entry (
"a=1,,b=2") and for a bare key with nodelimiter (
"foo"). Because aValueErroris not anEnvValidationError, itbypasses the library's error handling entirely: it is not converted into an
EnvValidationErrorwheneager=True, and it is not accumulated intoenv.seal()'s error report wheneager=False(it propagates out of the loopinstead).
Root cause
src/environs/__init__.py,_preprocess_dictbuilt the result with a dictcomprehension:
str.split(key_value_delimiter, 1)returns a single-element list when thedelimiter is absent, so unpacking it into
key, valraisesValueError.Everything in
_field2methodthat turns parse failures intoEnvValidationErroronly catches
marshmallow.ValidationError, so theValueErrorescapes.Fix
Iterate explicitly and raise
marshmallow.ValidationErrorfor any entry thatdoes not contain the key-value delimiter. This is the same exception type the
rest of the parsing path already raises, so it is correctly converted into
EnvValidationError(eager) or collected into the deferred error report(non-eager), and the message names the offending entry.
Test
tests/test_environs.py::TestCasting::test_invalid_dict_raises_validation_errorparametrizes a trailing delimiter, an empty entry, and a bare key, asserting
each raises
environs.EnvValidationError. All three fail on the currentmain(
ValueError: not enough values to unpack) and pass with the fix. The existingenv.dicttests continue to pass.