New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1458 - Allow for custom parsing of environment variables via parse_env_var in Config object #4406
Conversation
… via env_parse
52d5689
to
4b2c372
Compare
please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, I think this looks great.
Please update.
@classmethod | ||
def parse_env_var(cls, field_name: str, raw_val: str) -> Any: | ||
if field_name == 'numbers': | ||
return parse_list(raw_val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return parse_list(raw_val) | |
return [int(x) for x in s.split(',')] |
then remove parse_list
above, I think this is just as easy to read and more compact.
I don't think you need the strip since int(' 123 ') == 123
For the sake of speed, I've made the alterations I suggested - AFAIK this is the last change before V1.10 beta is released. 🎉 LGTM, @PrettyWood @hramezani please review. |
This is great 👍 I just left a small comment other than that LGTM 🚀 |
Okay, I'm going to go for it. I want to get the beta out today. @PrettyWood if you see anything important, we can create another pR. @acmiyaguchi thanks so much for this. |
Thank you for the review and accepting the patch into v1.10 beta! I appreciate Pydantic and look forward to seeing it mature. |
Thanks. Somehow the change for this feature didn't get included in the changelog for v1.10a1, but the feature is there. The change will be included in the changelog for the next (pre) release. |
Change Summary
This PR adds an
parse_env_var
classmethod into the Config object to allow for custom parsing of the environment within Settings. This is useful when specifying a custom format in environment strings e.g. lists (1,2,3) or key-value pairs (a=1,b=2,c=3). String-encoded JSON is not ideal in certain situations, such as when the environment is specified inside of a YAML file (leading to string-escaped string-encoded JSON).This is an alternative to #3977
Related issue number
fixes #1458
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)