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

Python completely freezes when uses Generator(Iterable) and itertools.repeat #9258

Open
1 task done
levsh opened this issue Apr 16, 2024 · 3 comments
Open
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@levsh
Copy link

levsh commented Apr 16, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hi!
Python freezes and not responding on Ctrl^C
How to fix this?
Thank you

Example Code

import itertools
from collections.abc import Generator, Iterable
from pydantic import BaseModel

class M(BaseModel):
    x: list[int] | Generator[int]
    # x: list[int] | Iterable[int]  # same result


m = M(x=[1])  # -> ok
m = M(x=itertools.repeat(5))  # -> freeze

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=false
                 install path: /Users/.../.pyenv/versions/3.11.5/lib/python3.11/site-packages/pydantic
               python version: 3.11.5 (main, Sep 16 2023, 09:45:16) [Clang 12.0.5 (clang-1205.0.22.11)]
                     platform: macOS-11.7.10-x86_64-i386-64bit
             related packages: typing_extensions-4.11.0 typing_extensions-4.8.0 pyright-1.1.332 pydantic-settings-2.2.1
                       commit: unknown
@levsh levsh added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 16, 2024
@levsh
Copy link
Author

levsh commented Apr 16, 2024

And Any with model_dump_json not works also

import itertools
from typing import Any
from pydantic import BaseModel

class M(BaseModel):
    x: Any

m = M(x=[1])
m.model_dump()  # ok
m.model_dump_json()  # ok

m = M(x=itertools.repeat(5))
m.model_dump()  # ok
m.model_dump_json()  # -> freeze

@Viicos
Copy link
Contributor

Viicos commented Apr 16, 2024

repeat(5) is an infinite iterator, what behavior are you expecting when doing m = M(x=itertools.repeat(5))? Pydantic could implement an iteration limit to avoid infinite loops but this is probably the responsibility of the developer.

@levsh
Copy link
Author

levsh commented Apr 17, 2024

@Viicos i try to define field that accept both list[int] and itertools.repeat and .model_dump returns original value without modification or unwraping infinity itertools.repeat
This example works(not freezing)

class M(BaseModel):
    x: Iterable[int] | list[int]

but for M(x=[5]).model_dump() returns SerializationIterator

{'x': SerializationIterator(index=0, iterator=ValidatorIterator(index=0, schema=Some(Int(IntValidator { strict: false }))))}

instead of [5]

Alternative version works for list[int] but freezing for itertools.repeat

class M(BaseModel):
    x: list[int] | Iterable[int]

And it would be perfect to get exception from Pydantic instead of freeze (if it is possible)

Thank you.

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 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants