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 property always set to None, when deque item type is provided. #6581

Closed
5 of 15 tasks
maciekglowka opened this issue Jul 11, 2023 · 5 comments
Closed
5 of 15 tasks
Assignees
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@maciekglowka
Copy link

maciekglowka commented Jul 11, 2023

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

The maxlen property of the deque field seems to be set to None when the queue item type is provided.

In the example below I first create an untyped deque and upon instancing everything goes well - the maxlen is set to 15.
However when the model definitions is more specific: deque[int] or Deque[int] then the maxlen value cannot be set.

Example Code

>>> from pydantic import BaseModel
>>> from collections import deque
>>> class D(BaseModel):
...     q: deque = deque(maxlen=10)
... 
>>> d = D(q=deque(maxlen=15))
>>> d
D(q=deque([], maxlen=15))
>>> 
>>> 
>>> class DI(BaseModel):
...     q: deque[int] = deque(maxlen=10)
... 
>>> d = DI(q=deque(maxlen=15))
>>> d
DI(q=deque([]))
>>> 
>>> from typing import Deque
>>> 
>>> class DT(BaseModel):
...     q: Deque[int] = deque(maxlen=10)
... 
>>> d = DT(q=deque(maxlen=15))
>>> d
DT(q=deque([]))

Python, Pydantic & OS Version

pydantic version: 1.10.5
pydantic compiled: True
install path: /opt/venv/lib/python3.11/site-packages/pydantic
python version: 3.11.4 (main, Jun 13 2023, 15:08:32) [GCC 10.2.1 20210110]
platform: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
optional deps. installed: ['typing-extensions']

Affected Components

Selected Assignee: @davidhewitt

@maciekglowka maciekglowka added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Jul 11, 2023
@maciekglowka
Copy link
Author

Some more cases:

>>> from collections import deque
>>> from pydantic import BaseModel
>>> class D(BaseModel):
...     q: deque[int] = deque(maxlen=10)
... 
>>> d = D()
>>> d
D(q=deque([], maxlen=10))

>>> d.q = deque(maxlen=25)
>>> d
D(q=deque([], maxlen=25))

So it seems that it is validation issue on model creation only.

I do not know the codebase here but potentially:

https://github.com/pydantic/pydantic/blob/f35c780cc3cf073c400ee038b8b6dd6d7b5703d9/pydantic/v1/fields.py#L954C25-L954C25

@hramezani
Copy link
Member

Thanks @maciekglowka for this issue 🙏

As you may know, Pydantic V2 is out and we are not actively working on V1.

I've checked your first example code on V2, all of them returning D(q=deque([], maxlen=15))

Same for you second example. In V2 you can have validation on assignment to a deque field like:

from collections import deque
from typing import Deque

from typing_extensions import Annotated
from pydantic import BaseModel, ConfigDict, Field

class D(BaseModel):
    q: Annotated[Deque[int], Field(deque(maxlen=10), max_length=10)]

    model_config = ConfigDict(validate_assignment=True)

d = D()
d.q = deque([1] * 25)

Note: We still accept bug fixes on V1 but it should be an easy fix.

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

Thanks for checking.
Yeah I've just forked the repo. If I'll be able to fix this I'm gonna make a PR.

@maciekglowka
Copy link
Author

@hramezani I've done a very simple fix (PR passes tests) - I am just rewriting the single attribute. Perhaps the rewrite should be less manual? But I am not really sure what happens under the hood in Pydantic.

@hramezani
Copy link
Member

Fixed in 2aaddf6

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

No branches or pull requests

3 participants