monochange_python 0.5.1

Python ecosystem adapter for monochange — discovers uv workspaces, Poetry, and setuptools projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#![deny(clippy::all)]
#![forbid(clippy::indexing_slicing)]

//! # `monochange_python`
//!
//! `monochange_python` discovers Python packages from uv workspaces, Poetry
//! projects, and standalone `pyproject.toml` files.
//!
//! ## Why use it?
//!
//! - discover uv workspaces and standalone Python packages with one adapter
//! - normalize Python package manifests and dependency edges for the shared
//!   planner
//! - infer lockfile refresh commands for uv and Poetry
//!
//! ## Public entry points
//!
//! - `discover_python_packages(root)` discovers Python packages
//! - `PythonAdapter` exposes the shared adapter interface
//!
//! ## Scope
//!
//! - uv workspace member expansion
//! - `pyproject.toml` parsing (`[project]` and `[tool.poetry]`)
//! - normalized dependency extraction from PEP 621 metadata
//! - lockfile command inference for uv and Poetry

use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

use glob::glob;
use monochange_core::AdapterDiscovery;
use monochange_core::DependencyKind;
use monochange_core::Ecosystem;
use monochange_core::EcosystemAdapter;
use monochange_core::LockfileCommandExecution;
use monochange_core::MonochangeError;
use monochange_core::MonochangeResult;
use monochange_core::PackageDependency;
use monochange_core::PackageRecord;
use monochange_core::PublishState;
use monochange_core::ShellConfig;
use monochange_core::SourceConfiguration;
use monochange_core::normalize_path;
use monochange_publish::PublishRequest;
use semver::Version;
use toml::Value;
use toml_edit::DocumentMut;
use toml_edit::Item;
use walkdir::DirEntry;
use walkdir::WalkDir;

pub const PYPROJECT_FILE: &str = "pyproject.toml";
pub const UV_LOCK_FILE: &str = "uv.lock";
pub const POETRY_LOCK_FILE: &str = "poetry.lock";

pub fn write_python_placeholder_manifest(
	dir: &Path,
	request: &PublishRequest,
	source: Option<&SourceConfiguration>,
) -> MonochangeResult<()> {
	let module_name = python_placeholder_module_name(&request.package_name);
	let project_urls = source.map(|source| {
		format!(
			"\n[project.urls]\nRepository = \"https://github.com/{}/{}\"\n",
			source.owner, source.repo
		)
	});
	let pyproject = format!(
		"[build-system]\nrequires = [\"hatchling\"]\nbuild-backend = \"hatchling.build\"\n\n[project]\nname = \"{}\"\nversion = \"{}\"\ndescription = \"Placeholder package for {}\"\nreadme = \"README.md\"\nrequires-python = \">=3.8\"\n{}\n[tool.hatch.build.targets.wheel]\npackages = [\"src/{}\"]\n",
		request.package_name,
		request.version,
		request.package_name,
		project_urls.unwrap_or_default(),
		module_name,
	);
	fs::write(dir.join("pyproject.toml"), pyproject).map_err(|error| {
		MonochangeError::Io(format!(
			"failed to write placeholder pyproject.toml: {error}"
		))
	})?;
	let package_dir = dir.join("src").join(&module_name);
	fs::create_dir_all(&package_dir).map_err(|error| {
		MonochangeError::Io(format!(
			"failed to create placeholder Python package: {error}"
		))
	})?;
	fs::write(
		package_dir.join("__init__.py"),
		"\"\"\"Placeholder package published by monochange.\"\"\"\n",
	)
	.map_err(|error| {
		MonochangeError::Io(format!(
			"failed to write placeholder Python package module: {error}"
		))
	})
}

fn python_placeholder_module_name(package_name: &str) -> String {
	let mut module = String::new();
	for character in package_name.chars() {
		if character.is_ascii_alphanumeric() || character == '_' {
			module.push(character.to_ascii_lowercase());
		} else {
			module.push('_');
		}
	}
	if module.is_empty() || module.starts_with(|character: char| character.is_ascii_digit()) {
		module.insert_str(0, "placeholder_");
	}
	module
}

