Infer type from default if provided but type omitted#3
Conversation
|
It might also be an idea to allow interpretation of the default value. with: Which will then be further evaluated/converted to the desired type. Allowing (when LOST is not set as an environment variable): Then, for some more corner cases, also change: into: As that will allow: And... in that case (using a dict as default) would also works when the environment value is set to something that can be parsed into a dictionary. |
|
Then there are some corner cases to consider... as Python evaluates bool([]), bool({}), and bool(None) as False, it might be correct to change: into: Leaving yet another corner case... as getenv('LOST', type=bool, default=None) gets a shortcut when LOST is not set as an environment variable. Which requires the earlier: to be changed to: |
|
To summarize my proposals into one: But please note that I've not adjusted (or even run) the tests. Thanks to the PR author and the original code for the concept and I hope my comments help you or anyone else to use the idea. As the PR author also said:
that also applies for my suggestions. |
|
Thanks for the ideas @mdxs , they seem very reasonable! I can't dig into in right now, but i definitely will do it soon |
|
No worries, here is an even further updated version; as I noted that Again, please note that I didn't try the existing test cases, nor did I add new ones... sorry about that. |
This change proposition adds inferring type from default value. It does that only when type keyword argument was not provided. Proper tests were added.
Oh, and dropped using .pop as it seems just unnecessary.
Feel free to pick only some part of this, or none, no pressure ;)
Cheers
PS: I'm not sure if I should update version..