Fix bare except clauses, torch.load deprecation, and typos#484
Conversation
- 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"
|
🚀 Deployed on https://697da4947a017ce755fb417a--opengeos.netlify.app |
There was a problem hiding this comment.
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 withexcept Exception:insamgeo/common.pyandsamgeo/fer.py. - Make
torch.load()behavior explicit insamgeo/text_sam.pyto avoid future default changes. - Fix typos in
pyproject.tomlandREADME.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) |
There was a problem hiding this comment.
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.
| 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") |
| 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) |
There was a problem hiding this comment.
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.
| try: | ||
| os.remove(filename) | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
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.
| except Exception: | ||
| pass |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except Exception: | |
| pass | |
| except Exception as e: | |
| # Non-critical cleanup failure; log and continue. | |
| print(f"Warning: could not remove temporary file {filename}: {e}") |
| try: | ||
| m.add_raster(sam.source, layer_name="Image") | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| ) | ||
|
|
||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except Exception: | |
| except Exception: | |
| # Intentionally ignore errors when adding visualization layers | |
| # (e.g., when localtileserver or client rendering is unavailable). |
| try: | ||
| os.remove(sam.prediction_fp) | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except Exception: | ||
| pass | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| try: | ||
| m.add_raster(sam.source, layer_name="Image") | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except Exception: | |
| except Exception: | |
| # Adding the image layer is optional; if it fails (e.g., no localtileserver), | |
| # we silently continue without the raster layer. |
| m.remove_layer(m.find_layer(m.layer_name)) | ||
| m.clear_drawings() | ||
| except: | ||
| except Exception: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| 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. |
Summary
This PR makes several small but meaningful code quality improvements:
Changes
Replace bare
except:withexcept Exception:(10 occurrences acrosscommon.pyandfer.py)except:catchesSystemExit,KeyboardInterrupt, andGeneratorExit, which should almost never be silently suppressedAdd
weights_only=Falsetotorch.load()intext_sam.pytorch.load()withoutweights_onlyemits aFutureWarningweights_only=True, which would break loading GroundingDINO checkpoints that contain non-tensor dataweights_only=Falseensures forward compatibility and silences the warningFix typo in
pyproject.tomldescription:Meta AI'→Meta AI's(missings)Fix package name typo in
README.md:Samgeo-geospatial→segment-geospatialNo behavioral changes
All changes are safe refactors — no logic changes, no new dependencies.