-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
feat: adding json, yaml and toml sources #211
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #211 +/- ##
==========================================
+ Coverage 97.61% 97.63% +0.01%
==========================================
Files 5 5
Lines 336 423 +87
Branches 71 89 +18
==========================================
+ Hits 328 413 +85
- Misses 6 7 +1
- Partials 2 3 +1 ☔ View full report in Codecov by Sentry. |
I think you mean toml, not JSON - please update the title. |
in fact it have the three of them :D. please review |
Thanks @Smixi for the PR 🙏 A couple of things to address:
|
pydantic_settings/sources.py
Outdated
@@ -19,10 +20,48 @@ | |||
from pydantic_settings.utils import path_type_label | |||
|
|||
if TYPE_CHECKING: | |||
if sys.version_info.minor >= 11: |
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.
if sys.version_info.minor >= 11: | |
if sys.version_info >= (3, 11): |
If they are default loaded, it means the default settings class is in |
Here is the source settings priority right now:
So, if we add new sources at the end there shouldn't be a breaking change and there will be new sources that users can use by defining the file path and encoding in config. It means that releasing the new |
My concerns is more about user having their own custom orders. They will need to reimplement the |
That's a good point. didn't think about this. |
ba9fc69
to
d848544
Compare
@hramezani That should be good. I just don't know how to deal with the coverage. |
pydantic_settings/sources.py
Outdated
return {} | ||
if isinstance(files, (str, os.PathLike)): | ||
files = [files] | ||
vars: dict[str, Any | None] = {} |
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.
vars: dict[str, Any | None] = {} | |
vars: dict[str, Any] = {} |
pydantic_settings/sources.py
Outdated
return vars | ||
|
||
@abstractmethod | ||
def _read_file(self, path: Path) -> dict[str, Any | None]: |
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.
def _read_file(self, path: Path) -> dict[str, Any | None]: | |
def _read_file(self, path: Path) -> dict[str, Any]: |
pydantic_settings/sources.py
Outdated
@@ -664,6 +704,103 @@ def __repr__(self) -> str: | |||
) | |||
|
|||
|
|||
class ConfigFileSourceMixin(ABC): | |||
def _read_files(self, files: PathType | None) -> dict[str, Any | None]: |
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.
def _read_files(self, files: PathType | None) -> dict[str, Any | None]: | |
def _read_files(self, files: PathType | None) -> dict[str, Any]: |
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.
As in env settings, I think this is allowed in config files to have null values, as it's defined in json and in yaml specs ?
Or is this because None is included in any ?
Changed
pydantic_settings/sources.py
Outdated
|
||
def _read_file(self, file_path: Path) -> dict[str, Any | None]: | ||
with open(file_path, encoding=self.yaml_file_encoding) as yaml_file: | ||
import_yaml() |
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.
Isn't it better to move import_yaml
out of the block to check the import before opening the file?
I think it can be tested like the old dotenv test in Pydantic V1 and then you can add a new step to the CI to uninstall deps and run the tests to run the above tests |
LGTM. Thanks @Smixi. @samuelcolvin I will keep this PR open. please take a look if you want. |
Hi @Smixi, I have a question: |
@korolkevich This PR is only a loader for variables. |
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 LGTM.
Thank you so much.
docs/index.md
Outdated
foobar = "Hello" | ||
[nested] | ||
nested_field = "world!" | ||
""" |
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.
I don't understand what this is?
Thanks @Smixi |
feat: #210
Selected Reviewer: @samuelcolvin