Skip to content

Commit

Permalink
fix: default param type mismatch errors don't report
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 8, 2024
1 parent 7440f2f commit 14d7fd3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 60 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

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

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

[workspace.dependencies]
erg_common = { version = "0.6.40", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.40", features = ["py_compat", "els"] }
els = { version = "0.1.52", features = ["py_compat"] }
erg_common = { version = "0.6.41", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.41", features = ["py_compat", "els"] }
els = { version = "0.1.53", 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.3.1", features = ["all-nodes-with-ranges", "location"] }
rustpython-ast = { git = "https://github.com/RustPython/Parser", version = "0.3.1", features = ["all-nodes-with-ranges", "location"] }
rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }
rustpython-ast = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }
# erg_compiler = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] }
# erg_common = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat", "els"] }
# els = { git = "https://github.com/erg-lang/erg", branch = "main", features = ["py_compat"] }
Expand Down
52 changes: 23 additions & 29 deletions crates/py2erg/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,6 @@ impl ASTConverter {
unreachable!()
};
let mut fields = vec![];
let mut params = vec![];
for chunk in init_def.body.block {
#[allow(clippy::single_match)]
match chunk {
Expand All @@ -1356,41 +1355,33 @@ impl ASTConverter {
};
// if `self.foo == ...`
if attr.obj.get_name().map(|s| &s[..]) == Some("self") {
let (param_typ_name, arg_typ_name) = if let Some(t_spec_op) = sig
// get attribute types
let arg_typ_ident = if let Some(t_spec_op) = sig
.params
.non_defaults
.iter()
.find(|&param| param.inspect() == Some(attr.ident.inspect()))
.and_then(|param| param.t_spec.as_ref())
{
.or_else(|| {
sig.params
.defaults
.iter()
.find(|&param| param.inspect() == Some(attr.ident.inspect()))
.and_then(|param| param.sig.t_spec.as_ref())
}) {
let typ_name = t_spec_op.t_spec.to_string().replace('.', "");
(typ_name.clone(), typ_name)
Identifier::public_with_line(
DOT,
typ_name.into(),
attr.obj.ln_begin().unwrap_or(0),
)
} else {
("Obj".to_string(), "Never".to_string()) // accept any type, can be any type
Identifier::public_with_line(
DOT,
"Never".into(),
attr.obj.ln_begin().unwrap_or(0),
)
};
let param_typ_ident = Identifier::public_with_line(
DOT,
param_typ_name.into(),
attr.obj.ln_begin().unwrap_or(0),
);
let param_typ_spec = TypeSpec::mono(param_typ_ident.clone());
let expr = Expr::Accessor(Accessor::Ident(param_typ_ident.clone()));
let as_op = Token::new(
TokenKind::As,
"as",
param_typ_spec.ln_begin().unwrap_or(0),
param_typ_spec.col_begin().unwrap_or(0),
);
let param_typ_spec = TypeSpecWithOp::new(as_op, param_typ_spec, expr);
let arg_typ_ident = Identifier::public_with_line(
DOT,
arg_typ_name.into(),
attr.obj.ln_begin().unwrap_or(0),
);
params.push(NonDefaultParamSignature::new(
ParamPattern::VarName(attr.ident.name.clone()),
Some(param_typ_spec),
));
let typ = Expr::Accessor(Accessor::Ident(arg_typ_ident));
let sig =
Signature::Var(VarSignature::new(VarPattern::Ident(attr.ident), None));
Expand Down Expand Up @@ -1424,14 +1415,17 @@ impl ASTConverter {
VisModifierSpec::Public(DOT),
VarName::from_static("__call__"),
);
let params = Params::new(params, None, vec![], None, None);
let class_ident = Identifier::public_with_line(
DOT,
self.namespace.last().unwrap().into(),
sig.ln_begin().unwrap_or(0),
);
let class_ident_expr = Expr::Accessor(Accessor::Ident(class_ident.clone()));
let class_spec = TypeSpecWithOp::new(COLON, TypeSpec::mono(class_ident), class_ident_expr);
let mut params = sig.params.clone();
if params.non_defaults.first().is_some_and(|param| param.inspect().map(|s| &s[..]) == Some("self")) {
params.non_defaults.remove(0);
}
let sig = Signature::Subr(SubrSignature::new(
set! { Decorator(Expr::static_local("Override")) },
call_ident,
Expand Down
10 changes: 10 additions & 0 deletions tests/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ def invalid(self):
assert D(1) > D(0)
c = -d # OK
e = E(1)

class F:
def __init__(self, x: int, y: int = 1, z: int = 2):
self.x = x
self.y = y
self.z = z

_ = F(1)
_ = F(1, 2)
_ = F(1, z=1, y=2)

0 comments on commit 14d7fd3

Please sign in to comment.