Skip to content

Commit 6408f84

Browse files
authored
chore: add type checks for unix epoch conversions (#1343)
1 parent b26e135 commit 6408f84

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

bigframes/operations/datetime_ops.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ToDatetimeOp(base_ops.UnaryOp):
4343
format: typing.Optional[str] = None
4444
unit: typing.Optional[str] = None
4545

46-
def output_type(self, *input_types):
46+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
4747
if input_types[0] not in (
4848
dtypes.FLOAT_DTYPE,
4949
dtypes.INT_DTYPE,
@@ -59,7 +59,7 @@ class ToTimestampOp(base_ops.UnaryOp):
5959
format: typing.Optional[str] = None
6060
unit: typing.Optional[str] = None
6161

62-
def output_type(self, *input_types):
62+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
6363
# Must be numeric or string
6464
if input_types[0] not in (
6565
dtypes.FLOAT_DTYPE,
@@ -75,29 +75,35 @@ class StrftimeOp(base_ops.UnaryOp):
7575
name: typing.ClassVar[str] = "strftime"
7676
date_format: str
7777

78-
def output_type(self, *input_types):
78+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
7979
return dtypes.STRING_DTYPE
8080

8181

8282
@dataclasses.dataclass(frozen=True)
8383
class UnixSeconds(base_ops.UnaryOp):
8484
name: typing.ClassVar[str] = "unix_seconds"
8585

86-
def output_type(self, *input_types):
86+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
87+
if input_types[0] is not dtypes.TIMESTAMP_DTYPE:
88+
raise TypeError("expected timestamp input")
8789
return dtypes.INT_DTYPE
8890

8991

9092
@dataclasses.dataclass(frozen=True)
9193
class UnixMillis(base_ops.UnaryOp):
9294
name: typing.ClassVar[str] = "unix_millis"
9395

94-
def output_type(self, *input_types):
96+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
97+
if input_types[0] is not dtypes.TIMESTAMP_DTYPE:
98+
raise TypeError("expected timestamp input")
9599
return dtypes.INT_DTYPE
96100

97101

98102
@dataclasses.dataclass(frozen=True)
99103
class UnixMicros(base_ops.UnaryOp):
100104
name: typing.ClassVar[str] = "unix_micros"
101105

102-
def output_type(self, *input_types):
106+
def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionType:
107+
if input_types[0] is not dtypes.TIMESTAMP_DTYPE:
108+
raise TypeError("expected timestamp input")
103109
return dtypes.INT_DTYPE

tests/system/small/bigquery/test_datetime.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import typing
1616

1717
import pandas as pd
18+
import pytest
1819

1920
from bigframes import bigquery
2021

@@ -32,6 +33,13 @@ def test_unix_seconds(scalars_dfs):
3233
pd.testing.assert_series_equal(actual_res, expected_res)
3334

3435

36+
def test_unix_seconds_incorrect_input_type_raise_error(scalars_dfs):
37+
df, _ = scalars_dfs
38+
39+
with pytest.raises(TypeError):
40+
bigquery.unix_seconds(df["string_col"])
41+
42+
3543
def test_unix_millis(scalars_dfs):
3644
bigframes_df, pandas_df = scalars_dfs
3745

@@ -45,6 +53,13 @@ def test_unix_millis(scalars_dfs):
4553
pd.testing.assert_series_equal(actual_res, expected_res)
4654

4755

56+
def test_unix_millis_incorrect_input_type_raise_error(scalars_dfs):
57+
df, _ = scalars_dfs
58+
59+
with pytest.raises(TypeError):
60+
bigquery.unix_millis(df["string_col"])
61+
62+
4863
def test_unix_micros(scalars_dfs):
4964
bigframes_df, pandas_df = scalars_dfs
5065

@@ -58,6 +73,13 @@ def test_unix_micros(scalars_dfs):
5873
pd.testing.assert_series_equal(actual_res, expected_res)
5974

6075

76+
def test_unix_micros_incorrect_input_type_raise_error(scalars_dfs):
77+
df, _ = scalars_dfs
78+
79+
with pytest.raises(TypeError):
80+
bigquery.unix_micros(df["string_col"])
81+
82+
6183
def _to_unix_epoch(
6284
ts: pd.Timestamp, unit: typing.Literal["s", "ms", "us"]
6385
) -> typing.Optional[int]:

0 commit comments

Comments
 (0)