inspect_v1-v2.diff (9444B)
1 21c21 2 < formatargspec(), formatargvalues() - format an argument spec 3 --- 4 > formatargvalues() - format an argument spec 5 34c34 6 < import ast 7 --- 8 > import abc 9 256,267c256,273 10 < co_argcount number of arguments (not including * or ** args) 11 < co_code string of raw compiled bytecode 12 < co_consts tuple of constants used in the bytecode 13 < co_filename name of file in which this code object was created 14 < co_firstlineno number of first line in Python source code 15 < co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg 16 < co_lnotab encoded mapping of line numbers to bytecode indices 17 < co_name name with which this code object was defined 18 < co_names tuple of names of local variables 19 < co_nlocals number of local variables 20 < co_stacksize virtual machine stack space required 21 < co_varnames tuple of names of arguments and local variables""" 22 --- 23 > co_argcount number of arguments (not including *, ** args 24 > or keyword only arguments) 25 > co_code string of raw compiled bytecode 26 > co_cellvars tuple of names of cell variables 27 > co_consts tuple of constants used in the bytecode 28 > co_filename name of file in which this code object was created 29 > co_firstlineno number of first line in Python source code 30 > co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg 31 > | 16=nested | 32=generator | 64=nofree | 128=coroutine 32 > | 256=iterable_coroutine | 512=async_generator 33 > co_freevars tuple of names of free variables 34 > co_kwonlyargcount number of keyword only arguments (not including ** arg) 35 > co_lnotab encoded mapping of line numbers to bytecode indices 36 > co_name name with which this code object was defined 37 > co_names tuple of names of local variables 38 > co_nlocals number of local variables 39 > co_stacksize virtual machine stack space required 40 > co_varnames tuple of names of arguments and local variables""" 41 288c294,314 42 < return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT) 43 --- 44 > if not isinstance(object, type): 45 > return False 46 > if object.__flags__ & TPFLAGS_IS_ABSTRACT: 47 > return True 48 > if not issubclass(type(object), abc.ABCMeta): 49 > return False 50 > if hasattr(object, '__abstractmethods__'): 51 > # It looks like ABCMeta.__new__ has finished running; 52 > # TPFLAGS_IS_ABSTRACT should have been accurate. 53 > return False 54 > # It looks like ABCMeta.__new__ has not finished running yet; we're 55 > # probably in __init_subclass__. We'll look for abstractmethods manually. 56 > for name, value in object.__dict__.items(): 57 > if getattr(value, "__isabstractmethod__", False): 58 > return True 59 > for base in object.__bases__: 60 > for name in getattr(base, "__abstractmethods__", ()): 61 > value = getattr(object, name, None) 62 > if getattr(value, "__isabstractmethod__", False): 63 > return True 64 > return False 65 365c391 66 < metamro = tuple([cls for cls in metamro if cls not in (type, object)]) 67 --- 68 > metamro = tuple(cls for cls in metamro if cls not in (type, object)) 69 433c459 70 < if isinstance(dict_obj, staticmethod): 71 --- 72 > if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)): 73 436c462 74 < elif isinstance(dict_obj, classmethod): 75 --- 76 > elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)): 77 481c507,510 78 < memo = {id(f)} # Memoise by id to tolerate non-hashable objects 79 --- 80 > # Memoise by id to tolerate non-hashable objects, but store objects to 81 > # ensure they aren't destroyed, which would allow their IDs to be reused. 82 > memo = {id(f): f} 83 > recursion_limit = sys.getrecursionlimit() 84 485c514 85 < if id_func in memo: 86 --- 87 > if (id_func in memo) or (len(memo) >= recursion_limit): 88 487c516 89 < memo.add(id_func) 90 --- 91 > memo[id_func] = func 92 616c645 93 < if hasattr(object, '__file__'): 94 --- 95 > if getattr(object, '__file__', None): 96 622c651 97 < if hasattr(object, '__file__'): 98 --- 99 > if getattr(object, '__file__', None): 100 635,636c664,666 101 < raise TypeError('{!r} is not a module, class, method, ' 102 < 'function, traceback, frame, or code object'.format(object)) 103 --- 104 > raise TypeError('module, class, method, function, traceback, frame, or ' 105 > 'code object was expected, got {}'.format( 106 > type(object).__name__)) 107 1184c1214,1226 108 < function to format the sequence of arguments.""" 109 --- 110 > function to format the sequence of arguments. 111 > 112 > Deprecated since Python 3.5: use the `signature` function and `Signature` 113 > objects. 114 > """ 115 > 116 > from warnings import warn 117 > 118 > warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and " 119 > "the `Signature` object directly", 120 > DeprecationWarning, 121 > stacklevel=2) 122 > 123 1353c1395 124 < raise TypeError("'{!r}' is not a Python function".format(func)) 125 --- 126 > raise TypeError("{!r} is not a Python function".format(func)) 127 1419d1460 128 < start = max(start, 1) 129 1597c1638 130 < raise TypeError("'{!r}' is not a Python generator".format(generator)) 131 --- 132 > raise TypeError("{!r} is not a Python generator".format(generator)) 133 1912a1954,1956 134 > # Lazy import ast because it's relatively heavy and 135 > # it's not used for other than this function. 136 > import ast 137 2218d2261 138 < 139 2220,2222c2263,2272 140 < new_params = (first_wrapped_param,) + tuple(sig.parameters.values()) 141 < 142 < return sig.replace(parameters=new_params) 143 --- 144 > if first_wrapped_param.kind is Parameter.VAR_POSITIONAL: 145 > # First argument of the wrapped callable is `*args`, as in 146 > # `partialmethod(lambda *args)`. 147 > return sig 148 > else: 149 > sig_params = tuple(sig.parameters.values()) 150 > assert (not sig_params or 151 > first_wrapped_param is not sig_params[0]) 152 > new_params = (first_wrapped_param,) + sig_params 153 > return sig.replace(parameters=new_params) 154 2366a2417,2426 155 > _PARAM_NAME_MAPPING = { 156 > _POSITIONAL_ONLY: 'positional-only', 157 > _POSITIONAL_OR_KEYWORD: 'positional or keyword', 158 > _VAR_POSITIONAL: 'variadic positional', 159 > _KEYWORD_ONLY: 'keyword-only', 160 > _VAR_KEYWORD: 'variadic keyword' 161 > } 162 > 163 > _get_paramkind_descr = _PARAM_NAME_MAPPING.__getitem__ 164 > 165 2401,2406c2461,2464 166 < 167 < if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD, 168 < _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD): 169 < raise ValueError("invalid value for 'Parameter.kind' attribute") 170 < self._kind = kind 171 < 172 --- 173 > try: 174 > self._kind = _ParameterKind(kind) 175 > except ValueError: 176 > raise ValueError(f'value {kind!r} is not a valid Parameter.kind') 177 2408,2409c2466,2468 178 < if kind in (_VAR_POSITIONAL, _VAR_KEYWORD): 179 < msg = '{} parameters cannot have default values'.format(kind) 180 --- 181 > if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD): 182 > msg = '{} parameters cannot have default values' 183 > msg = msg.format(_get_paramkind_descr(self._kind)) 184 2418c2477,2478 185 < raise TypeError("name must be a str, not a {!r}".format(name)) 186 --- 187 > msg = 'name must be a str, not a {}'.format(type(name).__name__) 188 > raise TypeError(msg) 189 2425,2429c2485,2488 190 < if kind != _POSITIONAL_OR_KEYWORD: 191 < raise ValueError( 192 < 'implicit arguments must be passed in as {}'.format( 193 < _POSITIONAL_OR_KEYWORD 194 < ) 195 --- 196 > if self._kind != _POSITIONAL_OR_KEYWORD: 197 > msg = ( 198 > 'implicit arguments must be passed as ' 199 > 'positional or keyword arguments, not {}' 200 2430a2490,2491 201 > msg = msg.format(_get_paramkind_descr(self._kind)) 202 > raise ValueError(msg) 203 2489c2550 204 < formatted = '{}:{}'.format(formatted, 205 --- 206 > formatted = '{}: {}'.format(formatted, 207 2493c2554,2557 208 < formatted = '{}={}'.format(formatted, repr(self._default)) 209 --- 210 > if self._annotation is not _empty: 211 > formatted = '{} = {}'.format(formatted, repr(self._default)) 212 > else: 213 > formatted = '{}={}'.format(formatted, repr(self._default)) 214 2698,2699c2762,2767 215 < msg = 'wrong parameter order: {!r} before {!r}' 216 < msg = msg.format(top_kind, kind) 217 --- 218 > msg = ( 219 > 'wrong parameter order: {} parameter before {} ' 220 > 'parameter' 221 > ) 222 > msg = msg.format(_get_paramkind_descr(top_kind), 223 > _get_paramkind_descr(kind))