pub struct PythonAdapter;

#[must_use]
pub const fn adapter() -> PythonAdapter {
	PythonAdapter
}

impl EcosystemAdapter for PythonAdapter {
	fn ecosystem(&self) -> Ecosystem {
		Ecosystem::Python
	}

	fn discover(&self, root: &Path) -> MonochangeResult<AdapterDiscovery> {
		discover_python_packages(root)
	}

	fn load_configured(
		&self,
		_root: &Path,
		_package_path: &Path,
	) -> MonochangeResult<Option<PackageRecord>> {
		Ok(None)
	}

	fn supported_versioned_file_kind(&self, path: &Path) -> bool {
		supported_versioned_file_kind(path).is_some()
	}

	fn validate_versioned_file(
		&self,
		full_path: &Path,
		display_path: &str,
		custom_fields: Option<&[String]>,
	) -> MonochangeResult<()> {
		validate_versioned_file(full_path, display_path, custom_fields)
	}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum PythonVersionedFileKind {
	Manifest,
	Lock,
}

#[must_use]
pub fn supported_versioned_file_kind(path: &Path) -> Option<PythonVersionedFileKind> {
	let file_name = path
		.file_name()
		.and_then(|name| name.to_str())
		.unwrap_or_default();
	match file_name {
		PYPROJECT_FILE => Some(PythonVersionedFileKind::Manifest),
		UV_LOCK_FILE | POETRY_LOCK_FILE => Some(PythonVersionedFileKind::Lock),
		_ => None,
	}
}

pub fn discover_lockfiles(package: &PackageRecord) -> Vec<PathBuf> {
	let manifest_dir = package
		.manifest_path
		.parent()
		.map_or_else(|| package.workspace_root.clone(), Path::to_path_buf);
	let scope = if manifest_dir == package.workspace_root {
		manifest_dir.clone()
	} else {
		package.workspace_root.clone()
	};

	let mut discovered: Vec<PathBuf> = [scope.join(UV_LOCK_FILE), scope.join(POETRY_LOCK_FILE)]
		.into_iter()
		.filter(|path| path.exists())
		.collect();

	if discovered.is_empty() && scope != manifest_dir {
		discovered.extend(
			[
				manifest_dir.join(UV_LOCK_FILE),
				manifest_dir.join(POETRY_LOCK_FILE),
			]
			.into_iter()
			.filter(|path| path.exists()),
		);
	}

	discovered
}

pub fn default_lockfile_commands(package: &PackageRecord) -> Vec<LockfileCommandExecution> {
	let lockfiles = discover_lockfiles(package);
	lockfiles
		.into_iter()
		.filter_map(|lockfile| {
			let file_name = lockfile.file_name()?.to_str()?;
			let command = lockfile_command(file_name)?;
			Some(LockfileCommandExecution {
				command: command.to_string(),
				cwd: lockfile
					.parent()
					.unwrap_or(&package.workspace_root)
					.to_path_buf(),
				shell: ShellConfig::None,
			})
		})
		.collect()
}

fn lockfile_command(file_name: &str) -> Option<&'static str> {
	match file_name {
		UV_LOCK_FILE => Some("uv lock"),
		POETRY_LOCK_FILE => Some("poetry lock --no-update"),
		_ => None,
	}
}

pub fn update_versioned_file_text(
	contents: &str,
	kind: PythonVersionedFileKind,
	owner_version: Option<&str>,
	versioned_deps: &BTreeMap<String, String>,
) -> Result<String, toml_edit::TomlError> {
	let mut document = contents.parse::<DocumentMut>()?;
	update_versioned_file(&mut document, kind, owner_version, versioned_deps);
	Ok(document.to_string())
}

