Skip to content

Commit

Permalink
fix: class importing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 6, 2024
1 parent bff700f commit 7440f2f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 39 deletions.
81 changes: 46 additions & 35 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.40-nightly.1", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.40-nightly.1", features = ["py_compat", "els"] }
els = { version = "0.1.52-nightly.1", features = ["py_compat"] }
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"] }
# 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"] }
Expand Down
12 changes: 12 additions & 0 deletions crates/py2erg/gen_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ impl DeclFileGenerator {
}
}

// e.g. `x: foo.Bar` => `foo = pyimport "foo"; x: foo.Bar`
fn prepare_using_type(&mut self, typ: &Type) {
let namespace = Str::rc(typ.namespace().split('.').next().unwrap());
if !namespace.is_empty() && self.imported.insert(namespace.clone()) {
self.code += &format!("{namespace} = pyimport \"{namespace}\"\n");
}
}

fn gen_chunk_decl(&mut self, chunk: &Expr) {
match chunk {
Expr::Def(def) => {
Expand All @@ -75,6 +83,7 @@ impl DeclFileGenerator {
.replace('\0', "")
.replace(['%', '*'], "___");
let ref_t = def.sig.ident().ref_t();
self.prepare_using_type(ref_t);
let typ = escape_type(ref_t.replace_failure().to_string_unabbreviated());
// Erg can automatically import nested modules
// `import http.client` => `http = pyimport "http"`
Expand Down Expand Up @@ -119,6 +128,7 @@ impl DeclFileGenerator {
.typ()
.replace_failure()
.to_string_unabbreviated();
self.prepare_using_type(class.sup.typ());
let sup = escape_type(sup);
let decl = format!(".{class_name} <: {sup}\n");
self.code += &decl;
Expand All @@ -129,6 +139,7 @@ impl DeclFileGenerator {
}) = def.obj.base_or_sup()
{
for (attr, t) in rec.iter() {
self.prepare_using_type(t);
let typ = escape_type(t.replace_failure().to_string_unabbreviated());
let decl = format!("{}.{}: {typ}\n", self.namespace, attr.symbol);
self.code += &decl;
Expand All @@ -140,6 +151,7 @@ impl DeclFileGenerator {
}) = def.obj.additional()
{
for (attr, t) in rec.iter() {
self.prepare_using_type(t);
let typ = escape_type(t.replace_failure().to_string_unabbreviated());
let decl = format!("{}.{}: {typ}\n", self.namespace, attr.symbol);
self.code += &decl;
Expand Down
2 changes: 1 addition & 1 deletion tests/foo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .bar import i, Bar
from .bar import i, Bar, Baz, Qux

from . import bar
from . import baz
6 changes: 6 additions & 0 deletions tests/foo/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

class Bar:
def f(self): return 1

class Baz(Exception):
pass

class Qux(Baz):
pass
2 changes: 2 additions & 0 deletions tests/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@

Resp = http.client.HTTPResponse
assert export.http.client.HTTPResponse == Resp

_ = bar.Baz

0 comments on commit 7440f2f

Please sign in to comment.