Add DetailedConverter for advanced environment variable conversion #3079
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.
🔥 Real-Life Horror Story: Config Edition
We have an app that accepts hexadecimal prefixes (0–f) as a configuration parameter. To support environment-based configuration, we used Sanic’s built-in environment variable parsing by setting SANIC_PREFIX with values from 0 to f.
You can imagine my surprise when I saw this in one of 16 pods running our app:
After digging into the documentation, I discovered that Sanic automatically interprets 'f' as bool type False.
Even more surprising was that Sanic's config parsing does not respect the types defined in the defaults dictionary. Also, custom converters only receive the value, without access to the associated key or default—making proper type handling impossible.
To solve this, I implemented a custom DetailedConverter that provides access to the full key, the config key, the raw value, and the defaults. This allows us to:
This enforces strong typing based on your default config and avoids silent issues like the "f" → False coercion.
Below solution for my issue: