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

Commita2bebb9

Browse files
feat: adds new input validation function similar to isinstance. (#2107)
* feat: adds new function similar to isinstance.* 🦉 Updates from OwlBot post-processorSeehttps://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md---------Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parentb35d741 commita2bebb9

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

‎google/cloud/bigquery/_helpers.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
importre
2323
importos
2424
importwarnings
25-
fromtypingimportOptional,Union
25+
fromtypingimportOptional,Union,Any,Tuple,Type
2626

2727
fromdateutilimportrelativedelta
2828
fromgoogle.cloud._helpersimportUTC# type: ignore
@@ -1004,3 +1004,33 @@ def _verify_job_config_type(job_config, expected_type, param_name="job_config"):
10041004
job_config=job_config,
10051005
)
10061006
)
1007+
1008+
1009+
def_isinstance_or_raise(
1010+
value:Any,
1011+
dtype:Union[Type,Tuple[Type, ...]],
1012+
none_allowed:Optional[bool]=False,
1013+
)->Any:
1014+
"""Determine whether a value type matches a given datatype or None.
1015+
Args:
1016+
value (Any): Value to be checked.
1017+
dtype (type): Expected data type or tuple of data types.
1018+
none_allowed Optional(bool): whether value is allowed to be None. Default
1019+
is False.
1020+
Returns:
1021+
Any: Returns the input value if the type check is successful.
1022+
Raises:
1023+
TypeError: If the input value's type does not match the expected data type(s).
1024+
"""
1025+
ifnone_allowedandvalueisNone:
1026+
returnvalue
1027+
1028+
ifisinstance(value,dtype):
1029+
returnvalue
1030+
1031+
or_none=""
1032+
ifnone_allowed:
1033+
or_none=" (or None)"
1034+
1035+
msg=f"Pass{value} as a '{dtype}'{or_none}. Got{type(value)}."
1036+
raiseTypeError(msg)

‎tests/unit/test__helpers.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
fromunittestimportmock
2525

2626
importgoogle.api_core
27+
fromgoogle.cloud.bigquery._helpersimport_isinstance_or_raise
2728

2829

2930
@pytest.mark.skipif(
@@ -1661,3 +1662,34 @@ def test_w_env_var(self):
16611662
host=self._call_fut()
16621663

16631664
self.assertEqual(host,HOST)
1665+
1666+
1667+
classTest__isinstance_or_raise:
1668+
@pytest.mark.parametrize(
1669+
"value,dtype,none_allowed,expected",
1670+
[
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"),
1677+
],
1678+
)
1679+
deftest__valid_isinstance_or_raise(self,value,dtype,none_allowed,expected):
1680+
result=_isinstance_or_raise(value,dtype,none_allowed=none_allowed)
1681+
assertresult==expected
1682+
1683+
@pytest.mark.parametrize(
1684+
"value,dtype,none_allowed,expected",
1685+
[
1686+
(None,str,False,pytest.raises(TypeError)),
1687+
({"key":"value"},str,True,pytest.raises(TypeError)),
1688+
({"key":"value"},str,False,pytest.raises(TypeError)),
1689+
({"key":"value"}, (str,float),True,pytest.raises(TypeError)),
1690+
({"key":"value"}, (str,float),False,pytest.raises(TypeError)),
1691+
],
1692+
)
1693+
deftest__invalid_isinstance_or_raise(self,value,dtype,none_allowed,expected):
1694+
withexpected:
1695+
_isinstance_or_raise(value,dtype,none_allowed=none_allowed)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp