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

MaxLen annotation validator for custom sequences is incorrect #6808

Closed
1 task done
ImogenBits opened this issue Jul 22, 2023 · 2 comments · Fixed by #6809
Closed
1 task done

MaxLen annotation validator for custom sequences is incorrect #6808

ImogenBits opened this issue Jul 22, 2023 · 2 comments · Fixed by #6809
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@ImogenBits
Copy link
Contributor

ImogenBits commented Jul 22, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using an annotated_type.MaxLen annotation with a custom sequence it rejects sequences that have precisely the maximum length when it should only reject if they are greater.

I'm pretty sure this is because the validator function used for custom sequences currently checks for strict inequality rather than less or equal.

Example Code

from typing import Annotated
from annotated_types import MaxLen

from pydantic import BaseModel

class MySequence(BaseModel):

    inner: list[int]

    def __len__(self):
        return len(self.inner)

    def __getitem__(self, __key: int) -> int:
        return self.inner[__key]


class MyModel(BaseModel):
    seq: Annotated[MySequence, MaxLen(3)]


MyModel.model_validate({"seq": {"inner": [1, 2, 3]}})

Python, Pydantic & OS Version

pydantic version: 2.0.3
pydantic-core version: 2.3.0 release build profile
python version: 3.11.4 | packaged by Anaconda, Inc. | (main, Jul  5 2023, 13:47:18) [MSC v.1916 64 bit (AMD64)]
platform: Windows-10-10.0.19045-SP0
optional deps. installed: ['typing-extensions'];

Selected Assignee: @davidhewitt

@ImogenBits ImogenBits added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Jul 22, 2023
@hramezani
Copy link
Member

Thanks @ImogenBits for reporting this 🙏

Would you like to open a PR? Otherwise I will fix it.

@hramezani hramezani removed the unconfirmed Bug not yet confirmed as valid/applicable label Jul 22, 2023
@ImogenBits
Copy link
Contributor Author

I made a PR

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

Successfully merging a pull request may close this issue.

3 participants