Skip to content
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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2104-lucastosetto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added simplified length limited string type, declared as StrLimited[max_length].
lucastosetto marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions pydantic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'NoneStr',
'NoneBytes',
'StrBytes',
'StrLimited',
'NoneStrBytes',
'StrictStr',
'ConstrainedBytes',
Expand Down
7 changes: 7 additions & 0 deletions pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
'StrictBytes',
'StrictInt',
'StrictFloat',
'StrLimited',
'PaymentCardNumber',
'ByteSize',
'PastDate',
Expand Down Expand Up @@ -436,6 +437,12 @@ def constr(
return _registered(type('ConstrainedStrValue', (ConstrainedStr,), namespace))


class StrLimited:
Copy link
Member

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 use StrLimited without parameterising it.

@classmethod
def __class_getitem__(cls, max_length: int) -> Type[str]:
lucastosetto marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also be clever and allow slice usage:

class MyModel(BaseModel):
    has_max_length: StrLimited[10]
    has_min_and_max_length: StrLimited[5:10]

WDYT?

return constr(max_length=max_length, strip_whitespace=True)


if TYPE_CHECKING:
StrictStr = str
else:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
StrictFloat,
StrictInt,
StrictStr,
StrLimited,
ValidationError,
conbytes,
condecimal,
Expand Down Expand Up @@ -733,6 +734,34 @@ class Model(BaseModel):
]


def test_str_limited_good():
max_length = 5
Copy link
Member

Choose a reason for hiding this comment

The 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[max_length]
lucastosetto marked this conversation as resolved.
Show resolved Hide resolved

m = Model(v='abcde')
lucastosetto marked this conversation as resolved.
Show resolved Hide resolved
assert m.v == 'abcde'


def test_str_limited_too_long():
max_length = 5
Copy link
Member

Choose a reason for hiding this comment

The 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[max_length]
lucastosetto marked this conversation as resolved.
Show resolved Hide resolved

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'
Expand Down