Skip to content

Commit

Permalink
Use new format for type eval snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Jan 29, 2024
1 parent fc2152e commit a5b6065
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 1,042 deletions.
1 change: 0 additions & 1 deletion typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ impl BuildManager {
}
}

// TODO: refactor type check tests to be like symbol table tests
#[cfg(test)]
mod tests {
use std::fs;
Expand Down
43 changes: 32 additions & 11 deletions typechecker/src/type_check/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,14 +1173,21 @@ impl TraversalVisitorImmutGeneric<PythonType> for TypeEvaluator {
/// (line, start, end)
struct DumpTypes {
pub type_eval: TypeEvaluator,
pub types: HashMap<String, PythonType>,
pub types: Vec<SnapshtType>,
pub enderpy_file: EnderpyFile,
}

#[derive(Debug, Clone)]
struct SnapshtType {
pub start: usize,
pub text: String,
pub typ: PythonType,
}

impl DumpTypes {
pub fn new(enderpy_file: EnderpyFile, type_eval: TypeEvaluator) -> Self {
Self {
types: HashMap::new(),
types: vec![],
type_eval,
enderpy_file,
}
Expand All @@ -1194,18 +1201,28 @@ impl DumpTypes {
pub fn save_type(&mut self, expr: &ast::Expression) {
let typ = self.type_eval.get_type(expr).unwrap_or(PythonType::Unknown);
log::debug!("save_type: {:?} => {:?}", expr, typ);
let start_pos = self.enderpy_file().get_position(expr.get_node().start);
let end_pos = self.enderpy_file().get_position(expr.get_node().end);
self.types.insert(format!("{}:{}", start_pos, end_pos), typ);
let symbol_text =
self.enderpy_file().source()[expr.get_node().start..expr.get_node().end].to_string();
let typ = SnapshtType {
start: expr.get_node().start,
text: symbol_text,
typ,
};
self.types.push(typ);
}

// TODO: move type annotation tests to its own file
pub fn save_type_annotation(&mut self, expr: &ast::Expression) {
let typ = self.type_eval.get_type_from_annotation(expr);
log::debug!("save_type: {:?} => {:?}", expr, typ);
let start_pos = self.enderpy_file().get_position(expr.get_node().start);
let end_pos = self.enderpy_file().get_position(expr.get_node().end);
self.types.insert(format!("{}:{}", start_pos, end_pos), typ);
let symbol_text =
self.enderpy_file().source()[expr.get_node().start..expr.get_node().end].to_string();
let typ = SnapshtType {
start: expr.get_node().start,
text: symbol_text,
typ,
};
self.types.push(typ);
}

fn visit_module(&mut self) {
Expand Down Expand Up @@ -1367,15 +1384,19 @@ mod tests {

// sort result by key
let mut result_sorted = result.clone().into_iter().collect::<Vec<_>>();
result_sorted.sort_by(|a, b| a.0.cmp(&b.0));
result_sorted.sort_by(|a, b| a.start.cmp(&b.start));

format!("{:#?}", result_sorted)
let str = result_sorted
.into_iter()
.map(|t| format!("'{}': {:?}", t.text, t.typ))
.collect::<Vec<_>>()
.join("\n");
return str;
}

#[test]
fn test_type_evaluator() {
glob!("../../test_data/inputs/", "*.py", |path| {
log::debug!("Testing file: {:?}", path);
let contents = fs::read_to_string(path).unwrap();
let result = snapshot_type_eval(&contents);
let _ = env_logger::builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ description: "def func(x):\n return x + 1\n"
expression: result
input_file: typechecker/test_data/inputs/any.py
---
[
(
"(line: 1, character: 11):(line: 1, character: 12)",
Unknown,
),
(
"(line: 1, character: 11):(line: 1, character: 16)",
Int,
),
(
"(line: 1, character: 15):(line: 1, character: 16)",
Int,
),
]
'x': Unknown
'x + 1': Int
'1': Int
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: typechecker/src/type_check/type_evaluator.rs
description: "a = 1\nb = \"1\"\nc = True\nd = False\na + int(b)\n\na + c\n\ndef func(param1: int) -> int:\n\treturn param1 + a\n\nclass C:\n\tcls_attribute = 1\n\n\tdef __init__(self, x: int):\n\t\tprint(self.cls_attribute)\n\t\tself.x = x\n\t\tprint(self.x)\n\n\tdef add(self, value: int):\n\t\tself.cls_attribute += value\n\nt = C(0)\nt.add(2)\nt.cls_attribute\nt.x\n\nl = [1,2,3]\nd = {\"a\": 1, \"b\": 2}\ns = {1,2,3}\n\nl.append(1)\n"
expression: result
input_file: typechecker/test_data/inputs/base.py
---
'1': Int
'"1"': Str
'True': Bool
'False': Bool
'a': Int
'int(b)': Class(ClassType { details: Class { name: "int", declaration_path: DeclarationPath { module_name: [TYPESHED].stdlib/builtins.pyi", node: Node { start: 7961, end: 12343 } }, methods: ["__new__", "__new__", "real", "imag", "numerator", "denominator", "conjugate", "bit_length", "__add__", "__sub__", "__mul__", "__floordiv__", "__truediv__", "__mod__", "__divmod__", "__radd__", "__rsub__", "__rmul__", "__rfloordiv__", "__rtruediv__", "__rmod__", "__rdivmod__", "__pow__", "__pow__", "__pow__", "__pow__", "__pow__", "__pow__", "__rpow__", "__and__", "__or__", "__xor__", "__lshift__", "__rshift__", "__rand__", "__ror__", "__rxor__", "__rlshift__", "__rrshift__", "__neg__", "__pos__", "__invert__", "__trunc__", "__ceil__", "__floor__", "__round__", "__getnewargs__", "__eq__", "__ne__", "__lt__", "__le__", "__gt__", "__ge__", "__float__", "__int__", "__abs__", "__hash__", "__bool__", "__index__"], attributes: {}, special: false }, type_parameters: [] })
'a': Int
'c': Bool
'param1': Unknown
'param1 + a': Int
'a': Int
'1': Int
'print(self.cls_attribute)': Callable(CallableType { name: "print", arguments: Arguments { node: Node { start: 69733, end: 69857 }, posonlyargs: [], args: [], vararg: Some(Arg { node: Node { start: 69734, end: 69748 }, arg: "values", annotation: Some(Name(Name { node: Node { start: 69742, end: 69748 }, id: "object" })) }), kwonlyargs: [Arg { node: Node { start: 69750, end: 69771 }, arg: "sep", annotation: Some(BinOp(BinOp { node: Node { start: 69755, end: 69765 }, op: BitOr, left: Name(Name { node: Node { start: 69755, end: 69758 }, id: "str" }), right: Constant(Constant { node: Node { start: 69761, end: 69765 }, value: None }) })) }, Arg { node: Node { start: 69773, end: 69795 }, arg: "end", annotation: Some(BinOp(BinOp { node: Node { start: 69778, end: 69788 }, op: BitOr, left: Name(Name { node: Node { start: 69778, end: 69781 }, id: "str" }), right: Constant(Constant { node: Node { start: 69784, end: 69788 }, value: None }) })) }, Arg { node: Node { start: 69797, end: 69844 }, arg: "file", annotation: Some(BinOp(BinOp { node: Node { start: 69803, end: 69837 }, op: BitOr, left: Subscript(Subscript { node: Node { start: 69803, end: 69830 }, value: Name(Name { node: Node { start: 69803, end: 69825 }, id: "_SupportsWriteAndFlush" }), slice: Name(Name { node: Node { start: 69826, end: 69829 }, id: "str" }) }), right: Constant(Constant { node: Node { start: 69833, end: 69837 }, value: None }) })) }, Arg { node: Node { start: 69846, end: 69857 }, arg: "flush", annotation: Some(Name(Name { node: Node { start: 69853, end: 69857 }, id: "bool" })) }], kw_defaults: [Some(Constant(Constant { node: Node { start: 69768, end: 69771 }, value: " " })), Some(Constant(Constant { node: Node { start: 69791, end: 69795 }, value: "\n" })), Some(Constant(Constant { node: Node { start: 69840, end: 69844 }, value: None })), None], kwarg: None, defaults: [] }, return_type: None })
'self.cls_attribute': Unknown
'x': Unknown
'print(self.x)': Callable(CallableType { name: "print", arguments: Arguments { node: Node { start: 69733, end: 69857 }, posonlyargs: [], args: [], vararg: Some(Arg { node: Node { start: 69734, end: 69748 }, arg: "values", annotation: Some(Name(Name { node: Node { start: 69742, end: 69748 }, id: "object" })) }), kwonlyargs: [Arg { node: Node { start: 69750, end: 69771 }, arg: "sep", annotation: Some(BinOp(BinOp { node: Node { start: 69755, end: 69765 }, op: BitOr, left: Name(Name { node: Node { start: 69755, end: 69758 }, id: "str" }), right: Constant(Constant { node: Node { start: 69761, end: 69765 }, value: None }) })) }, Arg { node: Node { start: 69773, end: 69795 }, arg: "end", annotation: Some(BinOp(BinOp { node: Node { start: 69778, end: 69788 }, op: BitOr, left: Name(Name { node: Node { start: 69778, end: 69781 }, id: "str" }), right: Constant(Constant { node: Node { start: 69784, end: 69788 }, value: None }) })) }, Arg { node: Node { start: 69797, end: 69844 }, arg: "file", annotation: Some(BinOp(BinOp { node: Node { start: 69803, end: 69837 }, op: BitOr, left: Subscript(Subscript { node: Node { start: 69803, end: 69830 }, value: Name(Name { node: Node { start: 69803, end: 69825 }, id: "_SupportsWriteAndFlush" }), slice: Name(Name { node: Node { start: 69826, end: 69829 }, id: "str" }) }), right: Constant(Constant { node: Node { start: 69833, end: 69837 }, value: None }) })) }, Arg { node: Node { start: 69846, end: 69857 }, arg: "flush", annotation: Some(Name(Name { node: Node { start: 69853, end: 69857 }, id: "bool" })) }], kw_defaults: [Some(Constant(Constant { node: Node { start: 69768, end: 69771 }, value: " " })), Some(Constant(Constant { node: Node { start: 69791, end: 69795 }, value: "\n" })), Some(Constant(Constant { node: Node { start: 69840, end: 69844 }, value: None })), None], kwarg: None, defaults: [] }, return_type: None })
'self.x': Unknown
'C(0)': Unknown
'(2)': Unknown
'2': Int
'[1,2,3]': Class(ClassType { details: Class { name: "list", declaration_path: DeclarationPath { module_name: [TYPESHED].stdlib/builtins.pyi", node: Node { start: 43505, end: 46298 } }, methods: ["__init__", "__init__", "copy", "append", "extend", "pop", "index", "count", "insert", "remove", "sort", "sort", "__len__", "__iter__", "__getitem__", "__getitem__", "__setitem__", "__setitem__", "__delitem__", "__add__", "__add__", "__iadd__", "__mul__", "__rmul__", "__imul__", "__contains__", "__reversed__", "__gt__", "__ge__", "__lt__", "__le__", "__eq__"], attributes: {}, special: false }, type_parameters: [Int] })
'{"a": 1, "b": 2}': Class(ClassType { details: Class { name: "dict", declaration_path: DeclarationPath { module_name: [TYPESHED].stdlib/builtins.pyi", node: Node { start: 46298, end: 49982 } }, methods: ["__init__", "__init__", "__init__", "__init__", "__init__", "__init__", "__init__", "__init__", "__new__", "copy", "keys", "values", "items", "fromkeys", "fromkeys", "get", "get", "get", "pop", "pop", "pop", "__len__", "__getitem__", "__setitem__", "__delitem__", "__iter__", "__eq__"], attributes: {}, special: false }, type_parameters: [Str, Int] })
'{1,2,3}': Class(ClassType { details: Class { name: "set", declaration_path: DeclarationPath { module_name: [TYPESHED].stdlib/builtins.pyi", node: Node { start: 49982, end: 52187 } }, methods: ["__init__", "__init__", "add", "copy", "difference", "difference_update", "discard", "intersection", "intersection_update", "isdisjoint", "issubset", "issuperset", "remove", "symmetric_difference", "symmetric_difference_update", "union", "update", "__len__", "__contains__", "__iter__", "__and__", "__iand__", "__or__", "__ior__", "__sub__", "__isub__", "__xor__", "__ixor__", "__le__", "__lt__", "__ge__", "__gt__", "__eq__"], attributes: {}, special: false }, type_parameters: [Int] })
'(1)': Unknown
'1': Int
Loading

0 comments on commit a5b6065

Please sign in to comment.