File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1616
1717from collections import OrderedDict
1818import copy
19+ from typing import Union
1920
2021from google .cloud .bigquery .table import _parse_schema_resource
2122from google .cloud .bigquery ._helpers import _rows_from_json
@@ -119,6 +120,20 @@ def to_api_repr(self):
119120 # attributes in the API representation when needed. Here we omit them.
120121 return {"type" : self ._type }
121122
123+ def with_name (self , new_name : Union [str , None ]):
124+ """Return a copy of the instance with ``name`` set to ``new_name``.
125+
126+ Args:
127+ name (Union[str, None]):
128+ The new name of the query parameter type. If ``None``, the existing
129+ name is cleared.
130+
131+ Returns:
132+ google.cloud.bigquery.query.ScalarQueryParameterType:
133+ A new instance with updated name.
134+ """
135+ return type (self )(self ._type , name = new_name , description = self .description )
136+
122137 def __repr__ (self ):
123138 name = f", name={ self .name !r} " if self .name is not None else ""
124139 description = (
Original file line number Diff line number Diff line change @@ -98,6 +98,26 @@ def test_repr_all_optional_attrs(self):
9898 "ScalarQueryParameterType('BYTES', name='foo', description='this is foo')" ,
9999 )
100100
101+ def test_with_name_returns_copy_w_changed_name (self ):
102+ param_type = self ._make_one ("BOOLEAN" , name = None , description = "Some checkbox." )
103+ modified_type = param_type .with_name ("allow_emails" )
104+
105+ self .assertIsNot (modified_type , param_type ) # Result is a copy.
106+ self .assertEqual (modified_type .name , "allow_emails" )
107+
108+ # The rest of the The rest of the fields should have been preserved.
109+ self .assertEqual (modified_type ._type , param_type ._type )
110+ self .assertEqual (modified_type .description , param_type .description )
111+
112+ def test_with_name_clearing_the_value (self ):
113+ param_type = self ._make_one (
114+ "BOOLEAN" , name = "allow_emails" , description = "Some checkbox."
115+ )
116+ modified_type = param_type .with_name (None )
117+
118+ self .assertIsNone (modified_type .name )
119+ self .assertEqual (param_type .name , "allow_emails" ) # original unchanged
120+
101121
102122class Test_ArrayQueryParameterType (unittest .TestCase ):
103123 @staticmethod
You can’t perform that action at this time.
0 commit comments