Skip to content

Fix: Fix Shapely import#584

Merged
adrien-berchet merged 3 commits intogeoalchemy:mainfrom
adrien-berchet:fix_shapely_typehint
Feb 20, 2026
Merged

Fix: Fix Shapely import#584
adrien-berchet merged 3 commits intogeoalchemy:mainfrom
adrien-berchet:fix_shapely_typehint

Conversation

@adrien-berchet
Copy link
Copy Markdown
Member

Fixes #583

Copy link
Copy Markdown

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

Fixes #583 by preventing geoalchemy2 from failing to import when Shapely is not installed, while keeping to_shape/from_shape guarded behind the optional dependency check.

Changes:

  • Add a TYPE_CHECKING-aware Shapely geometry type alias/fallback to avoid evaluating shapely.* in annotations when Shapely isn’t installed.
  • Update to_shape/from_shape type annotations to use the new alias instead of shapely.Geometry.

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

import shapely
import shapely.wkb
import shapely.wkt
from shapely import Geometry as ShapelyGeometry
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

from shapely import Geometry as ShapelyGeometry is likely incompatible with the Shapely versions this repo supports (tox installs Shapely>=1.3.0, and optional deps declare Shapely>=1.7). In Shapely 1.x there is no top-level Geometry, so this will raise ImportError even when Shapely is installed, incorrectly setting HAS_SHAPELY=False and breaking to_shape/from_shape. Import the geometry base type from shapely.geometry.base (e.g., BaseGeometry) or otherwise use a version-compatible symbol for the annotation without causing the Shapely import block to fail.

Suggested change
from shapely import Geometry as ShapelyGeometry
from shapely.geometry.base import BaseGeometry as ShapelyGeometry

Copilot uses AI. Check for mistakes.
Comment on lines 41 to 45


@check_shapely()
def to_shape(element: WKBElement | WKTElement) -> shapely.Geometry:
def to_shape(element: WKBElement | WKTElement) -> ShapelyGeometry:
"""Function to convert a :class:`geoalchemy2.types.SpatialElement` to a Shapely geometry.
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


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

Comment on lines +21 to +24
if not TYPE_CHECKING:

class ShapelyGeometry:
"""Requires shapely. Install with: pip install geoalchemy2[shapely]."""
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

The if not TYPE_CHECKING: guard prevents the stub class from being defined during static type checking. When type checkers analyze code without shapely installed (or without access to shapely's type stubs), they will encounter a NameError for ShapelyGeometry in the function signatures.

Consider removing the if not TYPE_CHECKING: condition and defining the stub class unconditionally in the except block. This ensures the stub is available in both runtime and static analysis contexts.

Suggested change
if not TYPE_CHECKING:
class ShapelyGeometry:
"""Requires shapely. Install with: pip install geoalchemy2[shapely]."""
class ShapelyGeometry:
"""Requires shapely. Install with: pip install geoalchemy2[shapely]."""

Copilot uses AI. Check for mistakes.
@adrien-berchet adrien-berchet merged commit 55ab873 into geoalchemy:main Feb 20, 2026
12 checks passed
@adrien-berchet adrien-berchet deleted the fix_shapely_typehint branch February 20, 2026 23:44
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.

[Bug report] Cannot import after release 0.18.2 if shapely is not installed

2 participants