Skip to content

Commit

Permalink
Expose regex_engine flag
Browse files Browse the repository at this point in the history
  • Loading branch information
utkini committed Oct 7, 2023
1 parent 32ea570 commit 80402b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pydantic/_internal/_config.py
Expand Up @@ -80,6 +80,7 @@ class ConfigWrapper:
json_schema_serialization_defaults_required: bool
json_schema_mode_override: Literal['validation', 'serialization', None]
coerce_numbers_to_str: bool
regex_engine: Literal['rust-regex', 'python-re']

def __init__(self, config: ConfigDict | dict[str, Any] | type[Any] | None, *, check: bool = True):
if check:
Expand Down Expand Up @@ -248,6 +249,7 @@ def _context_manager() -> Iterator[None]:
json_schema_serialization_defaults_required=False,
json_schema_mode_override=None,
coerce_numbers_to_str=False,
regex_engine='rust-regex',
)


Expand Down
24 changes: 24 additions & 0 deletions pydantic/config.py
Expand Up @@ -787,5 +787,29 @@ class Model(BaseModel):
```
"""

regex_engine: Literal['rust-regex', 'python-re']
"""
The regex engine to used for pattern validation
- `rust-regex` uses the [`regex`](https://docs.rs/regex) Rust crate,
which is non-backtracking and therefore more DDoS resistant, but does not support all regex features.
- `python-re` use the [`re`](https://docs.python.org/3/library/re.html) module,
which supports all regex features, but may be slower.
By default, the `rust-regex` engine will work.
```py
from pydantic import BaseModel, ConfigDict, Field
class Model(BaseModel):
model_config = ConfigDict(regex_engine='python-re')
value: str = Field(pattern=r'^abc(?=def)')
print(Model(value='abcdef').value)
#> "abc"
```
"""


__getattr__ = getattr_migration(__name__)

0 comments on commit 80402b7

Please sign in to comment.