pub fn update_versioned_file(
	document: &mut DocumentMut,
	kind: PythonVersionedFileKind,
	owner_version: Option<&str>,
	versioned_deps: &BTreeMap<String, String>,
) {
	match kind {
		PythonVersionedFileKind::Manifest => {
			update_project_version(document, owner_version);
			update_project_dependencies(document, versioned_deps);
		}
		PythonVersionedFileKind::Lock => {
			// Lock files (uv.lock, poetry.lock) are complex and fragile to
			// mutate directly. Prefer running lockfile commands (`uv lock` or
			// `poetry lock --no-update`) which re-resolve the full dependency
			// graph after manifest versions are updated.
		}
	}
}

fn update_project_version(document: &mut DocumentMut, owner_version: Option<&str>) {
	let Some(version) = owner_version else {
		return;
	};
	let Some(project) = document
		.get_mut("project")
		.and_then(Item::as_table_like_mut)
	else {
		return;
	};
	if let Some(existing) = project.get_mut("version")
		&& let Some(existing_value) = existing.as_value()
	{
		let mut new_value = toml_edit::Value::from(version);
		*new_value.decor_mut() = existing_value.decor().clone();
		*existing = Item::Value(new_value);
	}
}

fn update_project_dependencies(
	document: &mut DocumentMut,
	versioned_deps: &BTreeMap<String, String>,
) {
	if versioned_deps.is_empty() {
		return;
	}
	let Some(project) = document
		.get_mut("project")
		.and_then(Item::as_table_like_mut)
	else {
		return;
	};
	let Some(deps) = project.get_mut("dependencies").and_then(Item::as_array_mut) else {
		return;
	};
	for item in deps.iter_mut() {
		let Some(spec) = item.as_str() else {
			continue;
		};
		if let Some(updated) = update_dependency_specifier(spec, versioned_deps) {
			let mut new_value = toml_edit::Value::from(updated);
			*new_value.decor_mut() = item.decor().clone();
			*item = new_value;
		}
	}
}

/// Update a PEP 508 dependency specifier if the package name matches a
/// versioned dependency.
///
/// Input:  `"my-core>=1.0.0"`, deps = `{"my-core": ">=2.0.0"}`
/// Output: `Some("my-core>=2.0.0")`
fn update_dependency_specifier(
	spec: &str,
	versioned_deps: &BTreeMap<String, String>,
) -> Option<String> {
	let name = parse_dependency_name(spec)?;
	let normalized = normalize_python_package_name(&name);
	let version = versioned_deps.get(&normalized)?;
	// Replace everything after the package name with the new version constraint.
	// Preserve extras (e.g., `httpx[cli]>=1.0` → `httpx[cli]>=2.0`).
	let after_name = &spec[name.len()..];
	let extras_end = after_name
		.find(|ch: char| ch != '[' && ch != ']' && !ch.is_alphanumeric() && ch != ',')
		.unwrap_or(0);
	let extras = &after_name[..extras_end];
	Some(format!("{name}{extras}{version}"))
}

/// Parse the package name from a PEP 508 dependency specifier.
///
/// `"httpx>=0.20.0"` → `"httpx"`
/// `"httpx[cli]>=0.20.0"` → `"httpx"`
/// `"Django>2.1; os_name != 'nt'"` → `"Django"`
fn parse_dependency_name(spec: &str) -> Option<String> {
	let name: String = spec
		.chars()
		.take_while(|ch| ch.is_alphanumeric() || *ch == '-' || *ch == '_' || *ch == '.')
		.collect();
	if name.is_empty() { None } else { Some(name) }
}

/// Normalize a Python package name per PEP 503: lowercase, replace [-_.]+
/// with a single hyphen.
fn normalize_python_package_name(name: &str) -> String {
	let mut result = String::with_capacity(name.len());
	let mut prev_was_separator = false;
	for ch in name.chars() {
		if ch == '-' || ch == '_' || ch == '.' {
			if !prev_was_separator && !result.is_empty() {
				result.push('-');
			}
			prev_was_separator = true;
		} else {
			result.push(ch.to_ascii_lowercase());
			prev_was_separator = false;
		}
	}
	result
}

