Skip to content

Pydantic does not accept pre-compiled Pattern objects #1237

@adamgreg

Description

@adamgreg

Bug

Pydantic does not accept pre-compiled regular expression Pattern objects as valid values for Pattern-typed fields. Linked to issue #1211 . I'll create a pull request.

By the way, I've just discovered Pydantic and it blows my mind. Incredible work, thank you!

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.4
            pydantic compiled: False
                 install path: C:\Users\agregory\.virtualenvs\Diasemi\Lib\site-packages\pydantic
               python version: 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]
                     platform: Windows-10-10.0.17134-SP0
     optional deps. installed: []

import pydantic
import re
import typing

class Amodel(pydantic.BaseModel):
    name: typing.Pattern

    @pydantic.validator('name', pre=True)
    def compile_insensitive(cls, value: str) -> typing.Pattern:
        return re.compile(value, re.IGNORECASE)

x = Amodel(name='Something')

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