-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New type StrLimited #3724
New type StrLimited #3724
Changes from all commits
da46289
6633550
8349c7a
a174228
88f2e81
133a378
422f9f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added simplified length limited string type, declared as `StrLimited[max_length]`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ | |
'NoneStr', | ||
'NoneBytes', | ||
'StrBytes', | ||
'StrLimited', | ||
'NoneStrBytes', | ||
'StrictStr', | ||
'ConstrainedBytes', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ | |
'StrictBytes', | ||
'StrictInt', | ||
'StrictFloat', | ||
'StrLimited', | ||
'PaymentCardNumber', | ||
'ByteSize', | ||
'PastDate', | ||
|
@@ -436,6 +437,12 @@ def constr( | |
return _registered(type('ConstrainedStrValue', (ConstrainedStr,), namespace)) | ||
|
||
|
||
class StrLimited: | ||
@classmethod | ||
def __class_getitem__(cls, max_length: int) -> Type[ConstrainedStr]: | ||
return constr(max_length=max_length, strip_whitespace=True) | ||
Comment on lines
+441
to
+443
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This usage of |
||
|
||
|
||
if TYPE_CHECKING: | ||
StrictStr = str | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
StrictFloat, | ||
StrictInt, | ||
StrictStr, | ||
StrLimited, | ||
ValidationError, | ||
conbytes, | ||
condecimal, | ||
|
@@ -733,6 +734,34 @@ class Model(BaseModel): | |
] | ||
|
||
|
||
def test_str_limited_good(): | ||
max_length = 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove this, it's not used. |
||
|
||
class Model(BaseModel): | ||
v: StrLimited[5] | ||
|
||
m = Model(v='abcde ') | ||
assert m.v == 'abcde' | ||
|
||
|
||
def test_str_limited_too_long(): | ||
max_length = 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove this and just use a hard-coded value below, it's not adding anything. |
||
|
||
class Model(BaseModel): | ||
v: StrLimited[5] | ||
|
||
with pytest.raises(ValidationError) as exc_info: | ||
Model(v='abcdef') | ||
assert exc_info.value.errors() == [ | ||
{ | ||
'loc': ('v',), | ||
'msg': f'ensure this value has at most {max_length} characters', | ||
'type': 'value_error.any_str.max_length', | ||
'ctx': {'limit_value': max_length}, | ||
} | ||
] | ||
|
||
|
||
def test_module_import(): | ||
class PyObjectModel(BaseModel): | ||
module: PyObject = 'os.path' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might want to implement
__get_validators__
and raise an error people try to useStrLimited
without parameterising it.