Skip to content

Conversation

@jhamon
Copy link
Collaborator

@jhamon jhamon commented Nov 17, 2025

Comprehensive Type Hints Implementation

Summary

This PR implements comprehensive type hints throughout the Pinecone Python SDK, addressing customer requests for better type safety and IDE support. The changes include type annotations for all public methods, decorators, helper functions, and generated code templates, while adopting modern Python 3.10+ type syntax.

Problem

The SDK lacked comprehensive type hints, making it difficult for:

  • IDEs to provide accurate autocomplete and type checking
  • Static type checkers (like mypy) to validate code correctness
  • Developers to understand expected parameter and return types
  • Customers who rely on type hints for better development experience

Solution

Implemented comprehensive type hints across the entire SDK, including:

  • Return type annotations for all public methods in Pinecone, PineconeAsyncio, Index, IndexAsyncio, and IndexGRPC
  • Type hints for decorators using ParamSpec and TypeVar for proper signature preservation
  • Context manager type hints (__enter__, __exit__, __aenter__, __aexit__)
  • Generator and async generator return types
  • Helper function type annotations
  • Modern Python 3.10+ syntax (X | Y instead of Union[X, Y], X | None instead of Optional[X])
  • Improved OpenAPI code generation templates with return type annotations and better type inference

Key Changes

Core Client Classes

  • pinecone/pinecone.py: Added return types to all methods (create_index, delete_index, list_indexes, etc.)
  • pinecone/pinecone_asyncio.py: Added return types to all async methods and context manager support
  • pinecone/db_data/index.py: Added return types to all data operations (upsert, query, fetch, delete, etc.)
  • pinecone/db_data/index_asyncio.py: Added return types to all async data operations
  • pinecone/grpc/index_grpc.py: Added return types to all gRPC operations

Decorators

  • pinecone/utils/require_kwargs.py: Added ParamSpec and TypeVar for proper type preservation
  • pinecone/utils/error_handling.py: Added ParamSpec and TypeVar with proper signature handling

Factory Methods

  • pinecone/db_data/request_factory.py: Added explicit type annotations to all factory methods
  • pinecone/db_data/vector_factory.py: Added type annotations and support for VectorTupleWithMetadata
  • pinecone/db_data/sparse_values_factory.py: Added type annotations to helper methods
  • pinecone/db_data/resources/sync/bulk_import_request_factory.py: Added type annotations

Helper Functions

  • pinecone/utils/check_kwargs.py: Added type annotations (Callable[..., Any], set[str], None)
  • pinecone/db_data/sparse_values_factory.py: Added type annotations to _convert_to_list and _validate_list_items_type
  • pinecone/db_data/request_factory.py: Added type annotations to vec_builder, _parse_search_rerank, and upsert_records_args

Modern Type Syntax

  • Replaced Union[X, Y] with X | Y syntax (PEP 604)
  • Replaced Optional[X] with X | None syntax
  • Added from __future__ import annotations to support deferred evaluation for Python 3.9 compatibility

Type Configuration

  • mypy.ini: Configured mypy with gradual strictness settings
  • Added ignore_missing_imports for optional dependencies (grpc, aiohttp, aiohttp_retry, urllib3, tqdm)
  • Removed unnecessary ignore_errors and ignore_missing_imports settings after fixing underlying type issues
  • All pinecone.openapi_support.* modules now pass mypy without ignores

Interface Alignment

  • Updated pinecone/db_data/index_asyncio_interface.py to match implementation signatures
  • Updated pinecone/db_data/interfaces.py to use modern type syntax and correct types
  • Fixed method signature mismatches between interfaces and implementations
  • Aligned AsyncIterator imports between interface and implementation (collections.abc vs typing)

OpenAPI Support Module Improvements

  • pinecone/openapi_support/model_utils.py:
    • Fixed get_possible_classes() to handle Any (typing special form, not a class)
    • Fixed get_required_type_classes() to handle typing generics (Dict[str, Any], List[T], Tuple[T, ...])
    • Added is_valid_type() check for Any to accept any type when Any is in valid classes
    • Fixed type validation for nested dict values with Any types
    • Added proper handling of typing generics by normalizing to built-in types
  • pinecone/openapi_support/serializer.py: Fixed return type handling for file data
  • pinecone/openapi_support/api_client_utils.py: Fixed type annotations for multipart parameters
  • pinecone/openapi_support/rest_urllib3.py: Added explicit type for request_body
  • pinecone/openapi_support/asyncio_api_client.py: Fixed _check_type parameter handling and dynamic attribute assignment
  • pinecone/openapi_support/api_client.py: Fixed _check_type parameter handling and dynamic attribute assignment
  • pinecone/openapi_support/endpoint_utils.py: Fixed type annotation for validations parameter

Data Class Improvements

  • pinecone/db_data/dataclasses/fetch_response.py: Changed usage from Dict[str, int] to Optional[Usage] to align with OpenAPI model
  • pinecone/db_data/dataclasses/fetch_by_metadata_response.py: Changed usage from Dict[str, int] to Optional[Usage] for consistency
  • pinecone/grpc/utils.py: Updated parsing functions to pass Usage object directly instead of converting to dict

User-Facing Impact

Benefits

  • Better IDE Support: IDEs can now provide accurate autocomplete, parameter hints, and type checking
  • Static Type Checking: Developers can use mypy or other type checkers to catch type errors before runtime
  • Improved Documentation: Type hints serve as inline documentation for expected types
  • Better Developer Experience: Clearer understanding of API contracts and expected types

Breaking Changes

None - All changes are additive. Existing code continues to work without modification.

Migration Guide

No migration required. The type hints are purely additive and don't change runtime behavior.

Technical Details

Type Inference Improvements

  • Added explicit type annotations to factory methods to help mypy infer return types from OpenAPI model constructors
  • Improved Deserializer.deserialize() with generic typing for better type inference
  • Added __new__ method to generated models for better constructor type inference

Type Safety

  • All public methods now have return type annotations
  • Context managers properly typed for with and async with statements
  • Generators and async generators have proper return types
  • Decorators preserve function signatures using ParamSpec

Compatibility

  • Runtime requires Python 3.10+ (as per existing requirements)
  • All type hints are forward-compatible and don't affect runtime performance

Testing

  • All existing tests pass (388 unit tests)
  • Mypy type checking passes with comprehensive coverage

Files Changed

  • 182 files changed: Core client classes, data operations, gRPC operations, utilities, generated code templates, and configuration files
  • 2,313 insertions, 600 deletions: Net addition of comprehensive type annotations

Technical Achievements

Reduced Any Usage

  • Eliminated Any return types from generated API methods by adding explicit return type annotations
  • Reduced casting needed in client code through better type inference in generated models
  • Fixed "Returning Any" errors by adding explicit type annotations to factory methods and helper functions

Code Generation Quality

  • Generated code now includes proper return type annotations for all API methods
  • Post-processing steps ensure generated code passes mypy without manual fixes
  • Template improvements reduce the need for manual type annotations in client code

Future Work

  • Continue reducing Any usage in edge cases
  • Consider adding runtime type validation for critical paths (optional, behind a flag)
  • Monitor and improve type coverage as the codebase evolves

@jhamon jhamon marked this pull request as ready for review November 17, 2025 08:01
@jhamon jhamon merged commit c6392fa into release-candidate/2025-10 Nov 17, 2025
27 checks passed
@jhamon jhamon deleted the jhamon/more-types branch November 17, 2025 08:01
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