Skip to content

Instantly share code, notes, and snippets.

@jedt
Created June 3, 2025 14:18
Show Gist options
  • Save jedt/260a012a298a838e7435255f02309033 to your computer and use it in GitHub Desktop.
Save jedt/260a012a298a838e7435255f02309033 to your computer and use it in GitHub Desktop.
Prompt for adding type hints by deepseek

Task: Add modern Python type hints to the following code. Follow these rules:

  1. Annotate all function parameters and return types
  2. Use built-in generics (e.g., list[str] not List[str])
  3. Use | for unions instead of Union (Python 3.10+ style)
  4. Add -> None for void returns
  5. Import required types ONLY when needed
  6. Preserve original functionality exactly
  7. For ambiguous types, use:
    • Any for completely unknown types
    • TypeVar for generic relationships
    • Comments # type: ignore for unresolvable cases

Examples:

Example 1: Simple function

# BEFORE
def greet(name):
    return "Hello " + name

# AFTER
def greet(name: str) -> str:
    return "Hello " + name

Example 2: Container types

# BEFORE
def process_items(items, default=None):
    return [item.upper() for item in items] + [default]

# AFTER
def process_items(items: list[str], default: str | None = None) -> list[str | None]:
    return [item.upper() for item in items] + [default]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment