Skip to content

Commit e6e5cc2

Browse files
committed
Populate is_lazy in DirectImport
1 parent 573a293 commit e6e5cc2

6 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎rust/src/caching.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub fn parse_json_to_map(
118118
imported,
119119
line_number,
120120
line_contents,
121+
is_lazy: false, // TODO get working with cache.
121122
})
122123
.collect();
123124
parsed_map.insert(module, import_set);

‎rust/src/import_parsing.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct ImportedObject {
1010
pub line_number: usize,
1111
pub line_contents: String,
1212
pub typechecking_only: bool,
13+
pub is_lazy: bool,
1314
}
1415

1516
impl ImportedObject {
@@ -18,12 +19,14 @@ impl ImportedObject {
1819
line_number: usize,
1920
line_contents: String,
2021
typechecking_only: bool,
22+
is_lazy: bool,
2123
) -> Self {
2224
Self {
2325
name,
2426
line_number,
2527
line_contents,
2628
typechecking_only,
29+
is_lazy,
2730
}
2831
}
2932
}
@@ -84,6 +87,7 @@ impl<'a> StatementVisitor<'a> for Visitor<'a> {
8487
line_number.get(),
8588
self.source_code.line_text(line_number).trim().to_string(),
8689
self.typechecking_only,
90+
import_stmt.is_lazy,
8791
))
8892
}
8993
walk_stmt(self, stmt);
@@ -113,6 +117,7 @@ impl<'a> StatementVisitor<'a> for Visitor<'a> {
113117
line_number.get(),
114118
self.source_code.line_text(line_number).trim().to_string(),
115119
self.typechecking_only,
120+
import_from_stmt.is_lazy,
116121
))
117122
}
118123
walk_stmt(self, stmt);

‎rust/src/import_scanning.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct DirectImport {
1616
pub imported: String,
1717
pub line_number: usize,
1818
pub line_contents: String,
19+
pub is_lazy: bool,
1920
}
2021

2122
impl<'a, 'py> FromPyObject<'a, 'py> for DirectImport {
@@ -26,12 +27,14 @@ impl<'a, 'py> FromPyObject<'a, 'py> for DirectImport {
2627
let imported: String = ob.getattr("imported")?.getattr("name")?.extract()?;
2728
let line_number: usize = ob.getattr("line_number")?.extract()?;
2829
let line_contents: String = ob.getattr("line_contents")?.extract()?;
30+
let is_lazy: bool = ob.getattr("is_lazy")?.extract()?;
2931

3032
Ok(DirectImport {
3133
importer,
3234
imported,
3335
line_number,
3436
line_contents,
37+
is_lazy,
3538
})
3639
}
3740
}
@@ -152,6 +155,7 @@ fn scan_for_imports_no_py_single_module(
152155
imported: imported_module.name.to_string(),
153156
line_number: imported_object.line_number,
154157
line_contents: imported_object.line_contents,
158+
is_lazy: imported_object.is_lazy,
155159
});
156160
}
157161
None => {
@@ -165,6 +169,7 @@ fn scan_for_imports_no_py_single_module(
165169
imported: imported_module,
166170
line_number: imported_object.line_number,
167171
line_contents: imported_object.line_contents,
172+
is_lazy: imported_object.is_lazy,
168173
});
169174
}
170175
}
@@ -196,6 +201,9 @@ fn to_py_direct_imports<'a>(
196201
kwargs
197202
.set_item("line_contents", &rust_import.line_contents)
198203
.unwrap();
204+
kwargs
205+
.set_item("is_lazy", &rust_import.is_lazy)
206+
.unwrap();
199207
let py_direct_import = py_direct_import_class.call((), Some(&kwargs)).unwrap();
200208
pyset.add(&py_direct_import).unwrap();
201209
}

‎src/grimp/domain/valueobjects.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ class DirectImport:
5555
imported: Module
5656
line_number: int
5757
line_contents: str
58+
# Import in the form `lazy import` or `lazy from import`.
59+
is_lazy: bool = False
5860

5961
def __str__(self) -> str:
60-
return f"{self.importer} -> {self.imported} (l. {self.line_number})"
62+
lazy_label = ", lazy" if self.is_lazy else ""
63+
return f"{self.importer} -> {self.imported} (l. {self.line_number}{lazy_label})"
6164

6265

6366
@dataclass(frozen=True, order=True)

‎tests/unit/application/test_scanning.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,66 +152,77 @@ def test_lazy_imports():
152152
imported=Module("foo.two"),
153153
line_number=2,
154154
line_contents="lazy import foo.two",
155+
is_lazy=True,
155156
),
156157
DirectImport(
157158
importer=Module("foo.one.blue"),
158159
imported=Module("externalone"),
159160
line_number=3,
160161
line_contents="lazy import externalone",
162+
is_lazy=True,
161163
),
162164
DirectImport(
163165
importer=Module("foo.one.blue"),
164166
imported=Module("externaltwo"),
165167
line_number=4,
166168
line_contents="lazy import externaltwo.subpackage # with comment afterwards.",
169+
is_lazy=True,
167170
),
168171
DirectImport(
169172
importer=Module("foo.one.blue"),
170173
imported=Module("foo.one.green"),
171174
line_number=8,
172175
line_contents="lazy from foo.one import green",
176+
is_lazy=True,
173177
),
174178
DirectImport(
175179
importer=Module("foo.one.blue"),
176180
imported=Module("foo.two.yellow"),
177181
line_number=9,
178182
line_contents="lazy from foo.two import yellow",
183+
is_lazy=True,
179184
),
180185
DirectImport(
181186
importer=Module("foo.one.blue"),
182187
imported=Module("foo.three"),
183188
line_number=11,
184189
line_contents="lazy from foo import three",
190+
is_lazy=True,
185191
),
186192
DirectImport(
187193
importer=Module("foo.one.blue"),
188194
imported=Module("external"),
189195
line_number=12,
190196
line_contents="lazy from external import one",
197+
is_lazy=True,
191198
),
192199
DirectImport(
193200
importer=Module("foo.one.blue"),
194201
imported=Module("external"),
195202
line_number=13,
196203
line_contents="lazy from external.two import blue # with comment afterwards.",
204+
is_lazy=True,
197205
),
198206
DirectImport(
199207
importer=Module("foo.one.blue"),
200208
imported=Module("foo.one.green"),
201209
line_number=16,
202210
line_contents="lazy from . import green",
211+
is_lazy=True,
203212
),
204213
DirectImport(
205214
importer=Module("foo.one.blue"),
206215
imported=Module("foo.two.yellow"),
207216
line_number=17,
208217
line_contents="lazy from ..two import yellow",
218+
is_lazy=True,
209219
),
210220
DirectImport(
211221
importer=Module("foo.one.blue"),
212222
imported=Module("foo.three"),
213223
line_number=18,
214224
line_contents="lazy from .. import three",
225+
is_lazy=True,
215226
),
216227
}
217228
}

‎tests/unit/domain/test_valueobjects.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def test_str(self):
3333
line_contents="import bar",
3434
)
3535
assert str(import_path) == "foo -> bar (l. 10)"
36+
def test_lazy_str(self):
37+
import_path = DirectImport(
38+
importer=Module("foo"),
39+
imported=Module("bar"),
40+
line_number=10,
41+
line_contents="import bar",
42+
is_lazy=True,
43+
)
44+
assert str(import_path) == "foo -> bar (l. 10, lazy)"
3645

3746

3847
class TestLayer:

0 commit comments

Comments
 (0)