Skip to content

You now have robust error handling for malformed format strings in Da… #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,30 @@
parts: _Parts = {}
for token in fmt_tokens:
value: Union[Tuple[str, str, str], str]
if token == "Do":
value = match.group("value")
elif token == "W":
value = (match.group("year"), match.group("week"), match.group("day"))
else:
value = match.group(token)

try:
if token == "Do":
value = match.group("value")
elif token == "W":
value = (
match.group("year"),
match.group("week"),
match.group("day"),
)
else:
value = match.group(token)
except IndexError:
raise ParserMatchError(

Check warning on line 434 in arrow/parser.py

View check run for this annotation

Codecov / codecov/patch

arrow/parser.py#L434

Added line #L434 was not covered by tests
f"Format string {fmt!r} does not match input {datetime_string!r} (missing group for token {token!r})."
)
except Exception as e:
raise ParserMatchError(

Check warning on line 438 in arrow/parser.py

View check run for this annotation

Codecov / codecov/patch

arrow/parser.py#L437-L438

Added lines #L437 - L438 were not covered by tests
f"Error while matching group for token {token!r}: {e}"
)
if value is None:
raise ParserMatchError(
f"Unable to find a match group for the specified token {token!r}."
)

self._parse_token(token, value, parts) # type: ignore[arg-type]
)
self._parse_token(token, value, parts)

return self._build_datetime(parts)

Expand Down
Loading