Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/libcst/helpers/common.py: 83%
6 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 Type
8from libcst._types import CSTNodeT
11def ensure_type(node: object, nodetype: Type[CSTNodeT]) -> CSTNodeT:
12 """
13 Takes any python object, and a LibCST :class:`~libcst.CSTNode` subclass and
14 refines the type of the python object. This is most useful when you already
15 know that a particular object is a certain type but your type checker is not
16 convinced. Note that this does an instance check for you and raises an
17 exception if it is not the right type, so this should be used in situations
18 where you are sure of the type given previous checks.
19 """
21 if not isinstance(node, nodetype):
22 raise Exception(
23 f"Expected a {nodetype.__name__} but got a {node.__class__.__name__}!"
24 )
25 return node