Skip to content

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

@arthurazs

Description

@arthurazs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.X

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions