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

[PydanticV2] Add parameter to use Python Regex Engine in order to support look-around #2232

Open
ilovelinux opened this issue Dec 17, 2024 · 0 comments

Comments

@ilovelinux
Copy link

ilovelinux commented Dec 17, 2024

Is your feature request related to a problem? Please describe.

  1. I wrote a valid JSON Schema with many properties
  2. Some properties' pattern make use of look-ahead and look-behind, which are supported by JSON Schema specifications.
    See: JSON Schema supported patterns
  3. datamodel-code-generator generated the PydanticV2 models.
  4. PydanticV2 doesn't support look-around, look-ahead and look-behind by default (see regex cannot work from v1 in v2 in fields pydantic/pydantic#7058)
ImportError while loading [...]: in <module>
[...]
.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py:205: in __new__
    complete_model_class(
.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py:552: in complete_model_class
    cls.__pydantic_validator__ = create_schema_validator(
.venv/lib/python3.12/site-packages/pydantic/plugin/_schema_validator.py:50: in create_schema_validator
    return SchemaValidator(schema, config)
E   pydantic_core._pydantic_core.SchemaError: Error building "model" validator:
E     SchemaError: Error building "model-fields" validator:
E     SchemaError: Field "version":
E     SchemaError: Error building "str" validator:
E     SchemaError: regex parse error:
E       ^((?!0[0-9])[0-9]+(\.(?!$)|)){2,4}$
E         ^^^
E   error: look-around, including look-ahead and look-behind, is not supported

Describe the solution you'd like
PydanticV2 supports look-around, look-ahead and look-behind using Python as regex engine: pydantic/pydantic#7058 (comment)

I'd like to have a configuration parameter to use python-re as regex engine for Pydantic V2.

Describe alternatives you've considered
Workaround:

  1. Create a custom BaseModel
from pydantic import BaseModel, ConfigDict


class _BaseModel(BaseModel):
    model_config = ConfigDict(regex_engine='python-re')
  1. Use that class as BaseModel:
datamodel-codegen --base-model "module.with.basemodel._BaseModel"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant