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
Conversation
please review |
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.
Looking good, will need to be added to docs.
@@ -436,6 +437,12 @@ def constr( | |||
return _registered(type('ConstrainedStrValue', (ConstrainedStr,), namespace)) | |||
|
|||
|
|||
class StrLimited: |
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 use StrLimited
without parameterising it.
pydantic/types.py
Outdated
@@ -436,6 +437,12 @@ def constr( | |||
return _registered(type('ConstrainedStrValue', (ConstrainedStr,), namespace)) | |||
|
|||
|
|||
class StrLimited: | |||
@classmethod | |||
def __class_getitem__(cls, max_length: int) -> Type[str]: |
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.
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?
please update. |
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
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.
Looking good, but a few other things still to do:
__get_validators__
as suggested above- add docs, as suggested above
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove this, it's not used.
|
||
|
||
def test_str_limited_too_long(): | ||
max_length = 5 |
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 can remove this and just use a hard-coded value below, it's not adding anything.
@lucastosetto any chance you can fix this? |
Sure, I can work on it this week, but I won't have time to do it for v1.10 release. |
If it doesn't make it into v1.10 much of the implementation will have to change for V2. |
Ignore that, I'm not going to have time to work on this, I think it'll have to wait for V2. |
@classmethod | ||
def __class_getitem__(cls, max_length: int) -> Type[ConstrainedStr]: | ||
return constr(max_length=max_length, strip_whitespace=True) |
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.
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.
wasn't fixed and now conflicting, I'd still be happy to accept a fix for #2104 if someone would like to submit a PR. |
Change Summary
This represents a simplified length limited string and is an alias for the following syntax:
StrLimited[max_length]
=constr(max_length=max_length, strip_whitespace=True)
Usage example:
Related issue number
fix #2104
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)