mashumaro
mashumaro copied to clipboard
Add support for typing.Pattern and re.Pattern
Is your feature request related to a problem? Please describe.
There is a standard type Pattern
https://docs.python.org/3/library/typing.html#typing.Pattern. Support for annotations re.Pattern
and typing.Pattern
could be useful.
Describe the solution you'd like
A Pattern
object can be deserialized from string using re.compile
:
print(type(re.compile("[a-z]+"))) # <class 're.Pattern'>
For serialization we can use pattern
property:
print(repr(re.compile("[a-z]+").pattern)) # '[a-z]+'
I'm not sure if we should distinguish different variations of a generic Pattern
type. We can treat them all like Pattern[str]
unless there is a reasonable use of Pattern[bytes]
.