Skip to content

Defer spython imports in singularity.py to where they're used - #2344

Merged
mr-c merged 1 commit into
common-workflow-language:mainfrom
markjschreiber:lazy-import-spython
Aug 28, 2026
Merged

Defer spython imports in singularity.py to where they're used#2344
mr-c merged 1 commit into
common-workflow-language:mainfrom
markjschreiber:lazy-import-spython

Conversation

@markjschreiber

Copy link
Copy Markdown
Contributor

Summary

cwltool/singularity.py imports spython.main.Client, spython.main.parse.parsers.docker.DockerParser, and spython.main.parse.writers.singularity.SingularityWriter at module level. Since cwltool.workflow/cwltool.command_line_tool import cwltool.singularity unconditionally, this means spython (MPL-2.0 licensed) must be installed just to import cwltool at all — even for use cases like cwltool --validate that never touch Singularity.

All three names are only actually used inside SingularityCommandLineJob.get_image's Dockerfile-build branch (building a Singularity image from a dockerFile requirement), never at import time. This PR moves the three imports into that branch, right before first use.

No behavior change for actual Singularity usage — the imports still happen, just lazily, the first time an image actually needs to be built from a Dockerfile.

Verification

  • flake8, black --check, and mypy all pass on the changed file (mypy reports 3 pre-existing, unrelated errors in cwlviewer.py that exist identically on main before this change).
  • pytest tests/test_singularity.py tests/test_singularity_versions.py and the broader -k "singularity or docker" slice: all pass (remaining skips are pre-existing environment gates — no Docker/Singularity/Podman binary available — unrelated to this change).
  • Confirmed import cwltool.singularity now succeeds with spython completely uninstalled, and cwltool --validate runs successfully end-to-end without spython installed.

Motivation

We ran into this while trying to depend on cwltool from an internal Apache-2.0-licensed project that only needs --validate-style validation. Being forced to pull in an MPL-2.0-licensed package for a feature we never use blocked our internal third-party license review. This is a minimal, behavior-preserving fix rather than a request to change cwltool's own dependency policy — happy to adjust the approach if maintainers prefer something different (e.g. a try/except ImportError guard instead of a bare deferred import).

spython was imported at module level, meaning merely importing
cwltool.singularity (which cwltool.workflow/command_line_tool do
unconditionally) required spython to be installed even when Singularity
support is never used, e.g. for `cwltool --validate`. spython itself is
only used inside SingularityCommandLineJob.get_image's Dockerfile-build
branch, so move the three imports there. No behavior change for actual
Singularity usage.
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.24%. Comparing base (9f6fcba) to head (99ddfb5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2344   +/-   ##
=======================================
  Coverage   85.24%   85.24%           
=======================================
  Files          46       46           
  Lines        8622     8622           
  Branches     2020     2020           
=======================================
  Hits         7350     7350           
  Misses        806      806           
  Partials      466      466           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mr-c
mr-c merged commit 2c48ccc into common-workflow-language:main Aug 28, 2026
47 checks passed
@markjschreiber
markjschreiber deleted the lazy-import-spython branch August 31, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants