mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
We already have several places in unit tests that need this, as well as a spot in an existing codemod that could benefit from it. So, implement ensure_type which can be used to refine the type of a node to an exact node type. I purposefully left the node type as 'object' instead of libcst.CSTNode so that it could be used with RemovalSentinel or MaybeSentinel without needing to import these.
29 lines
882 B
Python
29 lines
882 B
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
# pyre-strict
|
|
from typing import Type, TypeVar
|
|
|
|
import libcst.nodes as libcst
|
|
|
|
|
|
def get_fully_qualified_name(node: libcst.BaseExpression) -> str:
|
|
if isinstance(node, libcst.Name):
|
|
return node.value
|
|
elif isinstance(node, libcst.Attribute):
|
|
return get_fully_qualified_name(node.value) + "." + node.attr.value
|
|
else:
|
|
raise Exception(f"Invalid node type {type(node)}!")
|
|
|
|
|
|
_CSTNodeT = TypeVar("_CSTNodeT", bound=libcst.CSTNode)
|
|
|
|
|
|
def ensure_type(node: object, nodetype: Type[_CSTNodeT]) -> _CSTNodeT:
|
|
if not isinstance(node, nodetype):
|
|
raise Exception(
|
|
f"Expected a {nodetype.__name__} bot got a {node.__class__.__name__}!"
|
|
)
|
|
return node
|