1# Copyright 2018 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Skylib module containing functions checking types.""" 15 16# create instance singletons to avoid unnecessary allocations 17_a_bool_type = type(True) 18_a_dict_type = type({}) 19_a_list_type = type([]) 20_a_string_type = type("") 21_a_tuple_type = type(()) 22_an_int_type = type(1) 23_a_depset_type = type(depset()) 24_a_struct_type = type(struct()) 25 26def _a_function(): 27 pass 28 29_a_function_type = type(_a_function) 30 31def _is_list(v): 32 """Returns True if v is an instance of a list. 33 34 Args: 35 v: The value whose type should be checked. 36 37 Returns: 38 True if v is an instance of a list, False otherwise. 39 """ 40 return type(v) == _a_list_type 41 42def _is_string(v): 43 """Returns True if v is an instance of a string. 44 45 Args: 46 v: The value whose type should be checked. 47 48 Returns: 49 True if v is an instance of a string, False otherwise. 50 """ 51 return type(v) == _a_string_type 52 53def _is_bool(v): 54 """Returns True if v is an instance of a bool. 55 56 Args: 57 v: The value whose type should be checked. 58 59 Returns: 60 True if v is an instance of a bool, False otherwise. 61 """ 62 return type(v) == _a_bool_type 63 64def _is_none(v): 65 """Returns True if v has the type of None. 66 67 Args: 68 v: The value whose type should be checked. 69 70 Returns: 71 True if v is None, False otherwise. 72 """ 73 return type(v) == type(None) 74 75def _is_int(v): 76 """Returns True if v is an instance of a signed integer. 77 78 Args: 79 v: The value whose type should be checked. 80 81 Returns: 82 True if v is an instance of a signed integer, False otherwise. 83 """ 84 return type(v) == _an_int_type 85 86def _is_tuple(v): 87 """Returns True if v is an instance of a tuple. 88 89 Args: 90 v: The value whose type should be checked. 91 92 Returns: 93 True if v is an instance of a tuple, False otherwise. 94 """ 95 return type(v) == _a_tuple_type 96 97def _is_dict(v): 98 """Returns True if v is an instance of a dict. 99 100 Args: 101 v: The value whose type should be checked. 102 103 Returns: 104 True if v is an instance of a dict, False otherwise. 105 """ 106 return type(v) == _a_dict_type 107 108def _is_function(v): 109 """Returns True if v is an instance of a function. 110 111 Args: 112 v: The value whose type should be checked. 113 114 Returns: 115 True if v is an instance of a function, False otherwise. 116 """ 117 return type(v) == _a_function_type 118 119def _is_depset(v): 120 """Returns True if v is an instance of a `depset`. 121 122 Args: 123 v: The value whose type should be checked. 124 125 Returns: 126 True if v is an instance of a `depset`, False otherwise. 127 """ 128 return type(v) == _a_depset_type 129 130def _is_set(v): 131 """Returns True if v is a set created by sets.make(). 132 133 Args: 134 v: The value whose type should be checked. 135 136 Returns: 137 True if v was created by sets.make(), False otherwise. 138 """ 139 return type(v) == _a_struct_type and hasattr(v, "_values") and _is_dict(v._values) 140 141types = struct( 142 is_list = _is_list, 143 is_string = _is_string, 144 is_bool = _is_bool, 145 is_none = _is_none, 146 is_int = _is_int, 147 is_tuple = _is_tuple, 148 is_dict = _is_dict, 149 is_function = _is_function, 150 is_depset = _is_depset, 151 is_set = _is_set, 152) 153