forked from pydantic/pydantic
/
test_settings.py
631 lines (447 loc) · 16.5 KB
/
test_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import os
from typing import Dict, List, Set
import pytest
from pydantic import BaseModel, BaseSettings, Field, NoneStr, ValidationError, dataclasses
from pydantic.env_settings import SettingsError, read_env_file
try:
import dotenv
except ImportError:
dotenv = None
class SimpleSettings(BaseSettings):
apple: str
def test_sub_env(env):
env.set('apple', 'hello')
s = SimpleSettings()
assert s.apple == 'hello'
def test_sub_env_override(env):
env.set('apple', 'hello')
s = SimpleSettings(apple='goodbye')
assert s.apple == 'goodbye'
def test_sub_env_missing():
with pytest.raises(ValidationError) as exc_info:
SimpleSettings()
assert exc_info.value.errors() == [{'loc': ('apple',), 'msg': 'field required', 'type': 'value_error.missing'}]
def test_other_setting():
with pytest.raises(ValidationError):
SimpleSettings(apple='a', foobar=42)
def test_with_prefix(env):
class Settings(BaseSettings):
apple: str
class Config:
env_prefix = 'foobar_'
with pytest.raises(ValidationError):
Settings()
env.set('foobar_apple', 'has_prefix')
s = Settings()
assert s.apple == 'has_prefix'
def test_nested_env_with_basemodel(env):
class TopValue(BaseModel):
apple: str
banana: str
class Settings(BaseSettings):
top: TopValue
with pytest.raises(ValidationError):
Settings()
env.set('top', '{"banana": "secret_value"}')
s = Settings(top={'apple': 'value'})
assert s.top == {'apple': 'value', 'banana': 'secret_value'}
def test_nested_env_with_dict(env):
class Settings(BaseSettings):
top: Dict[str, str]
with pytest.raises(ValidationError):
Settings()
env.set('top', '{"banana": "secret_value"}')
s = Settings(top={'apple': 'value'})
assert s.top == {'apple': 'value', 'banana': 'secret_value'}
class DateModel(BaseModel):
pips: bool = False
class ComplexSettings(BaseSettings):
apples: List[str] = []
bananas: Set[int] = set()
carrots: dict = {}
date: DateModel = DateModel()
def test_list(env):
env.set('apples', '["russet", "granny smith"]')
s = ComplexSettings()
assert s.apples == ['russet', 'granny smith']
assert s.date.pips is False
def test_set_dict_model(env):
env.set('bananas', '[1, 2, 3, 3]')
env.set('CARROTS', '{"a": null, "b": 4}')
env.set('daTE', '{"pips": true}')
s = ComplexSettings()
assert s.bananas == {1, 2, 3}
assert s.carrots == {'a': None, 'b': 4}
assert s.date.pips is True
def test_invalid_json(env):
env.set('apples', '["russet", "granny smith",]')
with pytest.raises(SettingsError, match='error parsing JSON for "apples"'):
ComplexSettings()
def test_required_sub_model(env):
class Settings(BaseSettings):
foobar: DateModel
with pytest.raises(ValidationError):
Settings()
env.set('FOOBAR', '{"pips": "TRUE"}')
s = Settings()
assert s.foobar.pips is True
def test_non_class(env):
class Settings(BaseSettings):
foobar: NoneStr
env.set('FOOBAR', 'xxx')
s = Settings()
assert s.foobar == 'xxx'
def test_env_str(env):
class Settings(BaseSettings):
apple: str = ...
class Config:
fields = {'apple': {'env': 'BOOM'}}
env.set('BOOM', 'hello')
assert Settings().apple == 'hello'
def test_env_list(env):
class Settings(BaseSettings):
foobar: str
class Config:
fields = {'foobar': {'env': ['different1', 'different2']}}
env.set('different1', 'value 1')
env.set('different2', 'value 2')
s = Settings()
assert s.foobar == 'value 1'
def test_env_list_field(env):
class Settings(BaseSettings):
foobar: str = Field(..., env='foobar_env_name')
env.set('FOOBAR_ENV_NAME', 'env value')
s = Settings()
assert s.foobar == 'env value'
def test_env_list_last(env):
class Settings(BaseSettings):
foobar: str
class Config:
fields = {'foobar': {'env': ['different2']}}
env.set('different1', 'value 1')
env.set('different2', 'value 2')
s = Settings()
assert s.foobar == 'value 2'
assert Settings(foobar='abc').foobar == 'abc'
def test_env_inheritance(env):
class SettingsParent(BaseSettings):
foobar: str = 'parent default'
class Config:
fields = {'foobar': {'env': 'different'}}
class SettingsChild(SettingsParent):
foobar: str = 'child default'
assert SettingsParent().foobar == 'parent default'
assert SettingsParent(foobar='abc').foobar == 'abc'
assert SettingsChild().foobar == 'child default'
assert SettingsChild(foobar='abc').foobar == 'abc'
env.set('different', 'env value')
assert SettingsParent().foobar == 'env value'
assert SettingsParent(foobar='abc').foobar == 'abc'
assert SettingsChild().foobar == 'env value'
assert SettingsChild(foobar='abc').foobar == 'abc'
def test_env_inheritance_field(env):
class SettingsParent(BaseSettings):
foobar: str = Field('parent default', env='foobar_env')
class SettingsChild(SettingsParent):
foobar: str = 'child default'
assert SettingsParent().foobar == 'parent default'
assert SettingsParent(foobar='abc').foobar == 'abc'
assert SettingsChild().foobar == 'child default'
assert SettingsChild(foobar='abc').foobar == 'abc'
env.set('foobar_env', 'env value')
assert SettingsParent().foobar == 'env value'
assert SettingsParent(foobar='abc').foobar == 'abc'
assert SettingsChild().foobar == 'child default'
assert SettingsChild(foobar='abc').foobar == 'abc'
def test_env_invalid(env):
with pytest.raises(TypeError, match=r'invalid field env: 123 \(int\); should be string, list or set'):
class Settings(BaseSettings):
foobar: str
class Config:
fields = {'foobar': {'env': 123}}
def test_env_field(env):
with pytest.raises(TypeError, match=r'invalid field env: 123 \(int\); should be string, list or set'):
class Settings(BaseSettings):
foobar: str = Field(..., env=123)
def test_aliases_warning(env):
with pytest.warns(FutureWarning, match='aliases are no longer used by BaseSettings'):
class Settings(BaseSettings):
foobar: str = 'default value'
class Config:
fields = {'foobar': 'foobar_alias'}
assert Settings().foobar == 'default value'
env.set('foobar_alias', 'xxx')
assert Settings().foobar == 'default value'
assert Settings(foobar_alias='42').foobar == '42'
def test_aliases_no_warning(env):
class Settings(BaseSettings):
foobar: str = 'default value'
class Config:
fields = {'foobar': {'alias': 'foobar_alias', 'env': 'foobar_env'}}
assert Settings().foobar == 'default value'
assert Settings(foobar_alias='42').foobar == '42'
env.set('foobar_alias', 'xxx')
assert Settings().foobar == 'default value'
env.set('foobar_env', 'xxx')
assert Settings().foobar == 'xxx'
assert Settings(foobar_alias='42').foobar == '42'
def test_case_sensitive(monkeypatch):
class Settings(BaseSettings):
foo: str
class Config:
case_sensitive = True
# Need to patch os.environ to get build to work on Windows, where os.environ is case insensitive
monkeypatch.setattr(os, 'environ', value={'Foo': 'foo'})
with pytest.raises(ValidationError) as exc_info:
Settings()
assert exc_info.value.errors() == [{'loc': ('foo',), 'msg': 'field required', 'type': 'value_error.missing'}]
def test_case_insensitive(monkeypatch):
class Settings1(BaseSettings):
foo: str
with pytest.warns(DeprecationWarning, match='Settings2: "case_insensitive" is deprecated on BaseSettings'):
class Settings2(BaseSettings):
foo: str
class Config:
case_insensitive = False
assert Settings1.__config__.case_sensitive is False
assert Settings2.__config__.case_sensitive is True
def test_nested_dataclass(env):
@dataclasses.dataclass
class MyDataclass:
foo: int
bar: str
class Settings(BaseSettings):
n: MyDataclass
env.set('N', '[123, "bar value"]')
s = Settings()
assert isinstance(s.n, MyDataclass)
assert s.n.foo == 123
assert s.n.bar == 'bar value'
def test_env_takes_precedence(env):
class Settings(BaseSettings):
foo: int
bar: str
def _build_values(self, init_kwargs, _env_file, _env_file_encoding):
return {**init_kwargs, **self._build_environ()}
env.set('BAR', 'env setting')
s = Settings(foo='123', bar='argument')
assert s.foo == 123
assert s.bar == 'env setting'
def test_config_file_settings_nornir(env):
"""
See https://github.com/samuelcolvin/pydantic/pull/341#issuecomment-450378771
"""
class Settings(BaseSettings):
a: str
b: str
c: str
def _build_values(self, init_kwargs, _env_file, _env_file_encoding):
config_settings = init_kwargs.pop('__config_settings__')
return {**config_settings, **init_kwargs, **self._build_environ()}
env.set('C', 'env setting c')
config = {'a': 'config a', 'b': 'config b', 'c': 'config c'}
s = Settings(__config_settings__=config, b='argument b', c='argument c')
assert s.a == 'config a'
assert s.b == 'argument b'
assert s.c == 'env setting c'
test_env_file = """\
# this is a comment
A=good string
# another one, followed by whitespace
b='better string'
c="best string"
"""
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_config(env, tmp_path):
p = tmp_path / '.env'
p.write_text(test_env_file)
class Settings(BaseSettings):
a: str
b: str
c: str
class Config:
env_file = p
env.set('A', 'overridden var')
s = Settings()
assert s.a == 'overridden var'
assert s.b == 'better string'
assert s.c == 'best string'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_config_case_sensitive(tmp_path):
p = tmp_path / '.env'
p.write_text(test_env_file)
class Settings(BaseSettings):
a: str
b: str
c: str
class Config:
env_file = p
case_sensitive = True
with pytest.raises(ValidationError) as exc_info:
Settings()
assert exc_info.value.errors() == [{'loc': ('a',), 'msg': 'field required', 'type': 'value_error.missing'}]
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_export(env, tmp_path):
p = tmp_path / '.env'
p.write_text(
"""\
export A='good string'
export B=better-string
export C="best string"
"""
)
class Settings(BaseSettings):
a: str
b: str
c: str
class Config:
env_file = p
env.set('A', 'overridden var')
s = Settings()
assert s.a == 'overridden var'
assert s.b == 'better-string'
assert s.c == 'best string'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_config_custom_encoding(tmp_path):
p = tmp_path / '.env'
p.write_text('pika=p!±@', encoding='latin-1')
class Settings(BaseSettings):
pika: str
class Config:
env_file = p
env_file_encoding = 'latin-1'
s = Settings()
assert s.pika == 'p!±@'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_none(tmp_path):
p = tmp_path / '.env'
p.write_text('a')
class Settings(BaseSettings):
a: str = 'xxx'
s = Settings(_env_file=p)
assert s.a == 'xxx'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_override_file(tmp_path):
p1 = tmp_path / '.env'
p1.write_text(test_env_file)
p2 = tmp_path / '.env.prod'
p2.write_text('A="new string"')
class Settings(BaseSettings):
a: str
class Config:
env_file = str(p1)
s = Settings(_env_file=p2)
assert s.a == 'new string'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_override_none(tmp_path):
p = tmp_path / '.env'
p.write_text(test_env_file)
class Settings(BaseSettings):
a: str = None
class Config:
env_file = p
s = Settings(_env_file=None)
assert s.a is None
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_not_a_file(env):
class Settings(BaseSettings):
a: str = None
env.set('A', 'ignore non-file')
s = Settings(_env_file='tests/')
assert s.a == 'ignore non-file'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_read_env_file_cast_sensitive(tmp_path):
p = tmp_path / '.env'
p.write_text('a="test"\nB=123')
assert read_env_file(p) == {'a': 'test', 'b': '123'}
assert read_env_file(p, case_sensitive=True) == {'a': 'test', 'B': '123'}
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_read_env_file_syntax_wrong(tmp_path):
p = tmp_path / '.env'
p.write_text('NOT_AN_ASSIGNMENT')
assert read_env_file(p, case_sensitive=True) == {'NOT_AN_ASSIGNMENT': None}
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_example(tmp_path):
p = tmp_path / '.env'
p.write_text(
"""\
# ignore comment
ENVIRONMENT="production"
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42
MY_VAR='Hello world'
"""
)
class Settings(BaseSettings):
environment: str
redis_address: str
meaning_of_life: int
my_var: str
s = Settings(_env_file=str(p))
assert s.dict() == {
'environment': 'production',
'redis_address': 'localhost:6379',
'meaning_of_life': 42,
'my_var': 'Hello world',
}
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_custom_encoding(tmp_path):
p = tmp_path / '.env'
p.write_text('pika=p!±@', encoding='latin-1')
class Settings(BaseSettings):
pika: str
with pytest.raises(UnicodeDecodeError):
Settings(_env_file=str(p))
s = Settings(_env_file=str(p), _env_file_encoding='latin-1')
assert s.dict() == {'pika': 'p!±@'}
@pytest.mark.skipif(dotenv, reason='python-dotenv is installed')
def test_dotenv_not_installed(tmp_path):
p = tmp_path / '.env'
p.write_text('a=b')
class Settings(BaseSettings):
a: str
with pytest.raises(ImportError, match=r'^python-dotenv is not installed, run `pip install pydantic\[dotenv\]`$'):
Settings(_env_file=p)
def test_alias_set(env):
class Settings(BaseSettings):
foo: str = 'default foo'
bar: str = 'bar default'
class Config:
fields = {'foo': {'env': 'foo_env'}}
assert Settings.__fields__['bar'].name == 'bar'
assert Settings.__fields__['bar'].alias == 'bar'
assert Settings.__fields__['foo'].name == 'foo'
assert Settings.__fields__['foo'].alias == 'foo'
class SubSettings(Settings):
spam: str = 'spam default'
assert SubSettings.__fields__['bar'].name == 'bar'
assert SubSettings.__fields__['bar'].alias == 'bar'
assert SubSettings.__fields__['foo'].name == 'foo'
assert SubSettings.__fields__['foo'].alias == 'foo'
assert SubSettings().dict() == {'foo': 'default foo', 'bar': 'bar default', 'spam': 'spam default'}
env.set('foo_env', 'fff')
assert SubSettings().dict() == {'foo': 'fff', 'bar': 'bar default', 'spam': 'spam default'}
env.set('bar', 'bbb')
assert SubSettings().dict() == {'foo': 'fff', 'bar': 'bbb', 'spam': 'spam default'}
env.set('spam', 'sss')
assert SubSettings().dict() == {'foo': 'fff', 'bar': 'bbb', 'spam': 'sss'}
def test_prefix_on_parent(env):
class MyBaseSettings(BaseSettings):
var: str = 'old'
class MySubSettings(MyBaseSettings):
class Config:
env_prefix = 'PREFIX_'
assert MyBaseSettings().dict() == {'var': 'old'}
assert MySubSettings().dict() == {'var': 'old'}
env.set('PREFIX_VAR', 'new')
assert MyBaseSettings().dict() == {'var': 'old'}
assert MySubSettings().dict() == {'var': 'new'}
def test_frozenset(env):
class Settings(BaseSettings):
foo: str = 'default foo'
class Config:
fields = {'foo': {'env': frozenset(['foo_a', 'foo_b'])}}
assert Settings.__fields__['foo'].field_info.extra['env_names'] == frozenset({'foo_a', 'foo_b'})
assert Settings().dict() == {'foo': 'default foo'}
env.set('foo_a', 'x')
assert Settings().dict() == {'foo': 'x'}