Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
baz: int
class ChildWithNull(object):
@story
def x(I):
I.one
class ChildWithShrink(object):
@story
def x(I):
I.one
@x.contract
class Contract(BaseModel):
baz: int
class ChildAlias(object):
@story
def x(I):
I.one
@x.contract
class Contract(BaseModel):
foo: Dict[str, str]
bar: Dict[str, str]
baz: Dict[str, int]
class ParamChild(object):
def test_custom_init_subclass_params():
class DerivedModel(BaseModel):
def __init_subclass__(cls, something):
cls.something = something
# if this raises a TypeError, then there is a regression of issue 867:
# pydantic.main.MetaModel.__new__ should include **kwargs at the end of the
# method definition and pass them on to the super call at the end in order
# to allow the special method __init_subclass__ to be defined with custom
# parameters on extended BaseModel classes.
class NewModel(DerivedModel, something=2):
something = 1
assert NewModel.something == 2
@validator('something')
def check_something(cls, v):
val_calls.append(v)
return v
@root_validator
def root_check(cls, values: Dict[str, Any]) -> Dict[str, Any]:
return values
def test_config_data_paths_existing(dst):
try:
ConfigData(
src_path="./i_do_not_exist",
extra_paths=["./i_do_not_exist"],
dst_path=dst,
envops=EnvOps(),
)
except ValidationError as e:
assert len(e.errors()) == 2
for i, p in enumerate(("src_path", "extra_paths")):
err = e.errors()[i]
assert err["loc"][0] == p
assert err["msg"] == "Project template not found."
else:
raise AssertionError()
class Child(Parent):
def echo(self):
return 'child'
class Different:
def echo(self):
return 'different'
class Model(BaseModel):
v: Type[Parent] = Parent
assert Model(v=Parent).v().echo() == 'parent'
assert Model().v().echo() == 'parent'
assert Model(v=Child).v().echo() == 'child'
with pytest.raises(ValidationError) as exc_info:
Model(v=Different)
assert exc_info.value.errors() == [
{
'loc': ('v',),
'msg': 'subclass of Parent expected',
'type': 'type_error.subclass',
'ctx': {'expected_class': 'Parent'},
}
def test_constrained_bytes_too_long():
with pytest.raises(ValidationError) as exc_info:
ConBytesModel(v=b'this is too long')
assert exc_info.value.errors() == [
{
'loc': ('v',),
'msg': 'ensure this value has at most 10 characters',
'type': 'value_error.any_str.max_length',
'ctx': {'limit_value': 10},
}
def test_invalid_analysis_date_range(invalid_request_range: JSONDict) -> None:
with pytest.raises(ValidationError) as e:
invalid_request = AnalysisRequest(**invalid_request_range) # noqa: F841
assert e.value.errors() == [
{
"loc": ("endDate",),
"msg": "Period must not exceed 24 hours",
"type": "value_error",
}
def test_url_str_relative_and_custom_schemes():
class Model(BaseModel):
v: urlstr(relative=True)
# By default, ws not allowed
url = 'ws://test.test'
with pytest.raises(ValidationError) as exc_info:
Model(v=url)
assert exc_info.value.errors() == [
{
'loc': ('v',),
'msg': 'url scheme "ws" is not allowed',
'type': 'value_error.url.scheme',
'ctx': {'scheme': 'ws'},
}
]
class Model(BaseModel):
v: urlstr(relative=True, schemes={'http', 'https', 'ws'})
assert Model(v=url).v == url
class Config:
orm_mode = True
class ModelInvalid(BaseModel):
foo: str
bar: int
class Config:
orm_mode = True
foo = FooGetAttr()
model = Model.from_orm(foo)
assert model.foo == 'Foo'
assert model.bar == 1
assert model.dict(exclude_unset=True) == {'foo': 'Foo'}
with pytest.raises(ValidationError):
ModelInvalid.from_orm(foo)