Skip to content

Fix bare except clauses, torch.load deprecation, and typos#484

Merged
giswqs merged 1 commit intomainfrom
fix/code-quality-improvements
Jan 31, 2026
Merged

Fix bare except clauses, torch.load deprecation, and typos#484
giswqs merged 1 commit intomainfrom
fix/code-quality-improvements

Conversation

@giswqs
Copy link
Member

@giswqs giswqs commented Jan 31, 2026

Summary

This PR makes several small but meaningful code quality improvements:

Changes

  1. Replace bare except: with except Exception: (10 occurrences across common.py and fer.py)

    • Bare except: catches SystemExit, KeyboardInterrupt, and GeneratorExit, which should almost never be silently suppressed
    • Follows PEP 8 guidelines which explicitly recommend against bare except clauses
  2. Add weights_only=False to torch.load() in text_sam.py

    • In PyTorch >= 2.4, torch.load() without weights_only emits a FutureWarning
    • In PyTorch >= 2.6, the default changed to weights_only=True, which would break loading GroundingDINO checkpoints that contain non-tensor data
    • Explicitly passing weights_only=False ensures forward compatibility and silences the warning
  3. Fix typo in pyproject.toml description: Meta AI'Meta AI's (missing s)

  4. Fix package name typo in README.md: Samgeo-geospatialsegment-geospatial

No behavioral changes

All changes are safe refactors — no logic changes, no new dependencies.

- Replace 10 bare 'except:' with 'except Exception:' across common.py and fer.py
  to follow Python best practices (PEP 8) and avoid silently catching SystemExit,
  KeyboardInterrupt, and GeneratorExit
- Add explicit 'weights_only=False' to torch.load() in text_sam.py to suppress
  FutureWarning in PyTorch >= 2.4 where the default will change to weights_only=True
- Fix typo in pyproject.toml description: "Meta AI'" -> "Meta AI's"
- Fix package name typo in README.md: "Samgeo-geospatial" -> "segment-geospatial"
Copilot AI review requested due to automatic review settings January 31, 2026 06:39
@github-actions
Copy link

@github-actions github-actions bot temporarily deployed to pull request January 31, 2026 06:43 Inactive
Copy link
Contributor

Copilot AI left a comment

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 improves code quality and forward-compatibility by tightening exception handling, addressing a torch.load deprecation/default change, and fixing small documentation/metadata typos.

Changes:

  • Replace bare except: clauses with except Exception: in samgeo/common.py and samgeo/fer.py.
  • Make torch.load() behavior explicit in samgeo/text_sam.py to avoid future default changes.
  • Fix typos in pyproject.toml and README.md.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
samgeo/text_sam.py Updates checkpoint loading to pass an explicit weights_only value.
samgeo/fer.py Replaces bare except: with except Exception: in geometry regularization flow.
samgeo/common.py Replaces bare except: with except Exception: in GUI/util paths.
pyproject.toml Fixes project description typo.
README.md Fixes package name typo in conda install notes.

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

repo_id=repo_id, filename=filename, force_filename=filename
)
checkpoint = torch.load(cache_file, map_location="cpu")
checkpoint = torch.load(cache_file, map_location="cpu", weights_only=False)
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

torch.load(..., weights_only=False) will raise a TypeError on PyTorch versions that don’t support the weights_only keyword. Since this repo’s dependencies don’t pin a minimum PyTorch version (e.g., requirements.txt is just torch), consider guarding this call (e.g., via inspect.signature(torch.load) or try/except TypeError fallback) or explicitly constraining the minimum supported PyTorch version.

Suggested change
checkpoint = torch.load(cache_file, map_location="cpu", weights_only=False)
load_sig = inspect.signature(torch.load)
if "weights_only" in load_sig.parameters:
checkpoint = torch.load(cache_file, map_location="cpu", weights_only=False)
else:
checkpoint = torch.load(cache_file, map_location="cpu")

Copilot uses AI. Check for mistakes.
repo_id=repo_id, filename=filename, force_filename=filename
)
checkpoint = torch.load(cache_file, map_location="cpu")
checkpoint = torch.load(cache_file, map_location="cpu", weights_only=False)
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Unconditionally setting weights_only=False forces an unsafe pickle load on newer PyTorch versions where the default is moving toward weights_only=True for security. Because repo_id/filename are caller-provided, consider exposing a weights_only (or unsafe_load) parameter and defaulting to the safer behavior when possible, only opting into weights_only=False for known/trusted checkpoints.

Copilot uses AI. Check for mistakes.
try:
os.remove(filename)
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

PR description says “No behavioral changes”, but switching from bare except: to except Exception: intentionally changes behavior by allowing KeyboardInterrupt/SystemExit to propagate instead of being swallowed. Please update/remove that claim in the PR description so it matches the change rationale described above.

Copilot uses AI. Check for mistakes.
Comment on lines +1835 to 1836
except Exception:
pass
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
pass
except Exception as e:
# Non-critical cleanup failure; log and continue.
print(f"Warning: could not remove temporary file {filename}: {e}")

Copilot uses AI. Check for mistakes.
try:
m.add_raster(sam.source, layer_name="Image")
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
)

except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
except Exception:
# Intentionally ignore errors when adding visualization layers
# (e.g., when localtileserver or client rendering is unavailable).

Copilot uses AI. Check for mistakes.
try:
os.remove(sam.prediction_fp)
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
except Exception:
pass
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
try:
m.add_raster(sam.source, layer_name="Image")
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
except Exception:
# Adding the image layer is optional; if it fails (e.g., no localtileserver),
# we silently continue without the raster layer.

Copilot uses AI. Check for mistakes.
m.remove_layer(m.find_layer(m.layer_name))
m.clear_drawings()
except:
except Exception:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
except Exception:
# Ignore any errors during map cleanup so that the reset action
# always completes, even if layers or drawings are already missing
# or the map state has changed unexpectedly.

Copilot uses AI. Check for mistakes.
@giswqs giswqs merged commit b0d3bda into main Jan 31, 2026
16 checks passed
@giswqs giswqs deleted the fix/code-quality-improvements branch January 31, 2026 15:06
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