def testIsInstance(self): saved = dict(preconditions._REGISTERED_CLASSES) try: # Can't parse class until it is registered. self.assertRaises(ValueError, preconditions.parse, "{BaseClass}") # Check parsed condition. preconditions.register(BaseClass) condition = preconditions.parse("{BaseClass}") self.assertIsInstance(condition, preconditions._IsInstancePrecondition) self.assertEquals(BaseClass, condition._cls) # Can't re-register a class. self.assertRaises(AssertionError, preconditions.register, BaseClass) finally: # Leave the world as we found it. preconditions._REGISTERED_CLASSES = saved
import itertools from pytype.pytd.parse import node from pytype.pytd.parse import preconditions # This mixin is used to define the classes that satisfy the {Type} # precondition. Each type class below should inherit from this mixin. # Note that the mixin must be registered with preconditions.register() below. class Type(object): __slots__ = () preconditions.register(Type) class TypeDeclUnit( node.Node('name: str or None', 'constants: tuple[Constant]', 'type_params: tuple[TypeParameter]', 'classes: tuple[Class]', 'functions: tuple[Function]', 'aliases: tuple[Alias]')): """Module node. Holds module contents (constants / classes / functions). Attributes: name: Name of this module, or None for the top-level module. constants: Iterable of module-level constants. type_params: Iterable of module-level type parameters. functions: Iterable of functions defined in this type decl unit. classes: Iterable of classes defined in this type decl unit. aliases: Iterable of aliases (or imports) for types in other modules.