#[tracing::instrument(skip_all)]
pub fn discover_python_packages(root: &Path) -> MonochangeResult<AdapterDiscovery> {
	let mut packages = Vec::new();
	let mut warnings = Vec::new();
	let mut included_manifests = BTreeSet::new();

	// Phase 1: uv workspace discovery
	let root_manifest = root.join(PYPROJECT_FILE);
	if root_manifest.exists() {
		let workspace_members = match parse_uv_workspace_members(&root_manifest) {
			Ok(members) => members,
			Err(error) => {
				warnings.push(format!("skipped {}: {error}", root_manifest.display()));
				None
			}
		};
		if let Some(workspace_members) = workspace_members {
			// Exclude the workspace root manifest from standalone discovery
			included_manifests.insert(normalize_path(&root_manifest));
			let member_manifests =
				expand_workspace_members(root, &workspace_members, &mut warnings);
			for manifest_path in member_manifests {
				if let Some(package) = parse_python_package(&manifest_path, root)? {
					included_manifests.insert(normalize_path(&manifest_path));
					packages.push(package);
				}
			}
		}
	}

	// Phase 2: scan for standalone pyproject.toml files not already discovered.
	// Parse errors are treated as warnings since the walker picks up all
	// pyproject.toml files including test fixtures and generated files.
	for manifest_path in find_all_pyproject_files(root) {
		let normalized = normalize_path(&manifest_path);
		if included_manifests.contains(&normalized) {
			continue;
		}
		let manifest_dir = manifest_path
			.parent()
			.unwrap_or_else(|| Path::new("."))
			.to_path_buf();
		match parse_python_package(&manifest_path, &manifest_dir) {
			Ok(Some(package)) => packages.push(package),
			Ok(None) => {}
			Err(error) => {
				warnings.push(format!("skipped {}: {error}", manifest_path.display()));
			}
		}
	}

	packages.sort_by(|left, right| left.id.cmp(&right.id));
	packages.dedup_by(|left, right| left.id == right.id);

	tracing::debug!(packages = packages.len(), "discovered python packages");

	Ok(AdapterDiscovery { packages, warnings })
}

fn parse_uv_workspace_members(manifest_path: &Path) -> MonochangeResult<Option<Vec<String>>> {
	let contents = fs::read_to_string(manifest_path).map_err(|error| {
		MonochangeError::Io(format!(
			"failed to read {}: {error}",
			manifest_path.display()
		))
	})?;
	let parsed = toml::from_str::<Value>(&contents).map_err(|error| {
		MonochangeError::Discovery(format!(
			"failed to parse {}: {error}",
			manifest_path.display()
		))
	})?;

	let members = parsed
		.get("tool")
		.and_then(|tool| tool.get("uv"))
		.and_then(|uv| uv.get("workspace"))
		.and_then(|workspace| workspace.get("members"))
		.and_then(Value::as_array)
		.map(|members| {
			members
				.iter()
				.filter_map(Value::as_str)
				.map(ToString::to_string)
				.collect()
		});

	Ok(members)
}

fn expand_workspace_members(
	root: &Path,
	patterns: &[String],
	warnings: &mut Vec<String>,
) -> BTreeSet<PathBuf> {
	let mut manifests = BTreeSet::new();

	for pattern in patterns {
		let joined = root.join(pattern).to_string_lossy().to_string();
		let matches: Vec<PathBuf> = glob(&joined)
			.into_iter()
			.flat_map(|paths| paths.filter_map(Result::ok))
			.map(|path| normalize_path(&path))
			.collect();

		if matches.is_empty() {
			warnings.push(format!(
				"uv workspace pattern `{pattern}` under {} matched no packages",
				root.display()
			));
		}

		for matched_path in matches {
			let manifest_path = if matched_path.is_dir() {
				matched_path.join(PYPROJECT_FILE)
			} else if matched_path.file_name().and_then(|name| name.to_str())
				== Some(PYPROJECT_FILE)
			{
				matched_path
			} else {
				continue;
			};

			if manifest_path.exists() {
				manifests.insert(manifest_path);
			}
		}
	}

	manifests
}

