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 all 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]`.
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[ConstrainedStr]:
return constr(max_length=max_length, strip_whitespace=True)
Comment on lines +441 to +443
Copy link
Contributor

Choose a reason for hiding this comment

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

This usage of __class_getitem__ isn't compatible with static typing, at least mypy will give an error for StrLimited[10], and I believe the other major type checkers would too. Mypy gives: error: The type "Type[StrLimited]" is not generic and not indexable [misc]. I'd suggest redesigning this to make StrLimited an object that implements __get_item__ instead.



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[5]

m = Model(v='abcde ')
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[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'
Expand Down