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

StrictBytes does not raise ValidationError when max_length is present in Field #4380

Closed
6 of 16 tasks
arthurazs opened this issue Aug 15, 2022 · 2 comments · Fixed by #4383
Closed
6 of 16 tasks

StrictBytes does not raise ValidationError when max_length is present in Field #4380

arthurazs opened this issue Aug 15, 2022 · 2 comments · Fixed by #4383
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@arthurazs
Copy link

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

I'm using pydantic version 1.9.2.

Mind this model:

# my_model/__init__.py
from pydantic import BaseModel, StrictBytes, Field


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

Even though b is StrictBytes, I can create an object with the following line:

m = Model(a=b'arthur', b=123) which I assume is a bug.

I cannot create an object the other way around:
m = Model(a=123, b=b'art'), which I assume is the intended behaviour.


Here follows a pytest example:

# tests/test_my_model.py

from my_model import Model
import pytest
from pydantic import ValidationError


def test_normal():
    m = Model(a=b'arthur', b=b'lucas')
    assert m.a == b'arthur'
    assert m.b == b'lucas'

def test_large():
    with pytest.raises(ValidationError) as error:
        m = Model(a=b'arthur', b=b'arthur')
    assert 'b\n' in str(error.value)
    assert 'value_error.any_str.max_length' in str(error.value)

def test_a_str():
    with pytest.raises(ValidationError) as error:
        m = Model(a=123123, b=b'lucas')
    assert 'a\n' in str(error.value)
    assert 'type_error.bytes' in str(error.value)

def test_b_str():
    with pytest.raises(ValidationError) as error:
        m = Model(a=b'arthur', b=123)  # does not raise
    assert 'b\n' in str(error.value)
    assert 'type_error.bytes' in str(error.value)

And the output from pytest:

platform linux -- Python 3.10.6, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/arthurazs/Desktop/my_model
collected 4 items                                                                     

tests/test_my_model.py ...F                                                      [100%]

====================================== FAILURES =======================================
_____________________________________ test_b_str ______________________________________

    def test_b_str():
>       with pytest.raises(ValidationError) as error:
E       Failed: DID NOT RAISE <class 'pydantic.error_wrappers.ValidationError'>

tests/test_my_model.py:24: Failed
=============================== short test summary info ===============================
FAILED tests/test_my_model.py::test_b_str - Failed: DID NOT RAISE <class 'pydantic.e...
============================= 1 failed, 3 passed in 0.04s =============================

Example Code

from pydantic import BaseModel, StrictBytes, Field


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

m = Model(a=b'arthur', b=123)  # does not raise

Python, Pydantic & OS Version

pydantic version: 1.9.2
            pydantic compiled: True
                 install path: /home/arthurazs/.cache/pypoetry/virtualenvs/asd-8JLwExQw-py3.10/lib/python3.10/site-packages/pydantic
               python version: 3.10.6 (main, Aug  2 2022, 15:11:28) [GCC 9.4.0]
                     platform: Linux-5.15.0-46-generic-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']

Affected Components

@arthurazs arthurazs added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Aug 15, 2022
@arthurazs arthurazs changed the title StrictBytes does not raise TypeError when max_length is present in Field StrictBytes does not raise ValidationError when max_length is present in Field Aug 15, 2022
@arthurazs arthurazs changed the title StrictBytes does not raise ValidationError when max_length is present in Field StrictBytes does not raise ValidationError when max_length is present in Field Aug 15, 2022
@hramezani
Copy link
Member

hramezani commented Aug 15, 2022

The reason is, by setting the max_length in b (b: StrictBytes = Field(..., max_length=5)), it won't be considered as StrictBytes anymore. then the provided value 123 will be converted to bytes during validation

from pydantic import BaseModel, StrictBytes, Field


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

m = Model(a=b'arthur', b=123)  # does not raise
print(m)  # a=b'arthur' b=b'123'

I am not sure if is it an intended behavior or not. @samuelcolvin what is your opinion here?

@hramezani hramezani removed the unconfirmed Bug not yet confirmed as valid/applicable label Aug 15, 2022
@samuelcolvin
Copy link
Member

samuelcolvin commented Aug 16, 2022

It's an error, happy to review a PR if it's simple and submitted asap. Otherwise this is/will be fixed in V2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants