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

Fix StrictStr does not raise ValidationError when max_length is present in Field #4388

merged 1 commit into from Aug 19, 2022


Copy link

@hramezani hramezani commented Aug 17, 2022

ref #4383.

Change Summary

same as the fix for StrictBytes.

Without this patch, the following code fails:

from pydantic import BaseModel, Field, StrictStr

class Model(BaseModel):
    a: StrictStr = Field(...)
    b: StrictStr = Field(..., max_length=5)

print(Model(a="123", b="123"))
Traceback (most recent call last):
  File "/tmp/", line 3, in <module>
    class Model(BaseModel):
  File "/home/hasan/work/pydantic/pydantic/", line 198, in __new__
    fields[ann_name] = ModelField.infer(
  File "/home/hasan/work/pydantic/pydantic/", line 497, in infer
    annotation = get_annotation_from_field_info(annotation, field_info, name, config.validate_assignment)
  File "/home/hasan/work/pydantic/pydantic/", line 1011, in get_annotation_from_field_info
    raise ValueError(
ValueError: On field "b" the following field constraints are set but not enforced: max_length. 
For more details see

Related issue number


  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/ for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Copy link
Member Author

please review

Copy link

great, thanks so much.

@hramezani hramezani deleted the strict_str branch August 19, 2022 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants