Python inspect._empty() Examples
The following are 30
code examples of inspect._empty().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
inspect
, or try the search function
.
Example #1
Source File: internals.py From Anki-Night-Mode with GNU General Public License v3.0 | 6 votes |
def move_args_to_kwargs(original_function, args, kwargs): args = list(args) import inspect signature = inspect.signature(original_function) i = 0 for name, parameter in signature.parameters.items(): if i >= len(args): break if parameter.default is not inspect._empty: value = args.pop(i) kwargs[name] = value else: i += 1 return args, kwargs
Example #2
Source File: func_details.py From agents-aea with Apache License 2.0 | 6 votes |
def check(self) -> None: """ Check for docstring and arguments have default values set. Raises exception if function definition does not contain docstring or default values. :return: None """ if not self.doc: raise ValueError("Function docstring is missing") if super()._arguments[0].name != self.CONTROL_ARG_NAME: raise ValueError( f"first function argument must be named `{self.CONTROL_ARG_NAME}`!" ) for arg in self._arguments: if arg.default == inspect._empty: # type: ignore # pylint: disable=protected-access raise ValueError( "function should have default values for every param except first one" )
Example #3
Source File: peakfinder2D_gui.py From pyxem with GNU General Public License v3.0 | 6 votes |
def __init__(self): self.signal = None self.indices = None self.methods = METHODS self.method_names = [method.__name__ for method in METHODS] self.params = { method.__name__: OrderedDict( [ (p.name, p.default) for p in inspect.signature(method).parameters.values() if p.default is not inspect._empty ] ) for method in METHODS } self._method = self.method_names[0]
Example #4
Source File: verify.py From pop with Apache License 2.0 | 6 votes |
def sig_map(ver): ''' Generates the map dict for the signature verification ''' vsig = inspect.signature(ver) vparams = list(vsig.parameters.values()) vdat = {'args': [], 'v_pos': -1, 'kw': [], 'kwargs': False, 'ann': {}} for ind in range(len(vparams)): param = vparams[ind] val = param.kind.value name = param.name if val == 0 or val == 1: vdat['args'].append(name) if param.default != inspect._empty: # Is a KW, can be inside of **kwargs vdat['kw'].append(name) elif val == 2: vdat['v_pos'] = ind elif val == 3: vdat['kw'].append(name) elif val == 4: vdat['kwargs'] = ind if param.annotation != inspect._empty: vdat['ann'][name] = param.annotation return vdat
Example #5
Source File: variabilityops.py From anvio with GNU General Public License v3.0 | 6 votes |
def is_passed_kwargs_compatible_with_passed_function(self, kwargs): params_inspection = inspect.signature(self.passed_function).parameters for param in params_inspection: has_default = True if params_inspection[param].default is not inspect._empty else False if not has_default and param not in kwargs.keys(): raise ConfigError("`%s` was passed to filter_data. All its arguments without defaults must " "also be passed, but `%s` was not. Do so with \"%s = ...\"" \ % (self.passed_function, param, param)) else: self.append_info_log("`%s` of `%s` was passed or has default" % (param, self.passed_function), True) bad_args = [arg for arg in kwargs.keys() if arg not in params_inspection] if bad_args: raise ConfigError("VariabilityFilter :: The args [%s] were passed to filter_data,\ but are not args of `%s`. Available args for this function are [%s]" \ % (", ".join(bad_args), self.passed_function, ", ".join(params_inspection))) for kwarg in kwargs: self.append_info_log("filter argument: `%s`" % (kwarg), "valid argument for `%s`" % (self.passed_function))
Example #6
Source File: test_loaders.py From mirdata with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_load_methods(): for dataset in DATASETS: dataset_name = dataset.__name__.split('.')[1] all_methods = dir(dataset) load_methods = [ getattr(dataset, m) for m in all_methods if m.startswith('load_') ] for load_method in load_methods: method_name = load_method.__name__ params = [ p for p in signature(load_method).parameters.values() if p.default == inspect._empty ] # get list of parameters that don't have defaults # add to the EXCEPTIONS dictionary above if your load_* function needs # more than one argument. if dataset_name in EXCEPTIONS and method_name in EXCEPTIONS[dataset_name]: extra_params = EXCEPTIONS[dataset_name][method_name] with pytest.raises(IOError): load_method("a/fake/filepath", **extra_params) else: with pytest.raises(IOError): load_method("a/fake/filepath")
Example #7
Source File: tracers.py From hyperparameter_hunter with MIT License | 6 votes |
def __new__(mcs, name, bases, namespace, **kwargs): class_obj = super().__new__(mcs, name, bases, dict(namespace)) all_args, all_kwargs = [], {} signature_parameters = signature(class_obj.__init__).parameters for k, v in signature_parameters.items(): if k not in ["self", "args", "kwargs"]: # FLAG: Might need kwargs to ok "input_dim" if (v.kind in [v.KEYWORD_ONLY, v.POSITIONAL_OR_KEYWORD]) and v.default != _empty: all_kwargs[k] = v.default else: all_args.append(k) setattr(class_obj, "__hh_default_args", all_args) setattr(class_obj, "__hh_default_kwargs", all_kwargs) return class_obj
Example #8
Source File: misc.py From napari with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __str__(self): """do not render separators commented code is what was taken out from the copy/pasted inspect module code :) """ result = [] # render_pos_only_separator = False # render_kw_only_separator = True for param in self.parameters.values(): formatted = str(param) result.append(formatted) rendered = '({})'.format(', '.join(result)) if self.return_annotation is not inspect._empty: anno = inspect.formatannotation(self.return_annotation) rendered += ' -> {}'.format(anno) return rendered
Example #9
Source File: misc.py From napari with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __str__(self): """wrap defaults""" kind = self.kind formatted = self._name # Fill in defaults if ( self._default is not inspect._empty or kind == inspect._KEYWORD_ONLY ): formatted = '{}={}'.format(formatted, formatted) if kind == inspect._VAR_POSITIONAL: formatted = '*' + formatted elif kind == inspect._VAR_KEYWORD: formatted = '**' + formatted return formatted
Example #10
Source File: adapt.py From cornerwise with MIT License | 6 votes |
def adapt_arg(arg, ann, deserializers=DESERIALIZERS): if not ann or ann is inspect._empty: return arg if ann.__module__ == "typing": if typing.Iterable in ann.__mro__: return adapt_iterable(arg, ann, deserializers) elif typing.Tuple in ann.__mro__: return adapt_tuple(arg, ann, deserializers) if isinstance(arg, ann): return arg deserializer = get_deserializer(ann, deserializers) if deserializer: adapted = deserializer(arg, ann) if adapted: return adapted return arg
Example #11
Source File: object.py From pygraphy with MIT License | 6 votes |
def validate(cls): if cls.__validated__: return cls.__validated__ = True for _, field in cls.__fields__.items(): if not isinstance(field, (Field, ResolverField)): raise ValidationError(f'{field} is an invalid field type') if field.ftype == _empty: raise ValidationError(f'The return type of resolver "{cls.__name__}.{field.name}" must not be empty') print_type(field.ftype, except_types=(types.InputType)) if isinstance(field, ResolverField): for gtype in field.params.values(): print_type(gtype) shelled = shelling_type(gtype) if isinstance(shelled, types.InputType): shelled.validate() if isinstance(field.ftype, ObjectType): field.ftype.validate() if is_union(field.ftype): shelled = shelling_type(field.ftype) if isinstance(shelled, ObjectType): shelled.validate()
Example #12
Source File: conf.py From xclim with Apache License 2.0 | 6 votes |
def _indicator_table(realm): """Return a sequence of dicts storing metadata about all available indices.""" import inspect inds = _get_indicators(getattr(xclim, realm)) table = {} for indname, ind in inds.items(): # Apply default values args = { name: p.default if p.default != inspect._empty else f"<{name}>" for (name, p) in ind._sig.parameters.items() } try: table[indname] = ind.json(args) except KeyError as err: print(f"{ind.identifier} could not be documented.({err})") else: table[indname]["function"] = f"xclim.indices.{ind.compute.__name__}" return table
Example #13
Source File: caching.py From easypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _apply_defaults(bound_arguments): """ Set default values for missing arguments. (from Python3.5) """ from collections import OrderedDict from inspect import _empty, _VAR_POSITIONAL, _VAR_KEYWORD arguments = bound_arguments.arguments new_arguments = [] for name, param in bound_arguments._signature.parameters.items(): try: new_arguments.append((name, arguments[name])) except KeyError: if param.default is not _empty: val = param.default elif param.kind is _VAR_POSITIONAL: val = () elif param.kind is _VAR_KEYWORD: val = {} else: # This BoundArguments was likely produced by # Signature.bind_partial(). continue new_arguments.append((name, val)) bound_arguments.arguments = OrderedDict(new_arguments)
Example #14
Source File: base.py From briefcase with BSD 3-Clause "New" or "Revised" License | 6 votes |
def create_config(klass, config, msg): try: return klass(**config) except TypeError: # Inspect the GlobalConfig constructor to find which # parameters are required and don't have a default # value. required_args = { name for name, param in inspect.signature(klass.__init__).parameters.items() if param.default == inspect._empty and name not in {'self', 'kwargs'} } missing_args = required_args - config.keys() missing = ', '.join( "'{arg}'".format(arg=arg) for arg in sorted(missing_args) ) raise BriefcaseConfigError( "{msg} is incomplete (missing {missing})".format( msg=msg, missing=missing ) )
Example #15
Source File: compat.py From easy_cache with MIT License | 6 votes |
def getargspec(func): signature = inspect.signature(func) args = [] varargs = None keywords = None defaults = [] for param in signature.parameters.values(): # type: Parameter if param.kind == Parameter.VAR_POSITIONAL: varargs = param.name elif param.kind in ( Parameter.POSITIONAL_ONLY, Parameter.KEYWORD_ONLY, Parameter.POSITIONAL_OR_KEYWORD): args.append(param.name) elif param.kind == Parameter.VAR_KEYWORD: keywords = param.name # noinspection PyProtectedMember if param.default is not inspect._empty: defaults.append(param.default) return ArgSpec(args, varargs, keywords, tuple(defaults))
Example #16
Source File: run.py From pysystemtrade with GNU General Public License v3.0 | 5 votes |
def parameter_type(parameter_signature): ptype = parameter_signature.annotation if ptype is inspect._empty: # get from default if has_default(parameter_signature): default_value = parameter_default(parameter_signature) ptype = type(default_value) else: # give up return NO_TYPE_PROVIDED return ptype.__name__
Example #17
Source File: field.py From pygraphy with MIT License | 5 votes |
def print_default_value(self, name): default = self._params[name].default return f' = {json.dumps(default)}' if default != inspect._empty else ''
Example #18
Source File: utils.py From clashroyale with MIT License | 5 votes |
def typecasted(func): """Decorator that converts arguments via annotations.""" signature = inspect.signature(func).parameters.items() @wraps(func) def wrapper(*args, **kwargs): args = list(args) new_args = [] new_kwargs = {} for _, param in signature: converter = param.annotation if converter is inspect._empty: converter = lambda a: a # do nothing if param.kind is param.POSITIONAL_OR_KEYWORD: if args: to_conv = args.pop(0) new_args.append(converter(to_conv)) elif param.kind is param.VAR_POSITIONAL: for a in args: new_args.append(converter(a)) else: for k, v in kwargs.items(): nk, nv = converter(k, v) new_kwargs[nk] = nv return func(*new_args, **new_kwargs) return wrapper
Example #19
Source File: introspection.py From pygraphy with MIT License | 5 votes |
def default_value(self) -> Optional[str]: # Do not support default value of input type field if not hasattr(self, '_param'): return None default = self._param.default return json.dumps(default) if default != inspect._empty else None
Example #20
Source File: run.py From pysystemtrade with GNU General Public License v3.0 | 5 votes |
def parameter_default(parameter_signature): default = parameter_signature.default if default is inspect._empty: default = NO_DEFAULT return default
Example #21
Source File: decorators.py From aiocache with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_args_dict(func, args, kwargs): defaults = { arg_name: arg.default for arg_name, arg in inspect.signature(func).parameters.items() if arg.default is not inspect._empty # TODO: bug prone.. } args_names = func.__code__.co_varnames[: func.__code__.co_argcount] return {**defaults, **dict(zip(args_names, args)), **kwargs}
Example #22
Source File: keras_helper.py From hyperparameter_hunter with MIT License | 5 votes |
def parameters_by_signature(instance, signature_filter=None): """Get a dict of the parameters used to create an instance of a class. This is only suited for classes whose attributes are named according to their input parameters Parameters ---------- instance: Class instance Instance of a class that has attributes named for the class's input parameters signature_filter: Callable, or None, default=None If not None, should be callable that expects as input (<arg_name>, <arg_val>), which are signature parameter names, and values, respectively. The callable should return a boolean: True if the pair should be added to `params`, or False if it should be ignored. If `signature_filter` is None, all signature parameters will be added to `params` Returns ------- params: Dict Mapping of input parameters in class's `__init__` signature to instance attribute values""" signature_filter = signature_filter or (lambda _name, _val: True) params = dict() signature_args = sorted(signature(instance.__class__).parameters.items()) for arg_name, arg_val in signature_args: if signature_filter(arg_name, arg_val): try: temp_val = getattr(instance, arg_name) params[arg_name] = temp_val if temp_val is not _empty else None except AttributeError: params[arg_name] = arg_val.default if arg_val.default is not _empty else None return params ################################################## # Keras Model Parametrization ##################################################
Example #23
Source File: general.py From marvin with BSD 3-Clause "New" or "Revised" License | 5 votes |
def use_inspect(func): ''' Inspect a function of arguments and keywords. Inspects a function or class method. Uses a different inspect for Python 2 vs 3 Only tested to work with args and defaults. varargs (variable arguments) and varkw (keyword arguments) seem to always be empty. Parameters: func (func): The function or method to inspect Returns: A tuple of arguments, variable arguments, keywords, and default values ''' pyver = sys.version_info.major if pyver == 2: args, varargs, varkw, defaults = inspect.getargspec(func) elif pyver == 3: sig = inspect.signature(func) args = [] defaults = [] varargs = varkw = None for par in sig.parameters.values(): # most parameters seem to be of this kind if par.kind == par.POSITIONAL_OR_KEYWORD: args.append(par.name) # parameters with default of inspect empty are required if par.default != inspect._empty: defaults.append(par.default) return args, varargs, varkw, defaults
Example #24
Source File: utils.py From brawlstats with MIT License | 5 votes |
def typecasted(func): """Decorator that converts arguments via annotations. Source: https://github.com/cgrok/clashroyale/blob/master/clashroyale/official_api/utils.py#L11""" signature = inspect.signature(func).parameters.items() @wraps(func) def wrapper(*args, **kwargs): args = list(args) new_args = [] new_kwargs = {} for _, param in signature: converter = param.annotation if converter is inspect._empty: converter = lambda a: a # do nothing if param.kind is param.POSITIONAL_OR_KEYWORD: if args: to_conv = args.pop(0) new_args.append(converter(to_conv)) elif param.kind is param.VAR_POSITIONAL: for a in args: new_args.append(converter(a)) else: for k, v in kwargs.items(): nk, nv = converter(k, v) new_kwargs[nk] = nv return func(*new_args, **new_kwargs) return wrapper
Example #25
Source File: utils.py From pggan-pytorch with MIT License | 5 votes |
def create_params(classes, excludes=None, overrides=None): params = {} if not excludes: excludes = {} if not overrides: overrides = {} for cls in classes: nm = cls.__name__ params[nm] = { k: (v.default if nm not in overrides or k not in overrides[nm] else overrides[nm][k]) for k, v in dict(inspect.signature(cls.__init__).parameters).items() if v.default != inspect._empty and (nm not in excludes or k not in excludes[nm]) } return params
Example #26
Source File: test_decorators.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_wraps(): """ Tests the compatibility replacement for functools.wraps which supports argument preservation across all supported Python versions. """ def foo(a, b, c=1, d=2, e=3, **kwargs): """A test function.""" return a, b, c, d, e, kwargs @wraps(foo) def bar(*args, **kwargs): return ('test',) + foo(*args, **kwargs) expected = ('test', 1, 2, 3, 4, 5, {'f': 6, 'g': 7}) assert bar(1, 2, 3, 4, 5, f=6, g=7) == expected assert bar.__name__ == 'foo' if foo.__doc__ is not None: # May happen if using optimized opcode assert bar.__doc__ == "A test function." if hasattr(foo, '__qualname__'): assert bar.__qualname__ == foo.__qualname__ sig = inspect.signature(bar) assert list(sig.parameters) == ['a', 'b', 'c', 'd', 'e', 'kwargs'] defaults = [inspect._empty, inspect._empty, 1, 2, 3, inspect._empty] assert [p.default for p in sig.parameters.values()] == defaults
Example #27
Source File: common.py From pycoQC with GNU General Public License v3.0 | 5 votes |
def make_arg_dict (func): """Parse the arguments default value, type and doc""" # Init method for classes if inspect.isclass(func): func = func.__init__ if inspect.isfunction(func) or inspect.ismethod(func): # Parse arguments default values and annotations d = OrderedDict() for name, p in inspect.signature(func).parameters.items(): if p.name not in ["self","cls"]: # Object stuff. Does not make sense to include in doc d[name] = OrderedDict() if name not in ["kwargs","args"]: # Include but skip default required and type # Get Annotation if p.annotation != inspect._empty: d[name]["type"] = p.annotation # Get default value if available if p.default == inspect._empty: d[name]["required"] = True else: d[name]["default"] = p.default # Parse the docstring in a dict docstr_dict = OrderedDict() lab=None for l in inspect.getdoc(func).split("\n"): l = l.strip() if l: if l.startswith("*"): lab = l[1:].strip() docstr_dict[lab] = [] elif lab: docstr_dict[lab].append(l) # Concatenate and copy doc in main dict for name in d.keys(): if name in docstr_dict: d[name]["help"] = " ".join(docstr_dict[name]) return d
Example #28
Source File: utils.py From clashroyale with MIT License | 5 votes |
def typecasted(func): """Decorator that converts arguments via annotations.""" signature = inspect.signature(func).parameters.items() @wraps(func) def wrapper(*args, **kwargs): args = list(args) new_args = [] new_kwargs = {} for _, param in signature: converter = param.annotation if converter is inspect._empty: converter = lambda a: a # do nothing if param.kind is param.POSITIONAL_OR_KEYWORD: if args: to_conv = args.pop(0) new_args.append(converter(to_conv)) elif param.kind is param.VAR_POSITIONAL: for a in args: new_args.append(converter(a)) else: for k, v in kwargs.items(): nk, nv = converter(k, v) new_kwargs[nk] = nv return func(*new_args, **new_kwargs) return wrapper
Example #29
Source File: UtilitiesTest.py From coala-quickstart with GNU Affero General Public License v3.0 | 5 votes |
def test_get_all_args(self): empty = inspect._empty self.assertEqual(get_all_args(AllKindsOfSettingsDependentBear.run), {'self': empty, 'file': empty, 'filename': empty, 'configs': empty, 'use_bears': empty, 'no_lines': empty, 'use_spaces': None, 'use_tabs': False, 'max_line_lengths': 1000, 'no_chars': 79, 'chars': False, 'dependency_results': {}})
Example #30
Source File: map.py From mars with Apache License 2.0 | 5 votes |
def __call__(self, series, dtype): if dtype is None: inferred_dtype = None if callable(self._arg): # arg is a function, try to inspect the signature sig = inspect.signature(self._arg) return_type = sig.return_annotation if return_type is not inspect._empty: inferred_dtype = np.dtype(return_type) else: if isinstance(self._arg, MutableMapping): inferred_dtype = pd.Series(self._arg).dtype else: inferred_dtype = self._arg.dtype if inferred_dtype is not None and np.issubdtype(inferred_dtype, np.number): if np.issubdtype(inferred_dtype, np.inexact): # for the inexact e.g. float # we can make the decision, # but for int, due to the nan which may occur, # we cannot infer the dtype dtype = inferred_dtype else: dtype = inferred_dtype if dtype is None: raise ValueError('cannot infer dtype, ' 'it needs to be specified manually for `map`') else: dtype = np.int64 if dtype is int else dtype dtype = np.dtype(dtype) inputs = [series] if isinstance(self._arg, SERIES_TYPE): inputs.append(self._arg) return self.new_series(inputs, shape=series.shape, dtype=dtype, index_value=series.index_value, name=series.name)