Typing Stubs for Shapely

2.1.0.20260408 · active · verified Tue Apr 14

types-shapely provides PEP 561-compliant typing stubs for the Shapely library, enabling static type checking tools like MyPy and Pyright to analyze code that uses Shapely. It is part of the broader typeshed project and aims to provide accurate annotations for Shapely versions matching `2.1.*`. The package is actively maintained within the typeshed ecosystem, with new releases reflecting updates to the upstream Shapely library's API or improvements in type fidelity.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `types-shapely` to enable static type checking for `shapely` code. After installing `shapely` and `types-shapely`, you can write your Python code with type annotations for Shapely objects. A type checker like MyPy will then use the provided stubs to validate your code.

import sys
import os

# Ensure shapely and types-shapely are installed
# For demonstration, we use a mock shell command
# In a real scenario, these would be in requirements.txt or pre-installed
if os.environ.get('SKIP_INSTALL', 'false').lower() != 'true':
    # This part is for demonstration only to show expected installs.
    # In a real environment, you'd just install via pip before running mypy.
    # Example of how you'd install (not executed directly here for safety):
    # print("Installing shapely and types-shapely (mock)...\n")
    # os.system(f'{sys.executable} -m pip install shapely types-shapely mypy')
    pass

from shapely.geometry import Point, Polygon
from typing import List, Tuple

def create_and_buffer_point(x: float, y: float, radius: float) -> Polygon:
    """Creates a point and buffers it."""
    point: Point = Point(x, y)
    buffered_polygon: Polygon = point.buffer(radius)
    return buffered_polygon

def get_polygon_area(poly: Polygon) -> float:
    """Returns the area of a polygon."""
    return poly.area

# Example usage
my_point = Point(0.0, 0.0)
my_polygon = create_and_buffer_point(0.0, 0.0, 10.0)
area = get_polygon_area(my_polygon)
print(f"Original point: {my_point}")
print(f"Buffered polygon: {my_polygon}")
print(f"Polygon area: {area}")

# To run type checking using mypy:
# 1. Save the above code as `main.py`
# 2. Ensure `shapely`, `types-shapely`, and `mypy` are installed:
#    `pip install shapely types-shapely mypy`
# 3. Run mypy from your terminal:
#    `mypy main.py`
# Expected output (if no type errors):
#    `Success: No issues found in 1 source file`

view raw JSON →