Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9378af

Browse files
authored
fix:insert_rows() accepts float column values as strings again (#824)
1 parent42b66d3 commitd9378af

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

‎google/cloud/bigquery/_helpers.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importdecimal
2020
importmath
2121
importre
22+
fromtypingimportUnion
2223

2324
fromgoogle.cloud._helpersimportUTC
2425
fromgoogle.cloud._helpersimport_date_from_iso8601_date
@@ -338,14 +339,15 @@ def _int_to_json(value):
338339
returnvalue
339340

340341

341-
def_float_to_json(value):
342+
def_float_to_json(value)->Union[None,str,float]:
342343
"""Coerce 'value' to an JSON-compatible representation."""
343344
ifvalueisNone:
344345
returnNone
345-
elifmath.isnan(value)ormath.isinf(value):
346-
returnstr(value)
347-
else:
348-
returnfloat(value)
346+
347+
ifisinstance(value,str):
348+
value=float(value)
349+
350+
returnstr(value)if (math.isnan(value)ormath.isinf(value))elsefloat(value)
349351

350352

351353
def_decimal_to_json(value):

‎tests/unit/test__helpers.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,21 +690,45 @@ def _call_fut(self, value):
690690
deftest_w_none(self):
691691
self.assertEqual(self._call_fut(None),None)
692692

693+
deftest_w_non_numeric(self):
694+
withself.assertRaises(TypeError):
695+
self._call_fut(object())
696+
697+
deftest_w_integer(self):
698+
result=self._call_fut(123)
699+
self.assertIsInstance(result,float)
700+
self.assertEqual(result,123.0)
701+
693702
deftest_w_float(self):
694703
self.assertEqual(self._call_fut(1.23),1.23)
695704

705+
deftest_w_float_as_string(self):
706+
self.assertEqual(self._call_fut("1.23"),1.23)
707+
696708
deftest_w_nan(self):
697709
result=self._call_fut(float("nan"))
698710
self.assertEqual(result.lower(),"nan")
699711

712+
deftest_w_nan_as_string(self):
713+
result=self._call_fut("NaN")
714+
self.assertEqual(result.lower(),"nan")
715+
700716
deftest_w_infinity(self):
701717
result=self._call_fut(float("inf"))
702718
self.assertEqual(result.lower(),"inf")
703719

720+
deftest_w_infinity_as_string(self):
721+
result=self._call_fut("inf")
722+
self.assertEqual(result.lower(),"inf")
723+
704724
deftest_w_negative_infinity(self):
705725
result=self._call_fut(float("-inf"))
706726
self.assertEqual(result.lower(),"-inf")
707727

728+
deftest_w_negative_infinity_as_string(self):
729+
result=self._call_fut("-inf")
730+
self.assertEqual(result.lower(),"-inf")
731+
708732

709733
classTest_decimal_to_json(unittest.TestCase):
710734
def_call_fut(self,value):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp