Skip to content

Commit 0a14d5d

Browse files
authored
chore: Linting, Clean-up, and Standardization (#66)
Came in to fix one Linting issue, ended up doing a more detailed clean-up: * replaced all instances of `type(object) == Type` for `isinstance(object, type)` * replaced all instances of `"some {} string".format(x)` with `f"some {x} string" * simplified long compound comparisons to reduce risk of bugs * fixed a bug There are still more improvements that could be made, but I've already done way more than I intended.
1 parent bd0b283 commit 0a14d5d

File tree

21 files changed

+149
-186
lines changed

21 files changed

+149
-186
lines changed

cloudquery/sdk/internal/memdb/memdb.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cloudquery.sdk import plugin
22
from cloudquery.sdk import message
33
from cloudquery.sdk import schema
4-
from typing import List, Generator, Any, Dict
4+
from typing import List, Generator, Dict
55
import pyarrow as pa
66

77
NAME = "memdb"
@@ -24,13 +24,13 @@ def sync(
2424
for table, record in self._db.items():
2525
yield message.SyncInsertMessage(record)
2626

27-
def write(self, msg_iterator: Generator[message.WriteMessage, None, None]) -> None:
28-
for msg in msg_iterator:
29-
if type(msg) == message.WriteMigrateTableMessage:
27+
def write(self, writer: Generator[message.WriteMessage, None, None]) -> None:
28+
for msg in writer:
29+
if isinstance(msg, message.WriteMigrateTableMessage):
3030
if msg.table.name not in self._db:
3131
self._db[msg.table.name] = msg.table
3232
self._tables[msg.table.name] = msg.table
33-
elif type(msg) == message.WriteInsertMessage:
33+
elif isinstance(msg, message.WriteInsertMessage):
3434
table = schema.Table.from_arrow_schema(msg.record.schema)
3535
self._db[table.name] = msg.record
3636
else:

cloudquery/sdk/scalar/binary.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Binary(Scalar):
66
def __eq__(self, scalar: Scalar) -> bool:
77
if scalar is None:
88
return False
9-
if type(scalar) == Binary:
9+
if isinstance(scalar, Binary):
1010
return self._value == scalar._value and self._valid == scalar._valid
1111
return False
1212

@@ -23,13 +23,13 @@ def set(self, value: any):
2323
self._value = value.value
2424
return
2525

26-
if type(value) == bytes:
26+
if isinstance(value, bytes):
2727
self._valid = True
2828
self._value = value
29-
elif type(value) == str:
29+
elif isinstance(value, str):
3030
self._valid = True
3131
self._value = value.encode()
3232
else:
3333
raise ScalarInvalidTypeError(
34-
"Invalid type {} for Binary scalar".format(type(value))
34+
f"Invalid type {type(value)} for Binary scalar"
3535
)

cloudquery/sdk/scalar/bool.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def parse_string_to_bool(input_string):
1010

1111
if lower_input in true_strings:
1212
return True
13-
elif lower_input in false_strings:
13+
if lower_input in false_strings:
1414
return False
1515
else:
16-
raise ScalarInvalidTypeError("Invalid boolean string: {}".format(input_string))
16+
raise ScalarInvalidTypeError(f"Invalid boolean string: {input_string}")
1717

1818

1919
class Bool(Scalar):
2020
def __eq__(self, scalar: Scalar) -> bool:
2121
if scalar is None:
2222
return False
23-
if type(scalar) == Bool:
23+
if isinstance(scalar, Bool):
2424
return self._value == scalar._value and self._valid == scalar._valid
2525
return False
2626

@@ -38,13 +38,11 @@ def set(self, value: Any):
3838
self._value = value.value
3939
return
4040

41-
if type(value) == bool:
41+
if isinstance(value, bool):
4242
self._value = value
43-
elif type(value) == str:
43+
elif isinstance(value, str):
4444
self._value = parse_string_to_bool(value)
4545
else:
46-
raise ScalarInvalidTypeError(
47-
"Invalid type {} for Bool scalar".format(type(value))
48-
)
46+
raise ScalarInvalidTypeError(f"Invalid type {type(value)} for Bool scalar")
4947

5048
self._valid = True

cloudquery/sdk/scalar/date32.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Date32(Scalar):
77
def __eq__(self, scalar: Scalar) -> bool:
88
if scalar is None:
99
return False
10-
if type(scalar) == Date32:
10+
if isinstance(scalar, Date32):
1111
return self._value == scalar._value and self._valid == scalar._valid
1212
return False
1313

@@ -25,15 +25,15 @@ def set(self, value: Any):
2525
self._value = value.value
2626
return
2727

28-
if type(value) == datetime:
28+
if isinstance(value, datetime):
2929
self._value = value
30-
elif type(value) == str:
30+
elif isinstance(value, str):
3131
self._value = datetime.strptime(value, "%Y-%m-%d")
32-
elif type(value) == time:
32+
elif isinstance(value, time):
3333
self._value = datetime.combine(datetime.today(), value)
3434
else:
3535
raise ScalarInvalidTypeError(
36-
"Invalid type {} for Date32 scalar".format(type(value))
36+
f"Invalid type {type(value)} for Date32 scalar"
3737
)
3838

3939
self._valid = True

cloudquery/sdk/scalar/date64.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Date64(Scalar):
77
def __eq__(self, scalar: Scalar) -> bool:
88
if scalar is None:
99
return False
10-
if type(scalar) == Date64:
10+
if isinstance(scalar, Date64):
1111
return self._value == scalar._value and self._valid == scalar._valid
1212
return False
1313

@@ -25,15 +25,15 @@ def set(self, value: Any):
2525
self._value = value.value
2626
return
2727

28-
if type(value) == datetime:
28+
if isinstance(value, datetime):
2929
self._value = value
30-
elif type(value) == str:
30+
elif isinstance(value, str):
3131
self._value = datetime.strptime(value, "%Y-%m-%d")
32-
elif type(value) == time:
32+
elif isinstance(value, time):
3333
self._value = datetime.combine(datetime.today(), value)
3434
else:
3535
raise ScalarInvalidTypeError(
36-
"Invalid type {} for Date64 scalar".format(type(value))
36+
f"Invalid type {type(value)} for Date64 scalar"
3737
)
3838

3939
self._valid = True

cloudquery/sdk/scalar/float.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, valid: bool = False, value: any = None, bitwidth: int = 64):
99
def __eq__(self, scalar: Scalar) -> bool:
1010
if scalar is None:
1111
return False
12-
if type(scalar) == Float and self._bitwidth == scalar.bitwidth:
12+
if isinstance(scalar, Float) and self._bitwidth == scalar.bitwidth:
1313
return self._value == scalar._value and self._valid == scalar._valid
1414
return False
1515

@@ -31,19 +31,19 @@ def set(self, value: any):
3131
self._value = value.value
3232
return
3333

34-
if type(value) == int:
34+
if isinstance(value, int):
3535
self._value = float(value)
36-
elif type(value) == float:
36+
elif isinstance(value, float):
3737
self._value = value
38-
elif type(value) == str:
38+
elif isinstance(value, str):
3939
try:
4040
self._value = float(value)
41-
except ValueError:
41+
except ValueError as e:
4242
raise ScalarInvalidTypeError(
43-
"Invalid value for Float{} scalar".format(self._bitwidth)
44-
)
43+
f"Invalid value for Float{self._bitwidth} scalar"
44+
) from e
4545
else:
4646
raise ScalarInvalidTypeError(
47-
"Invalid type {} for Float{} scalar".format(type(value), self._bitwidth)
47+
f"Invalid type {type(value)} for Float{self._bitwidth} scalar"
4848
)
4949
self._valid = True

cloudquery/sdk/scalar/int.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, valid: bool = False, value: any = None, bitwidth: int = 64):
1111
def __eq__(self, scalar: Scalar) -> bool:
1212
if scalar is None:
1313
return False
14-
if type(scalar) == Int and self._bitwidth == scalar.bitwidth:
14+
if isinstance(scalar, Int) and self._bitwidth == scalar.bitwidth:
1515
return self._value == scalar._value and self._valid == scalar._valid
1616
return False
1717

@@ -33,24 +33,24 @@ def set(self, value: any):
3333
self._value = value.value
3434
return
3535

36-
if type(value) == int:
36+
if isinstance(value, int):
3737
val = value
38-
elif type(value) == float:
38+
elif isinstance(value, float):
3939
val = int(value)
40-
elif type(value) == str:
40+
elif isinstance(value, str):
4141
try:
4242
val = int(value)
4343
except ValueError as e:
4444
raise ScalarInvalidTypeError(
45-
"Invalid type for Int{} scalar".format(self._bitwidth)
45+
f"Invalid type for Int{self._bitwidth} scalar"
4646
) from e
4747
else:
4848
raise ScalarInvalidTypeError(
49-
"Invalid type {} for Int{} scalar".format(type(value), self._bitwidth)
49+
f"Invalid type {type(value)} for Int{self._bitwidth} scalar"
5050
)
5151
if val < self._min or val >= self._max:
5252
raise ScalarInvalidTypeError(
53-
"Invalid Int{} scalar with value {}".format(self._bitwidth, val)
53+
f"Invalid Int{self._bitwidth} scalar with value {val}"
5454
)
5555
self._value = val
5656
self._valid = True

cloudquery/sdk/scalar/json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class JSON(Scalar):
66
def __eq__(self, scalar: Scalar) -> bool:
77
if scalar is None:
88
return False
9-
if type(scalar) == JSON:
9+
if isinstance(scalar, JSON):
1010
return self._value == scalar._value and self._valid == scalar._valid
1111
return False
1212

@@ -18,7 +18,7 @@ def set(self, value: any):
1818
if value is None:
1919
return
2020

21-
if type(value) == str or type(value) == bytes:
21+
if isinstance(value, (str, bytes)):
2222
# test if it is a valid json
2323
json.loads(value)
2424
self._value = value

cloudquery/sdk/scalar/list.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from cloudquery.sdk.scalar import Scalar, ScalarInvalidTypeError
22
from .scalar import NULL_VALUE
33
from .vector import Vector
4-
from typing import Any, Type, Union
4+
from typing import Any, Type
55

66

77
class List(Scalar):
@@ -10,12 +10,8 @@ def __init__(self, scalar_type: Type[Scalar]):
1010
self._value = Vector(scalar_type)
1111
self._type = scalar_type
1212

13-
def __eq__(self, other: Union[None, "List"]) -> bool:
14-
if other is None:
15-
return False
16-
if type(self) != type(other):
17-
return False
18-
if self._valid != other._valid:
13+
def __eq__(self, other: "List") -> bool:
14+
if (not isinstance(other, self.__class__)) or self._valid != other._valid:
1915
return False
2016
return self._value == other._value
2117

@@ -27,29 +23,29 @@ def type(self):
2723
def value(self):
2824
return self._value
2925

30-
def set(self, val: Any):
31-
if val is None:
26+
def set(self, value: Any):
27+
if value is None:
3228
self._valid = False
3329
self._value = Vector()
3430
return
3531

36-
if isinstance(val, Scalar) and type(val) == self._type:
37-
if not val.is_valid:
32+
if isinstance(value, self._type):
33+
if not value.is_valid:
3834
self._valid = False
3935
self._value = Vector()
4036
return
41-
return self.set([val.value])
37+
return self.set([value.value])
4238

43-
if isinstance(val, (list, tuple)):
39+
if isinstance(value, (list, tuple)):
4440
self._value = Vector()
45-
for item in val:
41+
for item in value:
4642
scalar = self._type()
4743
scalar.set(item)
4844
self._value.append(scalar)
4945
self._valid = True
5046
return
5147

52-
raise ScalarInvalidTypeError("Invalid type {} for List".format(type(val)))
48+
raise ScalarInvalidTypeError(f"Invalid type {type(value)} for List")
5349

5450
def __str__(self) -> str:
5551
if not self._valid:

0 commit comments

Comments
 (0)