我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用collections.abc.Callable()。
def test_Callable(self): non_samples = [None, 42, 3.14, 1j, "", b"", (), [], {}, set(), (lambda: (yield))(), (x for x in []), ] for x in non_samples: self.assertNotIsInstance(x, Callable) self.assertFalse(issubclass(type(x), Callable), repr(type(x))) samples = [lambda: None, type, int, object, len, list.append, [].append, ] for x in samples: self.assertIsInstance(x, Callable) self.assertTrue(issubclass(type(x), Callable), repr(type(x))) self.validate_abstract_methods(Callable, '__call__') self.validate_isinstance(Callable, '__call__')
def __new__(cls, name, bases, namespace, _root=False, args=None, result=None): if args is None and result is None: pass # Must be 'class Callable'. else: if args is not Ellipsis: if not isinstance(args, list): raise TypeError("Callable[args, result]: " "args must be a list." " Got %.100r." % (args,)) msg = "Callable[[arg, ...], result]: each arg must be a type." args = tuple(_type_check(arg, msg) for arg in args) msg = "Callable[args, result]: result must be a type." result = _type_check(result, msg) self = super().__new__(cls, name, bases, namespace, _root=_root) self.__args__ = args self.__result__ = result return self
def __init__(self, name, jump_strategy): assert(isinstance(jump_strategy, Callable)) self.name = name self.jump = jump_strategy
def test_direct_subclassing(self): for B in Hashable, Iterable, Iterator, Sized, Container, Callable: class C(B): pass self.assertTrue(issubclass(C, B)) self.assertFalse(issubclass(int, C))
def test_registration(self): for B in Hashable, Iterable, Iterator, Sized, Container, Callable: class C: __hash__ = None # Make sure it isn't hashable by default self.assertFalse(issubclass(C, B), B.__name__) B.register(C) self.assertTrue(issubclass(C, B))
def __getitem__(self, parameters): if self.__args__ is not None or self.__result__ is not None: raise TypeError("This Callable type is already parameterized.") if not isinstance(parameters, tuple) or len(parameters) != 2: raise TypeError( "Callable must be used as Callable[[arg, ...], result].") args, result = parameters return self.__class__(self.__name__, self.__bases__, dict(self.__dict__), _root=True, args=args, result=result)
def __instancecheck__(self, obj): # For unparametrized Callable we allow this, because # typing.Callable should be equivalent to # collections.abc.Callable. if self.__args__ is None and self.__result__ is None: return isinstance(obj, collections_abc.Callable) else: raise TypeError("Callable[] cannot be used with isinstance().")
def __new__(cls, name, bases, namespace, parameters=None, origin=None, extra=None): if parameters is None: # Extract parameters from direct base classes. Only # direct bases are considered and only those that are # themselves generic, and parameterized with type # variables. Don't use bases like Any, Union, Tuple, # Callable or type variables. params = None for base in bases: if isinstance(base, TypingMeta): if not isinstance(base, GenericMeta): raise TypeError( "You cannot inherit from magic class %s" % repr(base)) if base.__parameters__ is None: continue # The base is unparameterized. for bp in base.__parameters__: if _has_type_var(bp) and not isinstance(bp, TypeVar): raise TypeError( "Cannot inherit from a generic class " "parameterized with " "non-type-variable %s" % bp) if params is None: params = [] if bp not in params: params.append(bp) if params is not None: parameters = tuple(params) self = super().__new__(cls, name, bases, namespace, _root=True) self.__parameters__ = parameters if extra is not None: self.__extra__ = extra # Else __extra__ is inherited, eventually from the # (meta-)class default above. self.__origin__ = origin return self