Skip to content

Commit d52b058

Browse files
chore(tmp): debug lines (#371)
* chore(tmp): debug lines * chore(tmp): debug lines * chore(tmp): debug lines * chore(tmp): debug lines * fix(transform): use provided type as contents of list * chore(tmp): try getting more info * chore(tmp): debug lines * chore(tmp): debug * fix(transform): infer list of ints if all items can be ints * fix(transform): infer list of ints if all items can be ints * fix(cast): convert from float to int * fix(transform): infer list of ints if all items can be ints * chore(logs): add another log * chore(logs): add another log * chore(logs): add another log * chore(logs): add another log * chore(logs): add another log and refactor * fix(transform): actually check item not value * chore(logs): cleanup Co-authored-by: Alexander VT <[email protected]>
1 parent 0258577 commit d52b058

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ repos:
1010
- id: no-commit-to-branch
1111
args: [--branch, develop, --branch, master, --pattern, release/.*]
1212
- repo: https://github.com/psf/black
13-
rev: 20.8b1
13+
rev: 22.3.0
1414
hooks:
1515
- id: black

sheepdog/utils/transforms/__init__.py

+49-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def parse_bool_from_string(value):
3030
return mapping.get(strip(value).lower(), value)
3131

3232

33-
def parse_list_from_string(value):
33+
def parse_list_from_string(value, list_type=None):
3434
"""
3535
Handle array fields by converting them to a list.
3636
Try to cast to float to handle arrays of numbers.
@@ -39,11 +39,38 @@ def parse_list_from_string(value):
3939
1,2,3 -> [1,2,3]
4040
"""
4141
items = [x.strip() for x in value.split(",")]
42+
43+
all_ints = True
4244
try:
43-
items = [float(x) for x in items]
44-
except ValueError:
45-
pass # not an array of numbers
46-
return items
45+
# TODO: Actually pass in and use list_type as the expected type
46+
# and don't try to infer it this way.
47+
for item in items:
48+
if not float(item).is_integer():
49+
all_ints = False
50+
break
51+
except ValueError as exc:
52+
current_app.logger.warning(
53+
f"list of values {items} are likely NOT ints or floats so we're leaving "
54+
f"them as-is. Exception: {exc}"
55+
)
56+
return items
57+
58+
if all_ints:
59+
current_app.logger.warning(
60+
f"list of values {items} could all be integers, so we are ASSUMING they "
61+
"are instead of defaulting to float."
62+
)
63+
# all can be ints, infer `int` as correct type
64+
new_items = [int(float(item)) for item in items]
65+
else:
66+
current_app.logger.warning(
67+
f"list of values {items} are NOT all integers, so we are ASSUMING they "
68+
"they are all float by default."
69+
)
70+
# default to float for backwards compatibility
71+
new_items = [float(item) for item in items]
72+
73+
return new_items
4774

4875

4976
def set_row_type(row):
@@ -207,13 +234,29 @@ def value_to_list_value(self, cls, link_name, prop, value):
207234

208235
@staticmethod
209236
def get_converted_type_from_list(cls, prop_name, value):
237+
current_app.logger.debug(f"cls.__pg_properties__:{cls.__pg_properties__}")
210238
types = cls.__pg_properties__.get(prop_name, (str,))
239+
current_app.logger.debug(f"types:{types}")
211240
value_type = types[0]
241+
242+
property_list = cls.get_property_list()
243+
current_app.logger.debug(f"property_list:{property_list}")
244+
245+
# TODO: list_type is not used b/c for some reason it's always
246+
# str even if the dictionary says it's an array of ints
247+
list_type = None
248+
if len(types) > 1:
249+
list_type = types[1]
250+
251+
current_app.logger.debug(f"prop_name:{prop_name}")
252+
current_app.logger.debug(f"value:{value}")
253+
current_app.logger.debug(f"value_type:{value_type}")
254+
212255
try:
213256
if value_type == bool:
214257
return parse_bool_from_string(value)
215258
elif value_type == list:
216-
return parse_list_from_string(value)
259+
return parse_list_from_string(value, list_type=list_type)
217260
elif value_type == float:
218261
if float(value).is_integer():
219262
return int(float(value))

0 commit comments

Comments
 (0)