Skip to content

Commit fd81fb1

Browse files
committed
Address review feedback
1 parent dcc75f5 commit fd81fb1

11 files changed

+32
-58
lines changed

crates/red_knot_python_semantic/src/module_resolver/resolver.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl SearchPaths {
159159

160160
let SearchPathSettings {
161161
extra_paths,
162-
workspace_root: src_root,
162+
src_root,
163163
custom_typeshed,
164164
site_packages: site_packages_paths,
165165
} = settings;
@@ -1293,7 +1293,7 @@ mod tests {
12931293
target_version: PythonVersion::PY38,
12941294
search_paths: SearchPathSettings {
12951295
extra_paths: vec![],
1296-
workspace_root: src.clone(),
1296+
src_root: src.clone(),
12971297
custom_typeshed: Some(custom_typeshed),
12981298
site_packages: SitePackages::Known(vec![site_packages]),
12991299
},
@@ -1798,7 +1798,7 @@ not_a_directory
17981798
target_version: PythonVersion::default(),
17991799
search_paths: SearchPathSettings {
18001800
extra_paths: vec![],
1801-
workspace_root: SystemPathBuf::from("/src"),
1801+
src_root: SystemPathBuf::from("/src"),
18021802
custom_typeshed: None,
18031803
site_packages: SitePackages::Known(vec![
18041804
venv_site_packages,

crates/red_knot_python_semantic/src/module_resolver/testing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl TestCaseBuilder<MockedTypeshed> {
229229
target_version,
230230
search_paths: SearchPathSettings {
231231
extra_paths: vec![],
232-
workspace_root: src.clone(),
232+
src_root: src.clone(),
233233
custom_typeshed: Some(typeshed.clone()),
234234
site_packages: SitePackages::Known(vec![site_packages.clone()]),
235235
},

crates/red_knot_python_semantic/src/program.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct SearchPathSettings {
7070
pub extra_paths: Vec<SystemPathBuf>,
7171

7272
/// The root of the workspace, used for finding first-party modules.
73-
pub workspace_root: SystemPathBuf,
73+
pub src_root: SystemPathBuf,
7474

7575
/// Optional path to a "custom typeshed" directory on disk for us to use for standard-library types.
7676
/// If this is not provided, we will fallback to our vendored typeshed stubs for the stdlib,
@@ -84,7 +84,7 @@ pub struct SearchPathSettings {
8484
impl SearchPathSettings {
8585
pub fn new(src_root: SystemPathBuf) -> Self {
8686
Self {
87-
workspace_root: src_root,
87+
src_root,
8888
extra_paths: vec![],
8989
custom_typeshed: None,
9090
site_packages: SitePackages::Known(vec![]),

crates/red_knot_workspace/src/workspace/metadata.rs

+14-40
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl WorkspaceMetadata {
9090
pyproject,
9191
ancestor.to_path_buf(),
9292
base_configuration,
93-
)?;
93+
);
9494

9595
if let Some(workspace_table) = workspace_table {
9696
let workspace_root = ancestor;
@@ -168,7 +168,7 @@ impl WorkspaceMetadata {
168168

169169
// Create a package with a default configuration
170170
PackageMetadata {
171-
name: path.file_name().unwrap_or("<virtual>").into(),
171+
name: path.file_name().unwrap_or("root").into(),
172172
root: path.to_path_buf(),
173173
// TODO create the configuration from the pyproject toml
174174
configuration: base_configuration.cloned().unwrap_or_default(),
@@ -206,14 +206,11 @@ impl PackageMetadata {
206206
pyproject: PyProject,
207207
root: SystemPathBuf,
208208
base_configuration: Option<&Configuration>,
209-
) -> Result<Self, WorkspaceDiscoveryError> {
210-
let Some(project) = pyproject.project else {
211-
return Err(WorkspaceDiscoveryError::MissingProjectTable { package_path: root });
212-
};
213-
214-
let Some(name) = project.name.as_ref() else {
215-
return Err(WorkspaceDiscoveryError::MissingPackageName { package_path: root });
216-
};
209+
) -> Self {
210+
let name = pyproject.project.and_then(|project| project.name);
211+
let name = name
212+
.map(|name| Name::new(&*name))
213+
.unwrap_or_else(|| Name::new(root.file_name().unwrap_or("root")));
217214

218215
// TODO: load configuration from pyrpoject.toml
219216
let mut configuration = Configuration::default();
@@ -222,11 +219,11 @@ impl PackageMetadata {
222219
configuration.extend(base_configuration.clone());
223220
}
224221

225-
Ok(PackageMetadata {
226-
name: Name::from(name.as_str()),
222+
PackageMetadata {
223+
name,
227224
root,
228225
configuration,
229-
})
226+
}
230227
}
231228

232229
pub fn name(&self) -> &Name {
@@ -341,7 +338,7 @@ fn collect_packages(
341338
});
342339
}
343340

344-
let package = PackageMetadata::from_pyproject(pyproject, member_path, base_configuration)?;
341+
let package = PackageMetadata::from_pyproject(pyproject, member_path, base_configuration);
345342

346343
tracing::debug!(
347344
"Adding package '{}' at '{}'",
@@ -407,12 +404,6 @@ pub enum WorkspaceDiscoveryError {
407404
#[from]
408405
error: GlobError,
409406
},
410-
411-
#[error("the `[project]` is missing in the `pyproject.toml` for package '{package_path}'`")]
412-
MissingProjectTable { package_path: SystemPathBuf },
413-
414-
#[error("the `project.name` is missing in the `pyproject.toml` for package '{package_path}'`")]
415-
MissingPackageName { package_path: SystemPathBuf },
416407
}
417408

418409
#[cfg(test)]
@@ -433,7 +424,7 @@ mod tests {
433424

434425
system
435426
.memory_file_system()
436-
.write_files([(root.join("src/foo.py"), ""), (root.join("src/bar.py"), "")])
427+
.write_files([(root.join("foo.py"), ""), (root.join("bar.py"), "")])
437428
.context("Failed to write files")?;
438429

439430
let workspace = WorkspaceMetadata::discover(&root, &system, None)
@@ -454,15 +445,14 @@ mod tests {
454445
system
455446
.memory_file_system()
456447
.write_files([
457-
(root.join("src/foo.py"), ""),
458-
(root.join("src/bar.py"), ""),
459448
(
460449
root.join("pyproject.toml"),
461450
r#"
462451
[project]
463452
name = "backend"
464453
"#,
465454
),
455+
(root.join("db/__init__.py"), ""),
466456
])
467457
.context("Failed to write files")?;
468458

@@ -473,7 +463,7 @@ mod tests {
473463
snapshot_workspace!(workspace);
474464

475465
// Discovering the same package from a subdirectory should give the same result
476-
let from_src = WorkspaceMetadata::discover(&root.join("src"), &system, None)
466+
let from_src = WorkspaceMetadata::discover(&root.join("db"), &system, None)
477467
.context("Failed to discover workspace from src sub-directory")?;
478468

479469
assert_eq!(from_src, workspace);
@@ -489,8 +479,6 @@ mod tests {
489479
system
490480
.memory_file_system()
491481
.write_files([
492-
(root.join("src/foo.py"), ""),
493-
(root.join("src/bar.py"), ""),
494482
(
495483
root.join("pyproject.toml"),
496484
r#"
@@ -543,8 +531,6 @@ mod tests {
543531
system
544532
.memory_file_system()
545533
.write_files([
546-
(root.join("src/foo.py"), ""),
547-
(root.join("src/bar.py"), ""),
548534
(
549535
root.join("pyproject.toml"),
550536
r#"
@@ -597,8 +583,6 @@ mod tests {
597583
system
598584
.memory_file_system()
599585
.write_files([
600-
(root.join("src/foo.py"), ""),
601-
(root.join("src/bar.py"), ""),
602586
(
603587
root.join("pyproject.toml"),
604588
r#"
@@ -643,8 +627,6 @@ mod tests {
643627
system
644628
.memory_file_system()
645629
.write_files([
646-
(root.join("src/foo.py"), ""),
647-
(root.join("src/bar.py"), ""),
648630
(
649631
root.join("pyproject.toml"),
650632
r#"
@@ -685,8 +667,6 @@ mod tests {
685667
system
686668
.memory_file_system()
687669
.write_files([
688-
(root.join("src/foo.py"), ""),
689-
(root.join("src/bar.py"), ""),
690670
(
691671
root.join("pyproject.toml"),
692672
r#"
@@ -721,8 +701,6 @@ mod tests {
721701
system
722702
.memory_file_system()
723703
.write_files([
724-
(root.join("src/foo.py"), ""),
725-
(root.join("src/bar.py"), ""),
726704
(
727705
root.join("pyproject.toml"),
728706
r#"
@@ -752,8 +730,6 @@ mod tests {
752730
system
753731
.memory_file_system()
754732
.write_files([
755-
(root.join("src/foo.py"), ""),
756-
(root.join("src/bar.py"), ""),
757733
(
758734
root.join("pyproject.toml"),
759735
r#"
@@ -783,8 +759,6 @@ mod tests {
783759
system
784760
.memory_file_system()
785761
.write_files([
786-
(root.join("src/foo.py"), ""),
787-
(root.join("src/bar.py"), ""),
788762
(
789763
root.join("pyproject.toml"),
790764
r#"

crates/red_knot_workspace/src/workspace/settings.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ impl SearchPathConfiguration {
7474
.unwrap_or(SitePackages::Known(vec![]));
7575

7676
SearchPathSettings {
77-
extra_paths: self.clone().extra_paths.unwrap_or_default(),
78-
workspace_root: self
77+
extra_paths: self.extra_paths.clone().unwrap_or_default(),
78+
src_root: self
7979
.clone()
8080
.src_root
8181
.unwrap_or_else(|| workspace_root.to_path_buf()),
82-
custom_typeshed: self.clone().custom_typeshed,
82+
custom_typeshed: self.custom_typeshed.clone(),
8383
site_packages,
8484
}
8585
}

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__member_pattern_matching_file.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ WorkspaceMetadata(
2727
),
2828
search_paths: SearchPathSettings(
2929
extra_paths: [],
30-
workspace_root: "/app",
30+
src_root: "/app",
3131
custom_typeshed: None,
3232
site_packages: Known([]),
3333
),

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__member_pattern_matching_hidden_folder.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/red_knot_workspace/src/workspace/metadata.rs
3-
expression: "&workspace"
3+
expression: workspace
44
---
55
WorkspaceMetadata(
66
root: "/app",
@@ -27,7 +27,7 @@ WorkspaceMetadata(
2727
),
2828
search_paths: SearchPathSettings(
2929
extra_paths: [],
30-
workspace_root: "/app",
30+
src_root: "/app",
3131
custom_typeshed: None,
3232
site_packages: Known([]),
3333
),

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__package_without_pyproject.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/red_knot_workspace/src/workspace/metadata.rs
3-
expression: "&workspace"
3+
expression: workspace
44
---
55
WorkspaceMetadata(
66
root: "/app",
@@ -27,7 +27,7 @@ WorkspaceMetadata(
2727
),
2828
search_paths: SearchPathSettings(
2929
extra_paths: [],
30-
workspace_root: "/app",
30+
src_root: "/app",
3131
custom_typeshed: None,
3232
site_packages: Known([]),
3333
),

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__single_package.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/red_knot_workspace/src/workspace/metadata.rs
3-
expression: "&workspace"
3+
expression: workspace
44
---
55
WorkspaceMetadata(
66
root: "/app",
@@ -27,7 +27,7 @@ WorkspaceMetadata(
2727
),
2828
search_paths: SearchPathSettings(
2929
extra_paths: [],
30-
workspace_root: "/app",
30+
src_root: "/app",
3131
custom_typeshed: None,
3232
site_packages: Known([]),
3333
),

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__workspace_excluded.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ WorkspaceMetadata(
4040
),
4141
search_paths: SearchPathSettings(
4242
extra_paths: [],
43-
workspace_root: "/app",
43+
src_root: "/app",
4444
custom_typeshed: None,
4545
site_packages: Known([]),
4646
),

crates/red_knot_workspace/src/workspace/snapshots/red_knot_workspace__workspace__metadata__tests__workspace_members.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ WorkspaceMetadata(
5353
),
5454
search_paths: SearchPathSettings(
5555
extra_paths: [],
56-
workspace_root: "/app",
56+
src_root: "/app",
5757
custom_typeshed: None,
5858
site_packages: Known([]),
5959
),

0 commit comments

Comments
 (0)