mirror of https://github.com/mtshiba/pylyzer
fix: module resolution bug
This commit is contained in:
parent
83d59d4c53
commit
7419411243
|
|
@ -78,6 +78,7 @@ fn escape_name(name: String) -> String {
|
||||||
"str" => "Str".into(),
|
"str" => "Str".into(),
|
||||||
"bool" => "Bool".into(),
|
"bool" => "Bool".into(),
|
||||||
"list" => "GenericArray".into(),
|
"list" => "GenericArray".into(),
|
||||||
|
"bytes" => "Bytes".into(),
|
||||||
// "range" => "GenericRange".into(),
|
// "range" => "GenericRange".into(),
|
||||||
"dict" => "GenericDict".into(),
|
"dict" => "GenericDict".into(),
|
||||||
"set" => "GenericSet".into(),
|
"set" => "GenericSet".into(),
|
||||||
|
|
|
||||||
|
|
@ -56,17 +56,34 @@ pub fn gen_decl_er(input: &Input, hir: HIR, status: CheckStatus) -> DeclFile {
|
||||||
fn gen_chunk_decl(namespace: &str, chunk: Expr, code: &mut String) {
|
fn gen_chunk_decl(namespace: &str, chunk: Expr, code: &mut String) {
|
||||||
match chunk {
|
match chunk {
|
||||||
Expr::Def(def) => {
|
Expr::Def(def) => {
|
||||||
let name = def.sig.ident().inspect().replace('\0', "");
|
let mut name = def
|
||||||
if name.starts_with('%') {
|
.sig
|
||||||
return;
|
.ident()
|
||||||
}
|
.inspect()
|
||||||
|
.replace('\0', "")
|
||||||
|
.replace('%', "___");
|
||||||
let typ = def.sig.ident().ref_t().to_string();
|
let typ = def.sig.ident().ref_t().to_string();
|
||||||
let typ = escape_type(typ);
|
let typ = escape_type(typ);
|
||||||
let decl = format!("{namespace}.{name}: {typ}");
|
// Erg can automatically import nested modules
|
||||||
|
// `import http.client` => `http = pyimport "http"`
|
||||||
|
let decl = if def.sig.ident().ref_t().is_py_module() {
|
||||||
|
name = name.split('.').next().unwrap().to_string();
|
||||||
|
format!(
|
||||||
|
"{namespace}.{name} = pyimport {}",
|
||||||
|
def.sig.ident().ref_t().typarams()[0]
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!("{namespace}.{name}: {typ}")
|
||||||
|
};
|
||||||
*code += &decl;
|
*code += &decl;
|
||||||
}
|
}
|
||||||
Expr::ClassDef(def) => {
|
Expr::ClassDef(def) => {
|
||||||
let class_name = def.sig.ident().inspect().replace('\0', "");
|
let class_name = def
|
||||||
|
.sig
|
||||||
|
.ident()
|
||||||
|
.inspect()
|
||||||
|
.replace('\0', "")
|
||||||
|
.replace('%', "___");
|
||||||
let namespace = format!("{namespace}.{class_name}");
|
let namespace = format!("{namespace}.{class_name}");
|
||||||
let decl = format!(".{class_name}: ClassType");
|
let decl = format!(".{class_name}: ClassType");
|
||||||
*code += &decl;
|
*code += &decl;
|
||||||
|
|
@ -103,6 +120,11 @@ fn gen_chunk_decl(namespace: &str, chunk: Expr, code: &mut String) {
|
||||||
gen_chunk_decl(&namespace, attr, code);
|
gen_chunk_decl(&namespace, attr, code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Expr::Dummy(dummy) => {
|
||||||
|
for chunk in dummy.into_iter() {
|
||||||
|
gen_chunk_decl(namespace, chunk, code);
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
code.push('\n');
|
code.push('\n');
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import http.client
|
||||||
|
|
||||||
test = 1
|
test = 1
|
||||||
|
|
||||||
def add(a, b):
|
def add(a, b):
|
||||||
|
|
|
||||||
|
|
@ -33,3 +33,4 @@ max_delta = timedelta.max
|
||||||
assert dt.datetime.max == max_date
|
assert dt.datetime.max == max_date
|
||||||
|
|
||||||
Resp = http.client.HTTPResponse
|
Resp = http.client.HTTPResponse
|
||||||
|
assert export.http.client.HTTPResponse == Resp
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue