Skip to content

Commit 0226fe2

Browse files
authored
Remove old code relatd to optional dotenv (#155)
1 parent 919a20b commit 0226fe2

File tree

3 files changed

+4
-42
lines changed

3 files changed

+4
-42
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,11 @@ jobs:
5050

5151
- run: pip install -r requirements/testing.txt -r requirements/pyproject.txt
5252

53-
- name: test with dotenv
53+
- name: test
5454
run: make test
5555
env:
56-
COVERAGE_FILE: .coverage.${{ runner.os }}-py${{ matrix.python }}-with-dotenv
57-
CONTEXT: ${{ runner.os }}-py${{ matrix.python }}-with-dotenv
58-
59-
- run: pip uninstall -y python-dotenv
60-
61-
- name: test without dotenv
62-
run: make test
63-
env:
64-
COVERAGE_FILE: .coverage.${{ runner.os }}-py${{ matrix.python }}-without-dotenv
65-
CONTEXT: ${{ runner.os }}-py${{ matrix.python }}-without-dotenv
56+
COVERAGE_FILE: .coverage.${{ runner.os }}-py${{ matrix.python }}
57+
CONTEXT: ${{ runner.os }}-py${{ matrix.python }}
6658

6759
- run: coverage combine
6860
- run: coverage xml

pydantic_settings/sources.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pathlib import Path
1010
from typing import TYPE_CHECKING, Any, List, Mapping, Sequence, Tuple, Union, cast
1111

12+
from dotenv import dotenv_values
1213
from pydantic import AliasChoices, AliasPath, BaseModel, Json
1314
from pydantic._internal._typing_extra import origin_is_union
1415
from pydantic._internal._utils import deep_update, lenient_issubclass
@@ -620,11 +621,6 @@ def __repr__(self) -> str:
620621
def read_env_file(
621622
file_path: Path, *, encoding: str | None = None, case_sensitive: bool = False
622623
) -> Mapping[str, str | None]:
623-
try:
624-
from dotenv import dotenv_values
625-
except ImportError as e:
626-
raise ImportError('python-dotenv is not installed, run `pip install pydantic[dotenv]`') from e
627-
628624
file_vars: dict[str, str | None] = dotenv_values(file_path, encoding=encoding or 'utf8')
629625
if not case_sensitive:
630626
return {k.lower(): v for k, v in file_vars.items()}

tests/test_settings.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,6 @@ class Settings(BaseSettings):
660660
"""
661661

662662

663-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
664663
def test_env_file_config(env, tmp_path):
665664
p = tmp_path / '.env'
666665
p.write_text(test_env_file)
@@ -691,7 +690,6 @@ class Settings(BaseSettings):
691690
"""
692691

693692

694-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
695693
def test_env_file_with_env_prefix(env, tmp_path):
696694
p = tmp_path / '.env'
697695
p.write_text(prefix_test_env_file)
@@ -711,7 +709,6 @@ class Settings(BaseSettings):
711709
assert s.c == 'best string'
712710

713711

714-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
715712
def test_env_file_config_case_sensitive(tmp_path):
716713
p = tmp_path / '.env'
717714
p.write_text(test_env_file)
@@ -735,7 +732,6 @@ class Settings(BaseSettings):
735732
]
736733

737734

738-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
739735
def test_env_file_export(env, tmp_path):
740736
p = tmp_path / '.env'
741737
p.write_text(
@@ -761,7 +757,6 @@ class Settings(BaseSettings):
761757
assert s.c == 'best string'
762758

763759

764-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
765760
def test_env_file_export_validation_alias(env, tmp_path):
766761
p = tmp_path / '.env'
767762
p.write_text("""export a='{"b": ["1", "2"]}'""")
@@ -775,7 +770,6 @@ class Settings(BaseSettings):
775770
assert s.a == '2'
776771

777772

778-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
779773
def test_env_file_config_custom_encoding(tmp_path):
780774
p = tmp_path / '.env'
781775
p.write_text('pika=p!±@', encoding='latin-1')
@@ -797,7 +791,6 @@ def home_tmp():
797791
home_tmp_path.unlink()
798792

799793

800-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
801794
def test_env_file_home_directory(home_tmp):
802795
home_tmp_path, tmp_filename = home_tmp
803796
home_tmp_path.write_text('pika=baz')
@@ -810,7 +803,6 @@ class Settings(BaseSettings):
810803
assert Settings().pika == 'baz'
811804

812805

813-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
814806
def test_env_file_none(tmp_path):
815807
p = tmp_path / '.env'
816808
p.write_text('a')
@@ -822,7 +814,6 @@ class Settings(BaseSettings):
822814
assert s.a == 'xxx'
823815

824816

825-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
826817
def test_env_file_override_file(tmp_path):
827818
p1 = tmp_path / '.env'
828819
p1.write_text(test_env_file)
@@ -838,7 +829,6 @@ class Settings(BaseSettings):
838829
assert s.a == 'new string'
839830

840831

841-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
842832
def test_env_file_override_none(tmp_path):
843833
p = tmp_path / '.env'
844834
p.write_text(test_env_file)
@@ -852,7 +842,6 @@ class Settings(BaseSettings):
852842
assert s.a is None
853843

854844

855-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
856845
def test_env_file_not_a_file(env):
857846
class Settings(BaseSettings):
858847
a: str = None
@@ -862,7 +851,6 @@ class Settings(BaseSettings):
862851
assert s.a == 'ignore non-file'
863852

864853

865-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
866854
def test_read_env_file_cast_sensitive(tmp_path):
867855
p = tmp_path / '.env'
868856
p.write_text('a="test"\nB=123')
@@ -871,15 +859,13 @@ def test_read_env_file_cast_sensitive(tmp_path):
871859
assert read_env_file(p, case_sensitive=True) == {'a': 'test', 'B': '123'}
872860

873861

874-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
875862
def test_read_env_file_syntax_wrong(tmp_path):
876863
p = tmp_path / '.env'
877864
p.write_text('NOT_AN_ASSIGNMENT')
878865

879866
assert read_env_file(p, case_sensitive=True) == {'NOT_AN_ASSIGNMENT': None}
880867

881868

882-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
883869
def test_env_file_example(tmp_path):
884870
p = tmp_path / '.env'
885871
p.write_text(
@@ -907,7 +893,6 @@ class Settings(BaseSettings):
907893
}
908894

909895

910-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
911896
def test_env_file_custom_encoding(tmp_path):
912897
p = tmp_path / '.env'
913898
p.write_text('pika=p!±@', encoding='latin-1')
@@ -934,7 +919,6 @@ class Settings(BaseSettings):
934919
"""
935920

936921

937-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
938922
def test_multiple_env_file(tmp_path):
939923
base_env = tmp_path / '.env'
940924
base_env.write_text(test_default_env_file)
@@ -954,7 +938,6 @@ class Settings(BaseSettings):
954938
assert s.port == 8000
955939

956940

957-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
958941
def test_model_env_file_override_model_config(tmp_path):
959942
base_env = tmp_path / '.env'
960943
base_env.write_text(test_default_env_file)
@@ -974,7 +957,6 @@ class Settings(BaseSettings):
974957
assert s.port == 8000
975958

976959

977-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
978960
def test_multiple_env_file_encoding(tmp_path):
979961
base_env = tmp_path / '.env'
980962
base_env.write_text('pika=p!±@', encoding='latin-1')
@@ -988,7 +970,6 @@ class Settings(BaseSettings):
988970
assert s.pika == 'chu!±@'
989971

990972

991-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
992973
def test_read_dotenv_vars(tmp_path):
993974
base_env = tmp_path / '.env'
994975
base_env.write_text(test_default_env_file)
@@ -1009,7 +990,6 @@ def test_read_dotenv_vars(tmp_path):
1009990
}
1010991

1011992

1012-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
1013993
def test_read_dotenv_vars_when_env_file_is_none():
1014994
assert (
1015995
DotEnvSettingsSource(BaseSettings(), env_file=None, env_file_encoding=None)._read_env_files(
@@ -1211,7 +1191,6 @@ class Settings(BaseSettings):
12111191
Settings()
12121192

12131193

1214-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
12151194
def test_secrets_dotenv_precedence(tmp_path):
12161195
s = tmp_path / 'foo'
12171196
s.write_text('foo_secret_value_str')
@@ -1564,7 +1543,6 @@ class Settings(BaseSettings):
15641543
assert s.nested.SUB_sub.SUB_sub_SuB.val4 == 'v4'
15651544

15661545

1567-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
15681546
def test_dotenv_extra_allow(tmp_path):
15691547
p = tmp_path / '.env'
15701548
p.write_text('a=b\nx=y')
@@ -1579,7 +1557,6 @@ class Settings(BaseSettings):
15791557
assert s.x == 'y'
15801558

15811559

1582-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
15831560
def test_dotenv_extra_forbid(tmp_path):
15841561
p = tmp_path / '.env'
15851562
p.write_text('a=b\nx=y')
@@ -1596,7 +1573,6 @@ class Settings(BaseSettings):
15961573
]
15971574

15981575

1599-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
16001576
def test_dotenv_extra_case_insensitive(tmp_path):
16011577
p = tmp_path / '.env'
16021578
p.write_text('a=b')
@@ -1610,7 +1586,6 @@ class Settings(BaseSettings):
16101586
assert s.A == 'b'
16111587

16121588

1613-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
16141589
def test_dotenv_extra_sub_model_case_insensitive(tmp_path):
16151590
p = tmp_path / '.env'
16161591
p.write_text('a=b\nSUB_model={"v": "v1"}')
@@ -1803,7 +1778,6 @@ class Settings(BaseSettings):
18031778
assert s.x == '123'
18041779

18051780

1806-
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
18071781
def test_dotenv_optional_json_field(tmp_path):
18081782
p = tmp_path / '.env'
18091783
p.write_text("""DATA='{"foo":"bar"}'""")

0 commit comments

Comments
 (0)