fn parse_python_package(
	manifest_path: &Path,
	workspace_root: &Path,
) -> MonochangeResult<Option<PackageRecord>> {
	let contents = fs::read_to_string(manifest_path).map_err(|error| {
		MonochangeError::Io(format!(
			"failed to read {}: {error}",
			manifest_path.display()
		))
	})?;
	let parsed = toml::from_str::<Value>(&contents).map_err(|error| {
		MonochangeError::Discovery(format!(
			"failed to parse {}: {error}",
			manifest_path.display()
		))
	})?;

	// Prefer [project] (PEP 621) over [tool.poetry]
	let (name, version, dependencies) = if let Some(project) = parsed.get("project") {
		let name = project.get("name").and_then(Value::as_str);
		let version = project
			.get("version")
			.and_then(Value::as_str)
			.and_then(parse_pep440_as_semver);
		let dynamic = project
			.get("dynamic")
			.and_then(Value::as_array)
			.is_some_and(|arr| arr.iter().any(|v| v.as_str() == Some("version")));
		let version = if dynamic { None } else { version };
		let deps = parse_pep621_dependencies(project);
		(name, version, deps)
	} else if let Some(poetry) = parsed.get("tool").and_then(|tool| tool.get("poetry")) {
		let name = poetry.get("name").and_then(Value::as_str);
		let version = poetry
			.get("version")
			.and_then(Value::as_str)
			.and_then(parse_pep440_as_semver);
		let deps = parse_poetry_dependencies(poetry);
		(name, version, deps)
	} else {
		return Ok(None);
	};

	let Some(name) = name else {
		return Ok(None);
	};

	let mut record = PackageRecord::new(
		Ecosystem::Python,
		name,
		normalize_path(manifest_path),
		normalize_path(workspace_root),
		version,
		PublishState::Public,
	);
	record.declared_dependencies = dependencies;
	Ok(Some(record))
}

fn parse_pep621_dependencies(project: &Value) -> Vec<PackageDependency> {
	let mut deps = Vec::new();

	if let Some(dep_array) = project.get("dependencies").and_then(Value::as_array) {
		for dep in dep_array {
			if let Some(spec) = dep.as_str()
				&& let Some(name) = parse_dependency_name(spec)
			{
				deps.push(PackageDependency {
					name: normalize_python_package_name(&name),
					kind: DependencyKind::Runtime,
					version_constraint: extract_version_constraint(spec, &name),
					optional: false,
					source_field: Some("dependencies".to_string()),
				});
			}
		}
	}

	if let Some(optional_deps) = project
		.get("optional-dependencies")
		.and_then(Value::as_table)
	{
		for (_group, group_deps) in optional_deps {
			if let Some(dep_array) = group_deps.as_array() {
				for dep in dep_array {
					if let Some(spec) = dep.as_str()
						&& let Some(name) = parse_dependency_name(spec)
					{
						deps.push(PackageDependency {
							name: normalize_python_package_name(&name),
							kind: DependencyKind::Development,
							version_constraint: extract_version_constraint(spec, &name),
							optional: true,
							source_field: Some("optional-dependencies".to_string()),
						});
					}
				}
			}
		}
	}

	deps
}

