Skip to content

Commit fac17dc

Browse files
altendkysdispater
authored andcommitted
Reject text after matching string in from_format() (#372)
* Reject text after matching string in from_format() * Let black have its way
1 parent f629fa6 commit fac17dc

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

pendulum/formatting/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def parse(
403403
lambda m: self._replace_tokens(m.group(0), locale), escaped_fmt
404404
)
405405

406-
if not re.match(pattern, time):
406+
if not re.search("^" + pattern + "$", time):
407407
raise ValueError("String does not match format {}".format(fmt))
408408

409409
re.sub(pattern, lambda m: self._get_parsed_values(m, parsed, locale, now), time)

tests/datetime/test_from_format.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def test_from_format_returns_datetime():
1313
assert "UTC" == d.timezone_name
1414

1515

16+
def test_from_format_rejects_extra_text():
17+
with pytest.raises(ValueError):
18+
pendulum.from_format("1975-05-21 22:32:11 extra text", "YYYY-MM-DD HH:mm:ss")
19+
20+
1621
def test_from_format_with_timezone_string():
1722
d = pendulum.from_format(
1823
"1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz="Europe/London"

0 commit comments

Comments
 (0)