Skip to content

Fix: NameError when importing geoalchemy2.shape without Shapely installed#586

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/add-import-test-geoalchemy2
Closed

Fix: NameError when importing geoalchemy2.shape without Shapely installed#586
Copilot wants to merge 2 commits intomainfrom
copilot/add-import-test-geoalchemy2

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 20, 2026

geoalchemy2.shape raised NameError: name 'shapely' is not defined on import when Shapely was not installed, because shapely.Geometry was used as an eager type annotation while shapely was not bound in the module namespace.

Changes

  • geoalchemy2/shape.py: Replace bare shapely.Geometry type hints with a ShapelyGeometry alias that is always defined — resolved from shapely.Geometry when Shapely is available, or a no-op stub class at runtime when it isn't (gated with if not TYPE_CHECKING: to avoid confusing static analyzers).
  • tests/test_shape.py: Add test_import_without_shapely regression test that blocks all shapely.* entries in sys.modules, reloads geoalchemy2.shape, and asserts it loads cleanly with HAS_SHAPELY = False, then restores state.
# Previously raised NameError at import time when Shapely was absent:
# -> shapely.Geometry was evaluated eagerly but shapely was unbound

# Now works correctly:
import sys
sys.modules["shapely"] = None  # simulate missing Shapely
import importlib, geoalchemy2.shape
importlib.reload(geoalchemy2.shape)
assert not geoalchemy2.shape.HAS_SHAPELY  # no NameError

Checklist

This pull request is:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • Please include tests that fail with the main branch and pass with the provided fix.
  • A new feature implementation or update an existing feature
    • Please include tests that cover every lines of the new/updated feature.
    • Please update the documentation to describe the new/updated feature.
Original prompt

This section details on the original issue you should resolve

<issue_title>Create a test to ensure GeoAlchemy2 can be imported when Shapely is not installed</issue_title>
<issue_description>> This change fixes the runtime NameError when Shapely isn't installed, but there’s no test ensuring import geoalchemy2 (or import geoalchemy2.shape) works in an environment without Shapely. Consider adding a regression test that imports/reloads geoalchemy2.shape with Shapely unavailable (e.g., via an import hook or sys.modules patch) so this doesn’t regress again.

Originally posted by @copilot in #584</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: adrien-berchet <11536226+adrien-berchet@users.noreply.github.com>
Copilot AI changed the title [WIP] Add test for GeoAlchemy2 import without Shapely Fix: NameError when importing geoalchemy2.shape without Shapely installed Feb 20, 2026
Copilot AI requested a review from adrien-berchet February 20, 2026 15:13
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.

Create a test to ensure GeoAlchemy2 can be imported when Shapely is not installed

2 participants