Skip to content

Move desimodel footprint imports to just-in-time in io.py - #894

Merged
sbailey merged 5 commits into
mainfrom
copilot/just-in-time-imports-io
Aug 11, 2026
Merged

sbailey merged 5 commits into
mainfrom
copilot/just-in-time-imports-io

Conversation

Copilot AI commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Importing desitarget.io unconditionally triggered a desimodel data-not-installed warning even when no tile-related functions were called, because from desimodel.footprint import is_point_in_desi, tiles2pix sat at module level.

Changes

  • Removed module-level import of is_point_in_desi and tiles2pix from desimodel.footprint
  • Added just-in-time imports inside the three functions that actually use these symbols:
    • read_targets_in_tiles_quick — imported unconditionally at function entry (both symbols always used)
    • read_targets_in_quick — imported inside if shape == 'tiles': branch (only needed for that shape)
    • read_targets_in_tiles — imported before first use (both symbols used on all non-early-return paths)
# Before: triggers desimodel warning on every `import desitarget.io`
from desimodel.footprint import is_point_in_desi, tiles2pix  # line 34, module level

# After: desimodel only loaded when tile-reading functions are actually called
def read_targets_in_tiles_quick(...):
    ...
    from desimodel.footprint import is_point_in_desi, tiles2pix
    pixlist = tiles2pix(nside, tiles=tiles)

Python's import cache (sys.modules) means the deferred import has no repeated overhead after the first call.

Copilot AI linked an issue Jul 23, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Update just-in-time imports in io.py Move desimodel footprint imports to just-in-time in io.py Jul 23, 2026
Copilot AI requested a review from sbailey July 23, 2026 20:33
@sbailey
sbailey marked this pull request as ready for review July 27, 2026 17:54
Copilot AI review requested due to automatic review settings July 27, 2026 17:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents import desitarget.io from unconditionally importing desimodel (and triggering desimodel data-not-installed warnings) by moving desimodel.footprint imports to just-in-time imports inside the tile-reading functions that actually use them.

Changes:

  • Removed the module-level from desimodel.footprint import is_point_in_desi, tiles2pix import.
  • Added deferred (function-scope) imports of is_point_in_desi / tiles2pix within the three tile-based readers that use them.
  • Kept non-tile call paths free of desimodel imports, matching the intent of issue #893.
Comments suppressed due to low confidence (2)

py/desitarget/io.py:4069

  • These comments suggest desimodel is a "required dependency", but this code is actually deferring an optional dependency import until the shape == 'tiles' path is used. Reword (and consider dropping the redundant note) to clarify the intent is avoiding desimodel import at module import time.
        # import desimodel only when needed to avoid required dependency
        # Note: `is_point_in_desi` will be used below

py/desitarget/io.py:4253

  • Comment wording is misleading: desimodel is still required at runtime for tile-based filtering, but the intent is to avoid importing desimodel when desitarget.io is imported. Reword to clarify this is a deferred import of an optional dependency.
    # import only when needed to avoid desimodel required dependency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread py/desitarget/io.py Outdated
@sbailey

sbailey commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

@moustakas please confirm that this branch fixed your fastspecfit case reported in #894 .

Tests are failing due to inability to download DESI_SURVEYOPS shapshot from data.desi.lbl.gov .

@moustakas

Copy link
Copy Markdown
Member

Yes, looks good, thanks.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 40.674% (+5.2%) from 35.469% — copilot/just-in-time-imports-io into main

@sbailey
sbailey merged commit 44768bc into main Aug 11, 2026
9 checks passed
@sbailey
sbailey deleted the copilot/just-in-time-imports-io branch August 11, 2026 22:13
sbailey added a commit that referenced this pull request Aug 11, 2026
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.

just-in-time imports in io.py

5 participants