If name_ids is list or tuple or set with multiple strings, this function generates gast.Tuple of gast.Name. If the name_ids is single string or contains only 1 string, this function returns gast.Name if gen_tuple_if_single==False else returns gast.Tuple with only one gast.Name
(name_ids, ctx=gast.Load(), gen_tuple_if_single=False)
| 236 | |
| 237 | |
| 238 | def generate_name_node(name_ids, ctx=gast.Load(), gen_tuple_if_single=False): |
| 239 | """ |
| 240 | If name_ids is list or tuple or set with multiple strings, this function |
| 241 | generates gast.Tuple of gast.Name. |
| 242 | If the name_ids is single string or contains only 1 string, this function |
| 243 | returns gast.Name if gen_tuple_if_single==False else returns gast.Tuple |
| 244 | with only one gast.Name |
| 245 | |
| 246 | This function is used at several gast.Return statements. |
| 247 | """ |
| 248 | if isinstance(name_ids, str): |
| 249 | name_ids = [name_ids] |
| 250 | if not isinstance(name_ids, (list, tuple, set)): |
| 251 | raise TypeError( |
| 252 | f'name_ids must be list or tuple or set, but received {type(name_ids)}' |
| 253 | ) |
| 254 | |
| 255 | def create_node_for_name(name): |
| 256 | if '.' not in name: |
| 257 | return gast.Name( |
| 258 | id=name, ctx=ctx, annotation=None, type_comment=None |
| 259 | ) |
| 260 | return gast.parse(name).body[0].value |
| 261 | |
| 262 | gast_names = [create_node_for_name(name_id) for name_id in name_ids] |
| 263 | if len(gast_names) == 1 and not gen_tuple_if_single: |
| 264 | name_node = gast_names[0] |
| 265 | else: |
| 266 | name_node = gast.Tuple(elts=gast_names, ctx=ctx) |
| 267 | return name_node |
| 268 | |
| 269 | |
| 270 | def get_attribute_full_name(node): |
no test coverage detected