fn parse_poetry_dependencies(poetry: &Value) -> Vec<PackageDependency> {
	let mut deps = Vec::new();

	if let Some(dep_table) = poetry.get("dependencies").and_then(Value::as_table) {
		for (name, value) in dep_table {
			if name == "python" {
				continue;
			}
			let constraint = match value {
				Value::String(version) => Some(version.clone()),
				Value::Table(table) => {
					table
						.get("version")
						.and_then(Value::as_str)
						.map(ToString::to_string)
				}
				_ => None,
			};
			deps.push(PackageDependency {
				name: normalize_python_package_name(name),
				kind: DependencyKind::Runtime,
				version_constraint: constraint,
				optional: false,
				source_field: Some("dependencies".to_string()),
			});
		}
	}

	// Parse grouped dev dependencies
	if let Some(groups) = poetry.get("group").and_then(Value::as_table) {
		for (_group_name, group) in groups {
			if let Some(group_deps) = group
				.as_table()
				.and_then(|table| table.get("dependencies"))
				.and_then(Value::as_table)
			{
				for (name, value) in group_deps {
					let constraint = match value {
						Value::String(version) => Some(version.clone()),
						Value::Table(table) => {
							table
								.get("version")
								.and_then(Value::as_str)
								.map(ToString::to_string)
						}
						_ => None,
					};
					deps.push(PackageDependency {
						name: normalize_python_package_name(name),
						kind: DependencyKind::Development,
						version_constraint: constraint,
						optional: false,
						source_field: Some("group.dependencies".to_string()),
					});
				}
			}
		}
	}

	deps
}

fn extract_version_constraint(spec: &str, name: &str) -> Option<String> {
	let rest = spec.get(name.len()..)?;
	// Skip extras like [cli]
	let after_extras = if rest.starts_with('[') {
		rest.find(']').map_or(rest, |end| &rest[end + 1..])
	} else {
		rest
	};
	let constraint = after_extras.split(';').next().unwrap_or("").trim();
	if constraint.is_empty() {
		None
	} else {
		Some(constraint.to_string())
	}
}

/// Parse a PEP 440 version string as semver where possible.
///
/// Handles common cases like `1.2.3`, `1.2`, `0.1.0`. Ignores PEP 440
/// pre-release suffixes (`a1`, `b2`, `rc1`, `.post1`, `.dev0`) that don't
/// map cleanly to semver.
fn parse_pep440_as_semver(version: &str) -> Option<Version> {
	// Try direct semver parse first
	if let Ok(version) = Version::parse(version) {
		return Some(version);
	}
	// Try adding .0 for two-part versions like "1.2"
	let parts: Vec<&str> = version.split('.').collect();
	match parts.len() {
		2 => {
			let extended = format!("{version}.0");
			Version::parse(&extended).ok()
		}
		_ => None,
	}
}

fn find_all_pyproject_files(root: &Path) -> Vec<PathBuf> {
	WalkDir::new(root)
		.into_iter()
		.filter_entry(should_descend)
		.filter_map(Result::ok)
		.filter(|entry| entry.file_name() == PYPROJECT_FILE)
		.map(DirEntry::into_path)
		.map(|path| normalize_path(&path))
		.collect()
}

fn should_descend(entry: &DirEntry) -> bool {
	let file_name = entry.file_name().to_string_lossy();
	!matches!(
		file_name.as_ref(),
		".git"
			| ".venv" | "venv"
			| "__pycache__"
			| ".mypy_cache"
			| ".ruff_cache"
			| ".pytest_cache"
			| "node_modules"
			| "target"
			| ".devenv"
			| "book" | ".tox"
			| "dist" | "build"
			| ".eggs" | "*.egg-info"
	)
}

/// Return the default dependency-version prefix for this ecosystem.
/// Validate that a Python versioned file contains a readable version field.
///
/// Currently returns `Ok(())` because Python ecosystem files (pyproject.toml)
/// are not validated at the config layer.
pub fn validate_versioned_file(
	_full_path: &Path,
	_display_path: &str,
	_custom_fields: Option<&[String]>,
) -> MonochangeResult<()> {
	Ok(())
}

#[must_use]
pub fn default_dependency_version_prefix() -> &'static str {
	">="
}

/// Return the manifest fields that usually contain dependency versions.
#[must_use]
pub fn default_dependency_fields() -> &'static [&'static str] {
	&["dependencies"]
}
#[cfg(test)]
#[path = "__tests__/lib_tests.rs"]
mod tests;