Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/libcst/helpers/expression.py: 17%
23 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:43 +0000
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5#
6from typing import Optional, Union
8import libcst as cst
11def get_full_name_for_node(node: Union[str, cst.CSTNode]) -> Optional[str]:
12 """Return a dot concatenated full name for str, :class:`~libcst.Name`, :class:`~libcst.Attribute`.
13 :class:`~libcst.Call`, :class:`~libcst.Subscript`, :class:`~libcst.FunctionDef`, :class:`~libcst.ClassDef`,
14 :class:`~libcst.Decorator`.
15 Return ``None`` for not supported Node.
16 """
17 if isinstance(node, cst.Name):
18 return node.value
19 elif isinstance(node, str):
20 return node
21 elif isinstance(node, cst.Attribute):
22 return f"{get_full_name_for_node(node.value)}.{node.attr.value}"
23 elif isinstance(node, cst.Call):
24 return get_full_name_for_node(node.func)
25 elif isinstance(node, cst.Subscript):
26 return get_full_name_for_node(node.value)
27 elif isinstance(node, (cst.FunctionDef, cst.ClassDef)):
28 return get_full_name_for_node(node.name)
29 elif isinstance(node, cst.Decorator):
30 return get_full_name_for_node(node.decorator)
31 return None
34def get_full_name_for_node_or_raise(node: Union[str, cst.CSTNode]) -> str:
35 """Return a dot concatenated full name for str, :class:`~libcst.Name`, :class:`~libcst.Attribute`.
36 :class:`~libcst.Call`, :class:`~libcst.Subscript`, :class:`~libcst.FunctionDef`, :class:`~libcst.ClassDef`.
37 Raise Exception for not supported Node.
38 """
39 full_name = get_full_name_for_node(node)
40 if full_name is None:
41 raise Exception(f"Not able to parse full name for: {node}")
42 return full_name