def _instantiate_type(self, node, args, type_var): cls = type_var.data[0] try: return self.vm.annotations_util.init_annotation(node, cls, "attr.ib", self.vm.frames) except self.vm.annotations_util.LateAnnotationError: return abstract.LateAnnotation(cls, "attr.ib", self.vm.simple_stack())
def test_signature_annotations_existence(self): # def f(v: "X") -> "Y" sig = function.Signature( name="f", param_names=("v",), varargs_name=None, kwonly_params=(), kwargs_name=None, defaults={}, annotations={}, late_annotations={ "v": abstract.LateAnnotation("X", "v", None), "return": abstract.LateAnnotation("Y", "return", None) } ) self.assertEqual(repr(sig), "def f(v: 'X') -> 'Y'") self.assertFalse(sig.has_param_annotations) self.assertFalse(sig.has_return_annotation) sig.set_annotation("v", self._vm.convert.unsolvable) self.assertTrue(sig.has_param_annotations) self.assertFalse(sig.has_return_annotation) sig.set_annotation("return", self._vm.convert.unsolvable) self.assertTrue(sig.has_param_annotations) self.assertTrue(sig.has_return_annotation)
def convert_annotations_list(self, annotations_list): """Convert a (name, raw_annot) list to annotations and late annotations.""" annotations = {} late_annotations = {} for name, t in annotations_list: if t is None: continue try: annot = self._process_one_annotation(t, name, self.vm.frames) except self.LateAnnotationError: late_annotations[name] = abstract.LateAnnotation( t, name, self.vm.simple_stack()) else: if annot is not None: annotations[name] = annot return annotations, late_annotations
def init_annotation_var(self, node, name, var): """Convert annotation type to instance value.""" try: typ = abstract_utils.get_atomic_value(var) except abstract_utils.ConversionError: error = "Type must be constant for variable annotation" self.vm.errorlog.invalid_annotation(self.vm.frames, None, error, name) return self.vm.new_unsolvable(node) else: if self.get_type_parameters(typ): self.vm.errorlog.not_supported_yet( self.vm.frames, "using type parameter in variable annotation") return self.vm.new_unsolvable(node) try: return self.init_annotation(typ, name, self.vm.frames, node) except self.LateAnnotationError: return abstract.LateAnnotation(typ, name, self.vm.simple_stack())
def apply_type_comment(self, state, op, name, value): """If there is a type comment for the op, return its value.""" assert op is self.vm.frame.current_opcode if op.code.co_filename != self.vm.filename: return value if not op.type_comment: return value comment = op.type_comment try: frame = self.vm.frame var = abstract_utils.eval_expr(self.vm, state.node, frame.f_globals, frame.f_locals, comment) except abstract_utils.EvaluationError as e: self.vm.errorlog.invalid_type_comment(self.vm.frames, comment, details=utils.message(e)) value = self.vm.new_unsolvable(state.node) else: try: typ = abstract_utils.get_atomic_value(var) except abstract_utils.ConversionError: self.vm.errorlog.invalid_type_comment( self.vm.frames, comment, details="Must be constant.") value = self.vm.new_unsolvable(state.node) else: if self.get_type_parameters(typ): self.vm.errorlog.not_supported_yet( self.vm.frames, "using type parameter in type comment") try: value = self.init_annotation(typ, name, self.vm.frames, state.node) except self.LateAnnotationError: value = abstract.LateAnnotation(typ, name, self.vm.simple_stack()) return value