* Compute the `requires-python` constraints for a package.
( manifest: PythonManifest | undefined, workspaceManifest: PythonManifest | undefined, configs: PythonConfigs[] )
| 314 | * Compute the `requires-python` constraints for a package. |
| 315 | */ |
| 316 | function computeRequiresPython( |
| 317 | manifest: PythonManifest | undefined, |
| 318 | workspaceManifest: PythonManifest | undefined, |
| 319 | configs: PythonConfigs[] |
| 320 | ): PythonConstraint[] { |
| 321 | const constraints: PythonConstraint[] = []; |
| 322 | |
| 323 | // Check for `.python-version` between manifest and workspace. |
| 324 | // When present, it takes priority over `requires-python`. |
| 325 | let hasPythonVersionFile = false; |
| 326 | for (const configSet of configs) { |
| 327 | const pythonVersionConfig = configSet[PythonConfigKind.PythonVersion]; |
| 328 | if (pythonVersionConfig !== undefined) { |
| 329 | constraints.push({ |
| 330 | request: pythonVersionConfig.data, |
| 331 | source: pythonVersionConfig.path, |
| 332 | prettySource: pythonVersionConfig.path, |
| 333 | specifier: pythonVersionConfig.specifier, |
| 334 | }); |
| 335 | hasPythonVersionFile = true; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Check `requires-python` from nearest package pyproject.toml, |
| 341 | // but only if no `.python-version` file was found (it takes priority). |
| 342 | if (!hasPythonVersionFile) { |
| 343 | const manifestRequiresPython = manifest?.data.project?.['requires-python']; |
| 344 | if (manifestRequiresPython) { |
| 345 | const parsed = parsePep440Constraint(manifestRequiresPython); |
| 346 | if (parsed?.length) { |
| 347 | const request = pythonRequestFromConstraint(parsed); |
| 348 | constraints.push({ |
| 349 | request: [request], |
| 350 | source: manifest.path, |
| 351 | prettySource: `"requires-python" key in ${manifest.path}`, |
| 352 | specifier: manifestRequiresPython, |
| 353 | }); |
| 354 | } |
| 355 | } else { |
| 356 | // Check `requires-python` from workspace pyproject.toml |
| 357 | const workspaceRequiresPython = |
| 358 | workspaceManifest?.data.project?.['requires-python']; |
| 359 | if (workspaceRequiresPython) { |
| 360 | const parsed = parsePep440Constraint(workspaceRequiresPython); |
| 361 | if (parsed?.length) { |
| 362 | const request = pythonRequestFromConstraint(parsed); |
| 363 | constraints.push({ |
| 364 | request: [request], |
| 365 | source: workspaceManifest.path, |
| 366 | prettySource: `"requires-python" key in ${workspaceManifest.path}`, |
| 367 | specifier: workspaceRequiresPython, |
| 368 | }); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 |
no test coverage detected