Skip to content

Commit

Permalink
feat: type spec of reassignment
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 16, 2024
1 parent 11527b3 commit bbe828d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ edition = "2021"
repository = "https://github.com/mtshiba/pylyzer"

[workspace.dependencies]
erg_common = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] }
els = { version = "0.1.54-nightly.0", features = ["py_compat"] }
erg_common = { version = "0.6.42-nightly.2", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.42-nightly.2", features = ["py_compat", "els"] }
els = { version = "0.1.54-nightly.2", features = ["py_compat"] }
# rustpython-parser = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
# rustpython-ast = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }
Expand Down
23 changes: 12 additions & 11 deletions crates/py2erg/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ impl ASTConverter {
// if `self.foo == ...`
if attr.obj.get_name().map(|s| &s[..]) == Some("self") {
// get attribute types
let arg_typ_ident = if let Some(t_spec_op) = sig
let typ = if let Some(t_spec_op) = sig
.params
.non_defaults
.iter()
Expand All @@ -1370,19 +1370,20 @@ impl ASTConverter {
.and_then(|param| param.sig.t_spec.as_ref())
}) {
let typ_name = t_spec_op.t_spec.to_string().replace('.', "");
Identifier::public_with_line(
Expr::from(Accessor::Ident(Identifier::public_with_line(
DOT,
typ_name.into(),
attr.obj.ln_begin().unwrap_or(0),
)
)))
} else if let Some(typ) = redef.t_spec.map(|t_spec| t_spec.t_spec_as_expr) {
*typ
} else {
Identifier::public_with_line(
Expr::from(Accessor::Ident(Identifier::public_with_line(
DOT,
"Never".into(),
attr.obj.ln_begin().unwrap_or(0),
)
)))
};
let typ = Expr::Accessor(Accessor::Ident(arg_typ_ident));
let sig =
Signature::Var(VarSignature::new(VarPattern::Ident(attr.ident), None));
let body = DefBody::new(EQUAL, Block::new(vec![typ]), DefId(0));
Expand Down Expand Up @@ -1746,7 +1747,7 @@ impl ASTConverter {
let attr = value.attr(ident);
if let Some(value) = ann_assign.value {
let expr = self.convert_expr(*value);
let redef = ReDef::new(attr, expr);
let redef = ReDef::new(attr, Some(t_spec), expr);
Expr::ReDef(redef)
} else {
let tasc = TypeAscription::new(Expr::Accessor(attr), t_spec);
Expand Down Expand Up @@ -1774,7 +1775,7 @@ impl ASTConverter {
let def = Def::new(sig, body);
Expr::Def(def)
} else {
let redef = ReDef::new(Accessor::Ident(ident), expr);
let redef = ReDef::new(Accessor::Ident(ident), None, expr);
Expr::ReDef(redef)
}
}
Expand All @@ -1784,7 +1785,7 @@ impl ASTConverter {
.convert_attr_ident(attr.attr.to_string(), attr_name_loc(&value));
let attr = value.attr(ident);
let expr = self.convert_expr(*assign.value);
let adef = ReDef::new(attr, expr);
let adef = ReDef::new(attr, None, expr);
Expr::ReDef(adef)
}
py_ast::Expr::Tuple(tuple) => {
Expand Down Expand Up @@ -1897,7 +1898,7 @@ impl ASTConverter {
let ident = self.convert_ident(name.id.to_string(), name.location());
let bin =
BinOp::new(op, Expr::Accessor(Accessor::Ident(prev_ident)), val);
let redef = ReDef::new(Accessor::Ident(ident), Expr::BinOp(bin));
let redef = ReDef::new(Accessor::Ident(ident), None, Expr::BinOp(bin));
Expr::ReDef(redef)
}
}
Expand All @@ -1908,7 +1909,7 @@ impl ASTConverter {
.convert_attr_ident(attr.attr.to_string(), attr_name_loc(&attr_value));
let attr = attr_value.attr(ident);
let bin = BinOp::new(op, Expr::Accessor(attr.clone()), assign_value);
let redef = ReDef::new(attr, Expr::BinOp(bin));
let redef = ReDef::new(attr, None, Expr::BinOp(bin));
Expr::ReDef(redef)
}
other => {
Expand Down
10 changes: 7 additions & 3 deletions tests/collection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
i_arr = [0]
i_lis = [0]

i_arr.append(1)
i_arr.append("a") # ERR
i_lis.append(1)
i_lis.append("a") # ERR
_ = i_lis[0:0]

union_arr: list[int | str] = []
union_arr.append(1)
Expand All @@ -20,10 +21,13 @@
t: tuple[int, str] = (1, "a")
_ = t[0] == 1 # OK
_ = t[1] == 1 # ERR
_ = t[0:1]

def f(s: Str): return None
for i in getattr(1, "aaa", ()):
f(i)

assert 1 in [1, 2]
assert 1 in {1, 2}
assert 1 in {1: "a"}
assert 1 in (1, 2)

0 comments on commit bbe828d

Please sign in to comment.