本文整理汇总了Python中pydantic.BaseSettings方法的典型用法代码示例。如果您正苦于以下问题:Python pydantic.BaseSettings方法的具体用法?Python pydantic.BaseSettings怎么用?Python pydantic.BaseSettings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydantic
的用法示例。
在下文中一共展示了pydantic.BaseSettings方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_env_inheritance
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例2: test_env_inheritance_field
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例3: test_config_file_settings_nornir
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例4: test_env_file_config
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例5: test_env_file_config_case_sensitive
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'}]
示例6: test_env_file_export
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例7: test_env_file_example
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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',
}
示例8: is_pydantic
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
def is_pydantic(test_object):
try:
instance = isinstance(test_object, BaseSettings) or isinstance(test_object, BaseModel)
except TypeError:
instance = False
try:
subclass = issubclass(test_object, BaseSettings) or issubclass(test_object, BaseModel)
except TypeError:
subclass = False
return instance or subclass
示例9: test_with_prefix
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例10: test_nested_env_with_basemodel
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'}
示例11: test_nested_env_with_dict
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'}
示例12: test_required_sub_model
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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
示例13: test_non_class
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
def test_non_class(env):
class Settings(BaseSettings):
foobar: NoneStr
env.set('FOOBAR', 'xxx')
s = Settings()
assert s.foobar == 'xxx'
示例14: test_env_list
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'
示例15: test_env_list_field
# 需要导入模块: import pydantic [as 别名]
# 或者: from pydantic import BaseSettings [as 别名]
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'