Skip to content

refactor: replace os.path with pathlib across libraries and workspaces #1257

@Jammy2211

Description

@Jammy2211

Overview

Migrate all source code and workspaces from os.path.* (notably os.path.join)
to pathlib.Path. The codebase already uses pathlib extensively in newer code,
but ~99 legacy os.path.* references remain across 10 repos. This is a
mechanical refactor for consistency — no behaviour changes.

Plan

  • Replace os.path.join(a, b) with Path(a) / b
  • Replace os.path.exists / isdir / isfile with Path equivalents
  • Replace os.path.dirname(os.path.realpath(__file__)) with Path(__file__).parent.resolve()
  • Replace os.path.basename / split / splitext with .name / .parent / .suffix
  • Wrap with str(...) only where a downstream API requires a string
  • Drop now-unused import os lines
  • Verify per-repo with unit tests; verify workspaces with /smoke_test
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)
  • PyAutoConf
  • PyAutoArray
  • PyAutoGalaxy
  • PyAutoLens
  • autogalaxy_workspace
  • autolens_workspace
  • autolens_workspace_developer
  • autofit_workspace_developer
  • euclid_strong_lens_modeling_pipeline

Work Classification

Both (libraries first, workspaces follow)

Branch Survey

Repository Current Branch Dirty?
PyAutoConf main clean
PyAutoFit main clean
PyAutoArray main clean
PyAutoGalaxy main clean
PyAutoLens main clean
autogalaxy_workspace main 88 changed (unrelated dataset regen by smoke-test-optimization)
autolens_workspace main 148 changed (unrelated dataset regen by smoke-test-optimization)
autolens_workspace_developer main TBD
autofit_workspace_developer main TBD
euclid_strong_lens_modeling_pipeline main TBD

Suggested branch: feature/use-pathlib
Worktree root: ~/Code/PyAutoLabs-wt/use-pathlib/ (created later by /start_library)

Implementation Steps

  1. Refactor each library in dependency order: PyAutoConf → PyAutoFit → PyAutoArray → PyAutoGalaxy → PyAutoLens. Run unit tests after each.
  2. Refactor workspaces: autogalaxy_workspace, autolens_workspace, autolens_workspace_developer, autofit_workspace_developer, euclid_strong_lens_modeling_pipeline. Run /smoke_test to validate.
  3. Resolve workspace dirty state in autogalaxy_workspace and autolens_workspace before cutting workspace branches (coordinate with smoke-test-optimization task).
  4. Ship via /ship_library then /ship_workspace.

Key Files

  • PyAutoConf/autoconf/directory_config.pyos.path.isdir checks
  • PyAutoConf/autoconf/setup_colab.pyos.path.join for workspace paths
  • PyAutoConf/autoconf/json_prior/generate.pyos.path.exists
  • PyAutoConf/test_autoconf/test_fitsable.py — many os.path.join in test data
  • PyAutoFit/autofit/non_linear/paths/directory.pyos.path.exists/isfile
  • PyAutoFit/autofit/non_linear/paths/abstract.pyos.path.exists
  • PyAutoFit/autofit/non_linear/search/**/search.py — checkpoint file checks
  • PyAutoFit/autofit/mapper/model_mapper.pyos.path.dirname(os.path.realpath(__file__))
  • PyAutoFit/autofit/tools/util.pyos.path.join, os.path.split, os.path.exists
  • PyAutoFit/autofit/database/__init__.pyos.path.exists
  • PyAutoArray/autoarray/util/dataset_util.py, autoarray/plot/utils.py, 4 test files
  • PyAutoGalaxy/autogalaxy/plot/plot_utils.py, autogalaxy/util/plot_utils.py, 1 test file
  • PyAutoLens/... — 19 references (largest by count)
  • autolens_workspace/scripts/imaging/features/advanced/subhalo/sensitivity/slam_source_*.py
  • autolens_workspace/scripts/interferometer/casa_reduction.py
  • All docs/conf.py (sphinx boilerplate sys.path.insert(0, os.path.abspath(".")) — converted too, per "no more os.path")

Risks / edge cases

  • External libraries that may need str (some FITS readers in older versions) — wrap with str() where needed.
  • os.path.realpath resolves symlinks; Path.resolve() is equivalent in Python 3.6+.
  • Test fixtures: pytest's tmp_path is already a Path, aligns naturally.
  • The 88 + 148 dirty files in autogalaxy_workspace / autolens_workspace are dataset binaries from in-progress smoke-test-optimization — handle separately at workspace-phase time.

Original Prompt

Click to expand starting prompt

Most examples now use Pathlib, but there are still some os.path.join uses from
legacy.

Can you update all source code and workspaces to not use path.join at all
and always use Pathlib?

This should aspan all source code, workspaces, etc, no more os.path!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions