Skip to content

Commit 548532b

Browse files
authored
Replace tomlkit with tomli and remove toml_file_encoding config (#236)
1 parent 3652fbd commit 548532b

File tree

9 files changed

+30
-62
lines changed

9 files changed

+30
-62
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
CONTEXT: ${{ runner.os }}-py${{ matrix.python }}
5858

5959
- name: uninstall deps
60-
run: pip uninstall -y tomlkit PyYAML
60+
run: pip uninstall -y PyYAML
6161

6262
- name: test without deps
6363
run: make test

docs/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ docker service create --name pydantic-with-secrets --secret my_secret_data pydan
539539

540540
Other settings sources are available for common configuration files:
541541

542-
- `TomlConfigSettingsSource` using `toml_file` and toml_file_encoding arguments
542+
- `TomlConfigSettingsSource` using `toml_file` argument
543543
- `YamlConfigSettingsSource` using `yaml_file` and yaml_file_encoding arguments
544544
- `JsonConfigSettingsSource` using `json_file` and `json_file_encoding` arguments
545545

@@ -570,9 +570,7 @@ class Nested(BaseModel):
570570
class Settings(BaseSettings):
571571
foobar: str
572572
nested: Nested
573-
model_config = SettingsConfigDict(
574-
toml_file='config.toml', toml_file_encoding='utf-8'
575-
)
573+
model_config = SettingsConfigDict(toml_file='config.toml')
576574

577575
@classmethod
578576
def settings_customise_sources(

pydantic_settings/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class SettingsConfigDict(ConfigDict, total=False):
3434
yaml_file: PathType | None
3535
yaml_file_encoding: str | None
3636
toml_file: PathType | None
37-
toml_file_encoding: str | None
3837

3938

4039
# Extend `config_keys` by pydantic settings config keys to
@@ -207,7 +206,6 @@ def _settings_build_values(
207206
yaml_file=None,
208207
yaml_file_encoding=None,
209208
toml_file=None,
210-
toml_file_encoding=None,
211209
secrets_dir=None,
212210
protected_namespaces=('model_', 'settings_'),
213211
)

pydantic_settings/sources.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import tomllib
2525
else:
2626
tomllib = None
27-
import tomlkit
27+
import tomli
2828
import yaml
2929

3030
from pydantic_settings.main import BaseSettings
3131
else:
3232
yaml = None
3333
tomllib = None
34-
tomlkit = None
34+
tomli = None
3535

3636

3737
def import_yaml() -> None:
@@ -45,15 +45,15 @@ def import_yaml() -> None:
4545

4646

4747
def import_toml() -> None:
48-
global tomlkit
48+
global tomli
4949
global tomllib
5050
if sys.version_info < (3, 11):
51-
if tomlkit is not None:
51+
if tomli is not None:
5252
return
5353
try:
54-
import tomlkit
54+
import tomli
5555
except ImportError as e:
56-
raise ImportError('tomlkit is not installed, run `pip install pydantic-settings[toml]`') from e
56+
raise ImportError('tomli is not installed, run `pip install pydantic-settings[toml]`') from e
5757
else:
5858
if tomllib is not None:
5959
return
@@ -764,22 +764,16 @@ def __init__(
764764
self,
765765
settings_cls: type[BaseSettings],
766766
toml_file: PathType | None = DEFAULT_PATH,
767-
toml_file_encoding: str | None = None,
768767
):
769768
self.toml_file_path = toml_file if toml_file != DEFAULT_PATH else settings_cls.model_config.get('toml_file')
770-
self.toml_file_encoding = (
771-
toml_file_encoding
772-
if toml_file_encoding is not None
773-
else settings_cls.model_config.get('toml_file_encoding')
774-
)
775769
self.toml_data = self._read_files(self.toml_file_path)
776770
super().__init__(settings_cls, self.toml_data)
777771

778772
def _read_file(self, file_path: Path) -> dict[str, Any]:
779773
import_toml()
780-
with open(file_path, mode='rb', encoding=self.toml_file_encoding) as toml_file:
774+
with open(file_path, mode='rb') as toml_file:
781775
if sys.version_info < (3, 11):
782-
return tomlkit.load(toml_file)
776+
return tomli.load(toml_file)
783777
return tomllib.load(toml_file)
784778

785779

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dynamic = ['version']
4747

4848
[project.optional-dependencies]
4949
yaml = ["pyyaml>=6.0.1"]
50-
toml = ["tomlkit>=0.12"]
50+
toml = ["tomli>=2.0.1"]
5151

5252
[project.urls]
5353
Homepage = 'https://github.com/pydantic/pydantic-settings'

requirements/linting.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.11
2+
# This file is autogenerated by pip-compile with Python 3.8
33
# by the following command:
44
#
55
# pip-compile --output-file=requirements/linting.txt requirements/linting.in
@@ -32,22 +32,28 @@ platformdirs==4.2.0
3232
# via
3333
# black
3434
# virtualenv
35-
pre-commit==3.6.1
35+
pre-commit==3.5.0
3636
# via -r requirements/linting.in
3737
pyupgrade==3.15.0
3838
# via -r requirements/linting.in
3939
pyyaml==6.0.1
4040
# via
4141
# -r requirements/linting.in
4242
# pre-commit
43-
ruff==0.2.1
43+
ruff==0.2.2
4444
# via -r requirements/linting.in
4545
tokenize-rt==5.2.0
4646
# via pyupgrade
47+
tomli==2.0.1
48+
# via
49+
# black
50+
# mypy
4751
types-pyyaml==6.0.12.12
4852
# via -r requirements/linting.in
4953
typing-extensions==4.9.0
50-
# via mypy
54+
# via
55+
# black
56+
# mypy
5157
virtualenv==20.25.0
5258
# via pre-commit
5359

requirements/pyproject.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ python-dotenv==1.0.1
1414
# via pydantic-settings (pyproject.toml)
1515
pyyaml==6.0.1
1616
# via pydantic-settings (pyproject.toml)
17-
tomlkit==0.12.3
17+
tomli==2.0.1
1818
# via pydantic-settings (pyproject.toml)
1919
typing-extensions==4.9.0
2020
# via

requirements/testing.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pluggy==1.4.0
3232
# via pytest
3333
pygments==2.17.2
3434
# via rich
35-
pytest==8.0.0
35+
pytest==8.0.1
3636
# via
3737
# -r requirements/testing.in
3838
# pytest-examples
@@ -46,7 +46,7 @@ pytest-pretty==1.2.0
4646
# via -r requirements/testing.in
4747
rich==13.7.0
4848
# via pytest-pretty
49-
ruff==0.2.1
49+
ruff==0.2.2
5050
# via pytest-examples
5151
tomli==2.0.1
5252
# via

tests/test_settings.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
except ImportError:
5050
yaml = None
5151
try:
52-
import tomlkit
52+
import tomli
5353
except ImportError:
54-
tomlkit = None
54+
tomli = None
5555

5656

5757
class SimpleSettings(BaseSettings):
@@ -1187,34 +1187,6 @@ def settings_customise_sources(
11871187
Settings()
11881188

11891189

1190-
@pytest.mark.skipif(sys.version_info >= (3, 11) or tomlkit, reason='tomlkit/tomllib is installed')
1191-
def test_toml_not_installed(tmp_path):
1192-
p = tmp_path / '.env'
1193-
p.write_text(
1194-
"""
1195-
foobar = "Hello"
1196-
"""
1197-
)
1198-
1199-
class Settings(BaseSettings):
1200-
foobar: str
1201-
model_config = SettingsConfigDict(toml_file=p)
1202-
1203-
@classmethod
1204-
def settings_customise_sources(
1205-
cls,
1206-
settings_cls: Type[BaseSettings],
1207-
init_settings: PydanticBaseSettingsSource,
1208-
env_settings: PydanticBaseSettingsSource,
1209-
dotenv_settings: PydanticBaseSettingsSource,
1210-
file_secret_settings: PydanticBaseSettingsSource,
1211-
) -> Tuple[PydanticBaseSettingsSource, ...]:
1212-
return (TomlConfigSettingsSource(settings_cls),)
1213-
1214-
with pytest.raises(ImportError, match=r'^tomlkit is not installed, run `pip install pydantic-settings\[toml\]`$'):
1215-
Settings()
1216-
1217-
12181190
def test_alias_set(env):
12191191
class Settings(BaseSettings):
12201192
foo: str = Field('default foo', validation_alias='foo_env')
@@ -2159,7 +2131,7 @@ def settings_customise_sources(
21592131
assert s.model_dump() == {}
21602132

21612133

2162-
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomlkit is None, reason='tomlkit/tomllib is not installed')
2134+
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
21632135
def test_toml_file(tmp_path):
21642136
p = tmp_path / '.env'
21652137
p.write_text(
@@ -2195,7 +2167,7 @@ def settings_customise_sources(
21952167
assert s.nested.nested_field == 'world!'
21962168

21972169

2198-
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomlkit is None, reason='tomlkit/tomllib is not installed')
2170+
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
21992171
def test_toml_no_file():
22002172
class Settings(BaseSettings):
22012173
model_config = SettingsConfigDict(toml_file=None)
@@ -2215,7 +2187,7 @@ def settings_customise_sources(
22152187
assert s.model_dump() == {}
22162188

22172189

2218-
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomlkit is None, reason='tomlkit/tomllib is not installed')
2190+
@pytest.mark.skipif(sys.version_info <= (3, 11) and tomli is None, reason='tomli/tomllib is not installed')
22192191
def test_multiple_file_toml(tmp_path):
22202192
p1 = tmp_path / '.env.toml1'
22212193
p2 = tmp_path / '.env.toml2'

0 commit comments

Comments
 (0)