File: re.py

package info (click to toggle)
python-beanie 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,484 kB
  • sloc: python: 14,427; makefile: 6; sh: 6
file content (26 lines) | stat: -rw-r--r-- 567 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import re

import bson
import pydantic
from typing_extensions import Annotated

from beanie.odm.utils.pydantic import IS_PYDANTIC_V2


def _to_bson_regex(v):
    return v.try_compile() if isinstance(v, bson.Regex) else v


if IS_PYDANTIC_V2:
    Pattern = Annotated[
        re.Pattern,
        pydantic.BeforeValidator(
            lambda v: v.try_compile() if isinstance(v, bson.Regex) else v
        ),
    ]
else:

    class Pattern(bson.Regex):  # type: ignore[no-redef]
        @classmethod
        def __get_validators__(cls):
            yield _to_bson_regex