mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Add __annotations__ attribute to functions.
This commit is contained in:
1 parent
56bb6d23cc
commit
3f492e5a3f
3 files changed
+9
-9
No files matched your search
@@ -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
|
||||
+2
-7
@@ -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)
|
||||
}
|
||||
|
||||
+1
-1
@@ -1566,7 +1566,7 @@ impl PyObject {
|
||||
PyObject {
|
||||
payload,
|
||||
typ: Some(typ),
|
||||
dict: None,
|
||||
dict: Some(RefCell::new(PyAttributes::new())),
|
||||
}
|
||||
.into_ref()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user