Skip to content

Commit 43dc45e

Browse files
committed
updates logic and tests related to _isinstance_or_raise'
1 parent d7698d2 commit 43dc45e

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

google/cloud/bigquery/_helpers.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import re
2424
import os
2525
import warnings
26-
from typing import Optional, Union, Any, Tuple
26+
from typing import Optional, Union, Any, Tuple, Type
2727

2828
from dateutil import relativedelta
2929
from google.cloud._helpers import UTC # type: ignore
@@ -1031,13 +1031,18 @@ def from_api_repr(cls, resource: dict):
10311031
return config
10321032

10331033

1034-
def _isinstance_or_raise(value: Any, dtype: type, none_allowed: Optional[bool]):
1034+
def _isinstance_or_raise(
1035+
value: Any,
1036+
dtype: Union[Type, Tuple[Type, ...]],
1037+
none_allowed: Optional[bool]=False,
1038+
) -> Any:
10351039
"""Determine whether a value type matches a given datatype or None.
10361040
10371041
Args:
10381042
value (Any): Value to be checked.
1039-
dtype (type): Expected data type(s).
1040-
none_allowed Optional(bool): whether value is allowed to be None.
1043+
dtype (type): Expected data type or tuple of data types.
1044+
none_allowed Optional(bool): whether value is allowed to be None. Default
1045+
is False.
10411046
10421047
Returns:
10431048
Any: Returns the input value if the type check is successful.
@@ -1051,5 +1056,9 @@ def _isinstance_or_raise(value: Any, dtype: type, none_allowed: Optional[bool]):
10511056
if isinstance(value, dtype):
10521057
return value
10531058

1054-
msg = f"Pass {value} as a '{dtype}' (or None). Got {type(value)}." # Add the 'or None' conditionally
1059+
or_none = ''
1060+
if none_allowed:
1061+
or_none = ' (or None)'
1062+
1063+
msg = f"Pass {value} as a '{dtype}'{or_none}. Got {type(value)}."
10551064
raise TypeError(msg)

tests/unit/test__helpers.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,35 +1666,34 @@ def test_w_env_var(self):
16661666

16671667
class Test__isinstance_or_raise:
16681668
@pytest.mark.parametrize(
1669-
"value,dtype,expected",
1669+
"value,dtype,none_allowed,expected",
16701670
[
1671-
(None, None, None),
1672-
('hello world.uri', str, 'hello world.uri'),
1673-
(None, (str, None), None),
1674-
(None, (None, str), None),
1675-
(None, (str, None), None),
1676-
('hello world.uri', (None, str), 'hello world.uri'),
1677-
('hello world.uri', (str, None), 'hello world.uri'),
1671+
(None, str, True, None),
1672+
('hello world.uri', str, True, 'hello world.uri'),
1673+
('hello world.uri', str, False, 'hello world.uri'),
1674+
(None, (str, float), True, None),
1675+
('hello world.uri', (str, float), True, 'hello world.uri'),
1676+
('hello world.uri', (str, float), False, 'hello world.uri'),
16781677
],
16791678
)
1680-
def test__valid_isinstance_or_raise(self, value, dtype, expected):
1681-
result = _isinstance_or_raise(value, dtype)
1679+
def test__valid_isinstance_or_raise(self, value, dtype, none_allowed, expected):
1680+
result = _isinstance_or_raise(value, dtype, none_allowed=none_allowed)
16821681

16831682
assert result == expected
16841683

16851684
@pytest.mark.parametrize(
1686-
"value,dtype,expected",
1685+
"value,dtype,none_allowed,expected",
16871686
[
1688-
(None, str, pytest.raises(TypeError)),
1689-
({"key": "value"}, str, pytest.raises(TypeError)),
1690-
({"key": "value"}, None, pytest.raises(TypeError)),
1691-
({"key": "value"}, (str, None), pytest.raises(TypeError)),
1692-
({"key": "value"}, (None, str), pytest.raises(TypeError)),
1687+
(None, str, False, pytest.raises(TypeError)),
1688+
({"key": "value"}, str, True, pytest.raises(TypeError)),
1689+
({"key": "value"}, str, False, pytest.raises(TypeError)),
1690+
({"key": "value"}, (str, float), True, pytest.raises(TypeError)),
1691+
({"key": "value"}, (str, float), False, pytest.raises(TypeError)),
16931692
],
16941693
)
1695-
def test__invalid_isinstance_or_raise(self, value, dtype, expected):
1694+
def test__invalid_isinstance_or_raise(self, value, dtype, none_allowed, expected):
16961695
with expected as e:
1697-
result = _isinstance_or_raise(value, dtype)
1696+
result = _isinstance_or_raise(value, dtype, none_allowed=none_allowed)
16981697

16991698
assert result == e
17001699

0 commit comments

Comments
 (0)