Skip to content

Commit 1444a1f

Browse files
Merge pull request RustPython#640 from RustPython/type_annotations2
Add __annotations__ attribute to functions.
2 parents fae776d + 3f492e5 commit 1444a1f

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

tests/snippets/type_hints.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11

22
# See also: https://github.com/RustPython/RustPython/issues/587
33

4-
def curry(foo: int, bla=2) -> float:
4+
def curry(foo: int, bla: int =2) -> float:
55
return foo * 3.1415926 * bla
66

77
assert curry(2) > 10
8+
9+
print(curry.__annotations__)
10+
assert curry.__annotations__['foo'] is int
11+
assert curry.__annotations__['return'] is float
12+
assert curry.__annotations__['bla'] is int

vm/src/frame.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl Frame {
440440
let _qualified_name = self.pop_value();
441441
let code_obj = self.pop_value();
442442

443-
let _annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) {
443+
let annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) {
444444
self.pop_value()
445445
} else {
446446
vm.new_dict()
@@ -457,13 +457,8 @@ impl Frame {
457457
let scope = self.scope.clone();
458458
let obj = vm.ctx.new_function(code_obj, scope, defaults);
459459

460-
let annotation_repr = vm.to_pystr(&_annotations)?;
460+
vm.ctx.set_attr(&obj, "__annotations__", annotations);
461461

462-
warn!(
463-
"Type annotation must be stored in attribute! {:?}",
464-
annotation_repr
465-
);
466-
// TODO: use annotations with set_attr here!
467462
self.push_value(obj);
468463
Ok(None)
469464
}

vm/src/pyobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ impl PyObject {
15661566
PyObject {
15671567
payload,
15681568
typ: Some(typ),
1569-
dict: None,
1569+
dict: Some(RefCell::new(PyAttributes::new())),
15701570
}
15711571
.into_ref()
15721572
}

0 commit comments

Comments
 (0)