Skip to content

Conversation

hramezani
Copy link
Member

@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/test.py", line 3, in <module>
    class Model(BaseModel):
  File "/home/hasan/work/pydantic/pydantic/main.py", line 198, in __new__
    fields[ann_name] = ModelField.infer(
  File "/home/hasan/work/pydantic/pydantic/fields.py", line 497, in infer
    annotation = get_annotation_from_field_info(annotation, field_info, name, config.validate_assignment)
  File "/home/hasan/work/pydantic/pydantic/schema.py", 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 https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints

Related issue number

Checklist

  • 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/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@hramezani
Copy link
Member Author

please review

@samuelcolvin
Copy link
Member

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants