From 3f492e5a3f5df31ec2c280e2950db4b1bf5be01e Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Sat, 9 Mar 2019 11:48:35 +0100 Subject: [PATCH] Add __annotations__ attribute to functions. --- tests/snippets/type_hints.py | 7 ++++++- vm/src/frame.rs | 9 ++------- vm/src/pyobject.rs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/snippets/type_hints.py b/tests/snippets/type_hints.py index 0a36e36d6f3..b6c25e59f12 100644 --- a/tests/snippets/type_hints.py +++ b/tests/snippets/type_hints.py @@ -1,7 +1,12 @@ # See also: https://github.com/RustPython/RustPython/issues/587 -def curry(foo: int, bla=2) -> float: +def curry(foo: int, bla: int =2) -> float: return foo * 3.1415926 * bla assert curry(2) > 10 + +print(curry.__annotations__) +assert curry.__annotations__['foo'] is int +assert curry.__annotations__['return'] is float +assert curry.__annotations__['bla'] is int diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 8748175a71d..f96ade968dc 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -453,7 +453,7 @@ impl Frame { let _qualified_name = self.pop_value(); let code_obj = self.pop_value(); - let _annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) { + let annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) { self.pop_value() } else { vm.new_dict() @@ -470,13 +470,8 @@ impl Frame { let scope = self.scope.clone(); let obj = vm.ctx.new_function(code_obj, scope, defaults); - let annotation_repr = vm.to_pystr(&_annotations)?; + vm.ctx.set_attr(&obj, "__annotations__", annotations); - warn!( - "Type annotation must be stored in attribute! {:?}", - annotation_repr - ); - // TODO: use annotations with set_attr here! self.push_value(obj); Ok(None) } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index c123f8bd1c2..14f7b6e7b16 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1566,7 +1566,7 @@ impl PyObject { PyObject { payload, typ: Some(typ), - dict: None, + dict: Some(RefCell::new(PyAttributes::new())), } .into_ref() }