Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshusa committed Jan 8, 2024
1 parent ca6593a commit 1de8334
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.27
====
* Add support for regex Patterns

2.26
====
* Update type hints file
Expand Down
6 changes: 6 additions & 0 deletions docs/supported_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,9 @@ When dumping, the default is to dump a list of ints, unless `isodates=True` is s
The format with the list of ints is deprecated and kept for backward compatibility. Everybody should use the ISO 8601 strings.

The format with the list of ints does not support timezones.

re.Pattern
----------

Loads a str or bytes as a compiled Pattern object by passing through re.compile.
When dumping gives back the original str or bytes pattern.
24 changes: 24 additions & 0 deletions tests/test_dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,30 @@ def test_pattern(self):
assert loader.load(r'[bc](at|ot)\d+', typing.Pattern) == re.compile(r'[bc](at|ot)\d+')
assert loader.load(br'[bc](at|ot)\d+', typing.Pattern) == re.compile(br'[bc](at|ot)\d+')

# Right type, invalid value
with self.assertRaises(exceptions.TypedloadException) as e:
assert loader.load(r'(?P<my_group>[bc])(?P<my_group>(at|ot))\d+', re.Pattern)
with self.assertRaises(exceptions.TypedloadException) as e:
assert loader.load(br'(?P<my_group>[bc])(?P<my_group>(at|ot))\d+', re.Pattern)
with self.assertRaises(exceptions.TypedloadException) as e:
assert loader.load(r'(?P<my_group>[bc])(?P<my_group>(at|ot))\d+', typing.Pattern)
with self.assertRaises(exceptions.TypedloadException) as e:
assert loader.load(br'(?P<my_group>[bc])(?P<my_group>(at|ot))\d+', typing.Pattern)

# Wrong type
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(33, re.Pattern)
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(33, typing.Pattern)
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(False, re.Pattern)
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(False, typing.Pattern)
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(None, re.Pattern)
with self.assertRaises(exceptions.TypedloadTypeError) as e:
assert loader.load(None, typing.Pattern)

def test_ipaddress(self):
loader = dataloader.Loader()
assert loader.load('10.10.10.1', IPv4Address) == IPv4Address('10.10.10.1')
Expand Down
9 changes: 7 additions & 2 deletions typedload/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ def __init__(self, **kwargs) -> None:
(is_typeddict, _typeddictload),
(lambda type_: type_ in {datetime.date, datetime.time, datetime.datetime}, _datetimeload),
(is_pattern, _patternload),
(lambda type_: type_ in self.strconstructed, _strconstructload),
(lambda type_: type_ == datetime.timedelta, _timedeltaload),
(lambda type_: type_ in self.strconstructed, _strconstructload),
(is_attrs, _attrload),
(is_any, _anyload),
(is_newtype, _newtypeload),
Expand Down Expand Up @@ -822,7 +822,12 @@ def _patternload(l: Loader, value: Any, type_) -> re.Pattern:
(input_type,) = type_.__args__
if input_type in {bytes, str} and type(value) != input_type:
raise TypedloadValueError('Got %s of type %s, expected %s' % (repr(value), tname(type(value)), tname(type_)), value=value, type_=type_)
return re.compile(value)
try:
return re.compile(value)
except re.error as e:
raise TypedloadException(str(e), value=value, type_=type_)
except TypeError as e:
raise TypedloadTypeError(str(e), value=value, type_=type_)


def _timedeltaload(l: Loader, value, type_) -> datetime.timedelta:
Expand Down
3 changes: 3 additions & 0 deletions typedload/typechecks.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ def is_literal(type_: Any) -> bool:


def is_pattern(type_: Any) -> bool:
'''
Check if the type is a re.Pattern
'''
return type_ == Pattern or getattr(type_, "__origin__", None) == Pattern


Expand Down

0 comments on commit 1de8334

Please sign in to comment.