Skip to content
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

fix(GSheets): invalid pattern #458

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/shillelagh/adapters/api/gsheets/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def get_field(
"""
Return a Shillelagh ``Field`` from a Google Chart API results column.
"""
# GSheets returns type ``datetime`` for timestamps, but also for time of day and
# durations. We need to tokenize the pattern in order to figure out the correct type.
if col["type"] == "datetime" and "pattern" in col:
col["type"] = infer_column_type(col["pattern"])
# GSheets returns all kind of incorrect types (eg, ``datetime`` or ``timeofday`` for
# timestamps. We parse the patter in order to infer the correct type.
if col["type"] in {"datetime", "timeofday"} and "pattern" in col:
col["type"] = infer_column_type(col["pattern"].lower())

type_map: Dict[str, Tuple[Type[GSheetsField], List[Type[Filter]]]] = {
"string": (GSheetsString, [Range, Equal, NotEqual, Like, IsNull, IsNotNull]),
Expand Down
2 changes: 2 additions & 0 deletions src/shillelagh/adapters/api/gsheets/parsing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def tokenize(pattern: str, classes: List[Type[Token]]) -> Iterator[Token]:
token, pattern = class_.consume(pattern, tokens)
tokens.append(token)
break
else:
raise InvalidPattern(f'Could not consume "{pattern}"')

# combine unescaped literals
while tokens:
Expand Down
3 changes: 3 additions & 0 deletions src/shillelagh/adapters/api/gsheets/parsing/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ def infer_column_type(pattern: str) -> str:

GSheets returns ``datetime`` as the type for timestamps, but also for time of day and
durations. We need to parse the pattern to figure out the exact type.

This also handles a case where a timestamp (``1/2/24 14:41``) with a proper pattern
(``M/D/YY h:mm``) was being returned as type ``timeofday``.
"""
classes = [
# durations should come first because they need to be modified
Expand Down
15 changes: 15 additions & 0 deletions tests/adapters/api/gsheets/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ def test_get_field() -> None:
Order.ANY,
True,
)
assert get_field(
{
"id": "A",
"label": "Send time",
"type": "timeofday",
"pattern": "M/D/YY h:mm",
},
timezone,
) == GSheetsDateTime(
[Range, Equal, NotEqual, IsNull, IsNotNull],
Order.ANY,
True,
"M/D/YY h:mm",
timezone,
)


def test_format_error_message() -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/adapters/api/gsheets/parsing/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from shillelagh.adapters.api.gsheets.parsing.base import (
LITERAL,
InvalidPattern,
InvalidValue,
is_unescaped_literal,
tokenize,
Expand Down Expand Up @@ -84,6 +85,10 @@ def test_tokenize() -> None:
LITERAL(")"),
]

with pytest.raises(InvalidPattern) as excinfo:
list(tokenize("dd/mm/yyyy", []))
assert str(excinfo.value) == 'Could not consume "dd/mm/yyyy"'


def test_is_unescaped_literal() -> None:
"""
Expand Down
Loading