Skip to content

Commit d188484

Browse files
authored
Merge pull request #97 from dfarrow0/master
protect against failed type casts
2 parents 2be9e6c + 2cfdc5f commit d188484

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/acquisition/covidcast/csv_importer.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ def extract_and_check_row(row, geo_type):
170170

171171
if geo_type in ('hrr', 'msa', 'dma'):
172172
# these particular ids are prone to be written as ints -- and floats
173-
geo_id = str(CsvImporter.floaty_int(geo_id))
173+
try:
174+
geo_id = str(CsvImporter.floaty_int(geo_id))
175+
except ValueError:
176+
# expected a number, but got a string
177+
return (None, 'geo_id')
174178

175179
# sanity check geo_id with respect to geo_type
176180
if geo_type == 'county':
@@ -207,12 +211,20 @@ def extract_and_check_row(row, geo_type):
207211
return (None, 'val')
208212

209213
# optional nonnegative float
210-
stderr = CsvImporter.maybe_apply(float, row.se)
214+
try:
215+
stderr = CsvImporter.maybe_apply(float, row.se)
216+
except ValueError:
217+
# expected a number, but got a string
218+
return (None, 'se')
211219
if stderr is not None and stderr < 0:
212220
return (None, 'se')
213221

214222
# optional not-too-small float
215-
sample_size = CsvImporter.maybe_apply(float, row.sample_size)
223+
try:
224+
sample_size = CsvImporter.maybe_apply(float, row.sample_size)
225+
except ValueError:
226+
# expected a number, but got a string
227+
return (None, 'sample_size')
216228
if sample_size is not None and sample_size < CsvImporter.MIN_SAMPLE_SIZE:
217229
return (None, 'sample_size')
218230

tests/acquisition/covidcast/test_csv_importer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def make_row(
138138
(make_row(val=None), 'val'),
139139
(make_row(val='nan'), 'val'),
140140
(make_row(val='NaN'), 'val'),
141+
(make_row(geo_type='hrr', geo_id='hrr001'), 'geo_id'),
142+
(make_row(val='val'), 'val'),
143+
(make_row(se='se'), 'se'),
144+
(make_row(sample_size='sample_size'), 'sample_size'),
141145
]
142146

143147
for ((geo_type, row), field) in failure_cases:

0 commit comments

Comments
 (0)