diff options
| author | Bobby <[email protected]> | 2026-07-01 17:50:29 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-01 17:50:29 +0530 |
| commit | c757ea1fea447df3ed69e74b407ca9710a9f85e0 (patch) | |
| tree | a710f73b1a8b7b0117f41a18c9accd01127b033e /tests/pattern/classes.test.py | |
| parent | ad4eb75ddcadf263391bba8eed837cce3b860cf9 (diff) | |
| parent | e0d5e70a0d1a35a1e9311aa8200fb5c5f2ca0854 (diff) | |
| download | edify-c757ea1fea447df3ed69e74b407ca9710a9f85e0.tar.xz edify-c757ea1fea447df3ed69e74b407ca9710a9f85e0.zip | |
feat!: Regex result wrapper on .to_regex(), plus flag kwargs at the terminal (#270)
Reshapes what `.to_regex()` returns and takes, so pattern compilation
becomes a first-class edify value.
## `Regex` result wrapper class
`edify.Regex` is a composition wrapper over `re.Pattern` (which is a
CPython C type and cannot be subclassed). It exposes:
- `.source` — the emitted regex string.
- `.compiled` — the underlying `re.Pattern` for callers that need
identity checks, `isinstance` checks, or direct interop with libraries
typed on `re.Pattern`.
- Eight direct delegates — `.match()`, `.search()`, `.fullmatch()`,
`.findall()`, `.finditer()`, `.sub()`, `.subn()`, `.split()` — mirroring
the `re.Pattern` query surface.
- Value equality on `(source, flags)` plus `__hash__`.
## `to_regex()` now returns `Regex`
The terminal returns `edify.Regex` instead of raw `re.Pattern`. The
existing eight query methods continue to work via delegation. `.match()`
/ `.search()` / … on builders (via `MatcherMixin`) still work because
they go through the same delegation.
**Breaking:** `isinstance(x, re.Pattern)` and `re.Pattern`-typed
annotations on values returned from `.to_regex()` break. Callers that
need the raw pattern read `.compiled`.
## Flag kwargs on `.to_regex(...)`
`.to_regex()` now accepts six keyword-only flag arguments —
`ascii_only`, `debug`, `ignore_case`, `multiline`, `dotall`, `verbose` —
that OR-merge into the flag snapshot the builder already carries.
Passing `ignore_case=True` at the terminal is equivalent to calling
`.ignore_case()` mid-chain. Kwargs never turn a chain-set flag off; the
terminal is the natural home for pattern-global settings.
## Example
```python
from edify import RegexBuilder
email = RegexBuilder().start_of_input().one_or_more().letter().end_of_input()
regex = email.to_regex(ignore_case=True)
regex.source # ^[a-zA-Z]+$
regex.match("Hello") # <re.Match ...>
regex.compiled # <re.Pattern object; flags=re.IGNORECASE>
```
Closes #133
Closes #134
Closes #128
Diffstat (limited to 'tests/pattern/classes.test.py')
0 files changed, 0 insertions, 0 deletions
