Skip to content

Pattern type serialisation error #2045

Closed
@adrienyhuel

Description

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

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

             pydantic version: 1.7
            pydantic compiled: True
                 install path: /root/.local/lib/python3.8/site-packages/pydantic
               python version: 3.8.6 (default, Oct 13 2020, 20:49:19)  [GCC 8.3.0]
                     platform: Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-glibc2.2.5
     optional deps. installed: ['email-validator']
from typing import Pattern
from pydantic import BaseModel

class Model(BaseModel):
    filter: Pattern

Model(filter="^regex$").json()

This produce an error :
TypeError: Object of type 'Pattern' is not JSON serializable

I had to write a custom encoder to be able to serialize properly a Pattern type :

from typing import Pattern
from pydantic import BaseModel
import re

class Model(BaseModel):
    filter: Pattern
    class Config:
        json_encoders = {
            re.Pattern: lambda v: v.pattern,
        }

Model(filter="^regex$").json()

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