Skip to content

Improve type annotations with TypedDict for API configuration #44

@gawbul

Description

@gawbul

Priority: Low (Enhancement)

Issue

The API configuration dictionary uses generic dict[str, Any] types, which doesn't provide type safety or IDE autocomplete for API endpoint definitions.

Current Code

ensembl_api_table: dict[str, Any] = {
    "getArchiveById": {
        "doc": "Uses the given identifier to return its latest version",
        "url": "/archive/id/{{id}}",
        "method": "GET",
        "content_type": "application/json",
    },
    # ... many more
}

Recommendation

Create TypedDict for better type safety:

from typing import TypedDict, Literal, NotRequired

class APIEndpoint(TypedDict):
    """Definition of an Ensembl REST API endpoint."""
    doc: str
    url: str
    method: Literal["GET", "POST"]
    content_type: str
    post_parameters: NotRequired[list[str]]  # Only for POST endpoints

ensembl_api_table: dict[str, APIEndpoint] = {
    "getArchiveById": {
        "doc": "Uses the given identifier to return its latest version",
        "url": "/archive/id/{{id}}",
        "method": "GET",
        "content_type": "application/json",
    },
    # ... rest
}

Benefits

  • Better IDE autocomplete
  • Catch errors at type-check time
  • Self-documenting API structure
  • Easier for contributors to understand schema

Files to Update

  • pyensemblrest/ensembl_config.py - Add TypedDict and update type hints
  • pyensemblrest/ensemblrest.py - Update method signatures

Testing

  • Run mypy: poetry run mypy pyensemblrest/
  • Ensure no type errors introduced
  • Verify IDE autocomplete works

Notes

Requires Python 3.10+ for NotRequired (already minimum version)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions