Skip to content

Commit a19417e

Browse files
committed
chore: lint codebase
1 parent 6fbf536 commit a19417e

File tree

6 files changed

+75
-47
lines changed

6 files changed

+75
-47
lines changed

redshift_connector/interval.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import typing
2-
from redshift_connector.config import max_int4, max_int8, min_int4, min_int8
32
from datetime import timedelta as Timedelta
43

4+
from redshift_connector.config import max_int4, max_int8, min_int4, min_int8
5+
56

67
class Interval:
78
"""An Interval represents a measurement of time. In Amazon Redshift, an
@@ -90,6 +91,7 @@ def total_seconds(self: "Interval") -> float:
9091
"""Total seconds in the Interval, excluding month field."""
9192
return ((self.days * 86400) * 10**6 + self.microseconds) / 10**6
9293

94+
9395
class IntervalYearToMonth(Interval):
9496
"""An Interval Year To Month represents a measurement of time of the order
9597
of a few months and years. Note the difference with Interval which can
@@ -99,9 +101,10 @@ class IntervalYearToMonth(Interval):
99101
100102
Note that 1year = 12months.
101103
"""
102-
def __init__(self: "IntervalYearToMonth",
103-
months: int = 0,
104-
year_month: typing.Tuple[int, int] = None) -> None:
104+
105+
def __init__(
106+
self: "IntervalYearToMonth", months: int = 0, year_month: typing.Optional[typing.Tuple[int, int]] = None
107+
) -> None:
105108
if year_month is not None:
106109
year, month = year_month
107110
self.months = year * 12 + month
@@ -121,7 +124,7 @@ def _setMonths(self: "IntervalYearToMonth", value: int) -> None:
121124
# days = property(lambda self: self._days, _setDays)
122125
months = property(lambda self: self._months, _setMonths)
123126

124-
def getYearMonth(self: "IntervalDayToSecond") -> typing.Tuple[int, int]:
127+
def getYearMonth(self: "IntervalYearToMonth") -> typing.Tuple[int, int]:
125128
years = int(self.months / 12)
126129
months = self.months - 12 * years
127130
return (years, months)
@@ -130,15 +133,12 @@ def __repr__(self: "IntervalYearToMonth") -> str:
130133
return "<IntervalYearToMonth %s months>" % (self.months)
131134

132135
def __eq__(self: "IntervalYearToMonth", other: object) -> bool:
133-
return (
134-
other is not None
135-
and isinstance(other, IntervalYearToMonth)
136-
and self.months == other.months
137-
)
136+
return other is not None and isinstance(other, IntervalYearToMonth) and self.months == other.months
138137

139-
def __neq__(self: "IntervalYearToMonth", other: "IntervalYearToMonth") -> bool:
138+
def __neq__(self: "IntervalYearToMonth", other: "Interval") -> bool:
140139
return not self.__eq__(other)
141140

141+
142142
class IntervalDayToSecond(Interval):
143143
"""An Interval Day To Second represents a measurement of time of the order
144144
of a few microseconds. Note the difference with Interval which can
@@ -148,9 +148,10 @@ class IntervalDayToSecond(Interval):
148148
149149
Note that 1day = 24 * 3600 * 1000000 microseconds.
150150
"""
151-
def __init__(self: "IntervalDayToSecond",
152-
microseconds: int = 0,
153-
timedelta: Timedelta = None) -> None:
151+
152+
def __init__(
153+
self: "IntervalDayToSecond", microseconds: int = 0, timedelta: typing.Optional[Timedelta] = None
154+
) -> None:
154155
if timedelta is not None:
155156
self.microseconds = int(timedelta.total_seconds() * (10**6))
156157
else:
@@ -173,13 +174,9 @@ def __repr__(self: "IntervalDayToSecond") -> str:
173174
return "<IntervalDayToSecond %s microseconds>" % (self.microseconds)
174175

175176
def __eq__(self: "IntervalDayToSecond", other: object) -> bool:
176-
return (
177-
other is not None
178-
and isinstance(other, IntervalDayToSecond)
179-
and self.microseconds == other.microseconds
180-
)
177+
return other is not None and isinstance(other, IntervalDayToSecond) and self.microseconds == other.microseconds
181178

182-
def __neq__(self: "IntervalDayToSecond", other: "IntervalDayToSecond") -> bool:
179+
def __neq__(self: "IntervalDayToSecond", other: "Interval") -> bool:
183180
return not self.__eq__(other)
184181

185182
def getTimedelta(self: "IntervalDayToSecond") -> Timedelta:

redshift_connector/utils/type_utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
_client_encoding,
2323
timegm,
2424
)
25-
from redshift_connector.interval import Interval, IntervalYearToMonth, IntervalDayToSecond
25+
from redshift_connector.interval import (
26+
Interval,
27+
IntervalDayToSecond,
28+
IntervalYearToMonth,
29+
)
2630
from redshift_connector.pg_types import (
2731
PGEnum,
2832
PGJson,
@@ -205,11 +209,13 @@ def interval_send_integer(v: typing.Union[Timedelta, Interval]) -> bytes:
205209

206210
return typing.cast(bytes, qhh_pack(microseconds, 0, months))
207211

212+
208213
def intervaly2m_send_integer(v: IntervalYearToMonth) -> bytes:
209214
months = v.months # type: ignore
210215

211216
return typing.cast(bytes, i_pack(months))
212217

218+
213219
def intervald2s_send_integer(v: IntervalDayToSecond) -> bytes:
214220
microseconds = v.microseconds # type: ignore
215221

@@ -284,14 +290,17 @@ def interval_recv_integer(data: bytes, offset: int, length: int) -> typing.Union
284290
else:
285291
return Timedelta(days, seconds, micros)
286292

293+
287294
def intervaly2m_recv_integer(data: bytes, offset: int, length: int) -> IntervalYearToMonth:
288-
months, = typing.cast(typing.Tuple[int], i_unpack(data, offset))
295+
(months,) = typing.cast(typing.Tuple[int], i_unpack(data, offset))
289296
return IntervalYearToMonth(months)
290297

298+
291299
def intervald2s_recv_integer(data: bytes, offset: int, length: int) -> IntervalDayToSecond:
292-
microseconds, = typing.cast(typing.Tuple[int], q_unpack(data, offset))
300+
(microseconds,) = typing.cast(typing.Tuple[int], q_unpack(data, offset))
293301
return IntervalDayToSecond(microseconds)
294302

303+
295304
def timetz_recv_binary(data: bytes, offset: int, length: int) -> time:
296305
return time_recv_binary(data, offset, length).replace(tzinfo=Timezone.utc)
297306

test/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def serverless_cname_db_kwargs() -> typing.Dict[str, typing.Union[str, bool]]:
130130

131131
return db_connect
132132

133+
133134
@pytest.fixture(scope="class")
134135
def ds_consumer_db_kwargs() -> typing.Dict[str, str]:
135136
db_connect = {

test/integration/datatype/_generate_test_datatype_tables.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import typing
44
from datetime import date, datetime, time, timezone
55
from decimal import Decimal
6-
from redshift_connector.interval import IntervalYearToMonth, IntervalDayToSecond
76
from enum import Enum, auto
87

8+
from redshift_connector.interval import IntervalDayToSecond, IntervalYearToMonth
9+
910
if typing.TYPE_CHECKING:
1011
from redshift_connector import Connection
1112

@@ -36,7 +37,17 @@ def list(cls) -> typing.List["RedshiftDatatypes"]:
3637

3738

3839
redshift_test_data: typing.Dict[
39-
str, typing.Union[typing.Tuple[typing.Tuple[str, str], ...], typing.List[typing.Tuple[str, ...]]]
40+
str,
41+
typing.Union[
42+
typing.Tuple[typing.Tuple[str, str], ...],
43+
typing.List[
44+
typing.Union[
45+
typing.Tuple[str, ...],
46+
typing.Tuple[str, IntervalYearToMonth, str],
47+
typing.Tuple[str, IntervalDayToSecond, str],
48+
]
49+
],
50+
],
4051
] = {
4152
RedshiftDatatypes.geometry.name: (
4253
(
@@ -161,14 +172,14 @@ def list(cls) -> typing.List["RedshiftDatatypes"]:
161172
RedshiftDatatypes.intervaly2m.name: [
162173
("37 months", IntervalYearToMonth(37), "y2m_postgres_format"),
163174
("1-1", IntervalYearToMonth(13), "y2m_sql_standard_format"),
164-
("-178956970-8", IntervalYearToMonth(-2**31), "y2m_min_value"),
165-
("178956970-7", IntervalYearToMonth(2**31 - 1), "y2m_max_value")
175+
("-178956970-8", IntervalYearToMonth(-(2**31)), "y2m_min_value"),
176+
("178956970-7", IntervalYearToMonth(2**31 - 1), "y2m_max_value"),
166177
],
167178
RedshiftDatatypes.intervald2s.name: [
168179
("10 days 48 hours", IntervalDayToSecond(12 * 86400 * 1000000), "d2s_postgres_format"),
169180
("10 23:59:59.999999", IntervalDayToSecond(11 * 86400 * 1000000 - 1), "d2s_sql_standard_format"),
170-
("-106751991 -04:00:54.775808", IntervalDayToSecond(-2**63), "d2s_min_value"),
171-
("106751991 04:00:54.775807", IntervalDayToSecond(2**63 - 1), "d2s_max_value")
181+
("-106751991 -04:00:54.775808", IntervalDayToSecond(-(2**63)), "d2s_min_value"),
182+
("106751991 04:00:54.775807", IntervalDayToSecond(2**63 - 1), "d2s_max_value"),
172183
]
173184
# TODO: re-enable
174185
# RedshiftDatatypes.geography.name: (

test/integration/datatype/test_datatypes.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import typing
55
from datetime import datetime as Datetime
66
from datetime import timezone
7-
from redshift_connector.interval import IntervalYearToMonth, IntervalDayToSecond
87
from math import isclose
98
from test.integration.datatype._generate_test_datatype_tables import ( # type: ignore
109
DATATYPES_WITH_MS,
@@ -22,6 +21,7 @@
2221

2322
import redshift_connector
2423
from redshift_connector.config import ClientProtocolVersion
24+
from redshift_connector.interval import IntervalDayToSecond, IntervalYearToMonth
2525

2626
conf = configparser.ConfigParser()
2727
root_path = os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(__file__, os.pardir))))
@@ -120,9 +120,9 @@ def test_redshift_varbyte_insert(db_kwargs, _input, client_protocol) -> None:
120120
assert len(results[0]) == 2
121121
assert results[0][1] == bytes(data, encoding="utf-8").hex()
122122

123+
123124
@pytest.mark.parametrize("client_protocol", ClientProtocolVersion.list())
124-
@pytest.mark.parametrize("datatype", [RedshiftDatatypes.intervaly2m.name,
125-
RedshiftDatatypes.intervald2s.name])
125+
@pytest.mark.parametrize("datatype", [RedshiftDatatypes.intervaly2m.name, RedshiftDatatypes.intervald2s.name])
126126
def test_redshift_interval_insert(db_kwargs, datatype, client_protocol) -> None:
127127
db_kwargs["client_protocol_version"] = client_protocol
128128
data = redshift_test_data[datatype]
@@ -139,13 +139,13 @@ def test_redshift_interval_insert(db_kwargs, datatype, client_protocol) -> None:
139139
print(results)
140140
for idx, result in enumerate(results):
141141
print(result[1], data[idx][1])
142-
assert(isinstance(result[1], redshift_type))
143-
assert(result[1] == data[idx][1])
142+
assert isinstance(result[1], redshift_type)
143+
assert result[1] == data[idx][1]
144144
cursor.execute("drop table t_interval")
145145

146+
146147
@pytest.mark.parametrize("client_protocol", ClientProtocolVersion.list())
147-
@pytest.mark.parametrize("datatype", [RedshiftDatatypes.intervaly2m.name,
148-
RedshiftDatatypes.intervald2s.name])
148+
@pytest.mark.parametrize("datatype", [RedshiftDatatypes.intervaly2m.name, RedshiftDatatypes.intervald2s.name])
149149
def test_redshift_interval_prep_stmt(db_kwargs, datatype, client_protocol) -> None:
150150
db_kwargs["client_protocol_version"] = client_protocol
151151
data = redshift_test_data[datatype]
@@ -155,19 +155,20 @@ def test_redshift_interval_prep_stmt(db_kwargs, datatype, client_protocol) -> No
155155
with con.cursor() as cursor:
156156
cursor.execute("create table t_interval_ps(id text, v1 {})".format(datatype))
157157
cursor.paramstyle = "pyformat"
158-
cursor.executemany("insert into t_interval_ps(id, v1) values (%(id_val)s, %(v1_val)s)",
159-
({"id_val": row[-1], "v1_val": row[1]} for row in data[:2]))
158+
cursor.executemany(
159+
"insert into t_interval_ps(id, v1) values (%(id_val)s, %(v1_val)s)",
160+
({"id_val": row[-1], "v1_val": row[1]} for row in data[:2]),
161+
)
160162
cursor.paramstyle = "qmark"
161-
cursor.executemany("insert into t_interval_ps values (?, ?)",
162-
([row[-1], row[1]] for row in data[2:]))
163+
cursor.executemany("insert into t_interval_ps values (?, ?)", ([row[-1], row[1]] for row in data[2:]))
163164
cursor.execute("select id, v1 from t_interval_ps")
164165
results: typing.Tuple = cursor.fetchall()
165166
assert len(results) == len(data)
166167
print(results)
167168
for idx, result in enumerate(results):
168169
print(result[1], data[idx][1])
169-
assert(isinstance(result[1], redshift_type))
170-
assert(result[1] == data[idx][1])
170+
assert isinstance(result[1], redshift_type)
171+
assert result[1] == data[idx][1]
171172
cursor.execute("drop table t_interval_ps")
172173

173174

test/unit/datatype/test_data_in.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import pytest # type: ignore
88

99
import redshift_connector
10-
from redshift_connector.interval import Interval, IntervalYearToMonth, IntervalDayToSecond
10+
from redshift_connector.interval import (
11+
Interval,
12+
IntervalDayToSecond,
13+
IntervalYearToMonth,
14+
)
1115
from redshift_connector.utils import type_utils
1216

1317

@@ -347,14 +351,19 @@ class Datatypes(Enum):
347351
(b"\x01\x0e\x00\x00\x00*", 2, 4, IntervalYearToMonth(months=42)),
348352
(b"\x01\x0e\x00\x00\x00*", 2, 4, IntervalYearToMonth(year_month=(3, 6))),
349353
(b"\x00\x00\x00\x02", 0, 4, IntervalYearToMonth(months=2)),
350-
(b"\x00\x00\x00\x02", 0, 4, IntervalYearToMonth(year_month=(1, -10)))
354+
(b"\x00\x00\x00\x02", 0, 4, IntervalYearToMonth(year_month=(1, -10))),
351355
],
352356
Datatypes.intervald2s: [
353357
(b"\x00\x00\x0c\x00\x00\x00\x00\x02\xdf\xda\xe8\x00", 4, 8, IntervalDayToSecond(microseconds=12345600000)),
354-
(b"\x00\x00\x0c\x00\x00\x00\x00\x02\xdf\xda\xe8\x00", 4, 8, IntervalDayToSecond(timedelta=timedelta(hours=3, minutes=25, seconds=45.6))),
358+
(
359+
b"\x00\x00\x0c\x00\x00\x00\x00\x02\xdf\xda\xe8\x00",
360+
4,
361+
8,
362+
IntervalDayToSecond(timedelta=timedelta(hours=3, minutes=25, seconds=45.6)),
363+
),
355364
(b"\x00\x00\x00\x00\x00\x00\x00\x02", 0, 8, IntervalDayToSecond(microseconds=2)),
356-
(b"\x00\x00\x00\x00\x00\x00\x00\x02", 0, 8, IntervalDayToSecond(timedelta=timedelta(microseconds=2)))
357-
]
365+
(b"\x00\x00\x00\x00\x00\x00\x00\x02", 0, 8, IntervalDayToSecond(timedelta=timedelta(microseconds=2))),
366+
],
358367
}
359368

360369

0 commit comments

Comments
 (0)