typemap

Pick

Select specific fields from a type

Pick only certain fields from a type, excluding all others.

Quick Example

import typemap_extensions as tm
from typemap import eval_typing

class User:
    id: int
    name: str
    email: str
    password_hash: str

# Pick only id and name
PublicUser = eval_typing(tm.Pick[User, tuple["id", "name"]])
# Results in: class with id: int, name: str

Signature

class Pick[T, K]:
    """Pick specific fields from a type."""

Parameters

ParameterTypeDescription
TtypeSource type
KtupleFields to pick (as Literals)

Usage

Basic Pick

class Product:
    id: int
    name: str
    price: float
    description: str
    created_at: datetime

# Only keep what's needed for a list view
ProductList = eval_typing(tm.Pick[Product, tuple["id", "name", "price"]])

Single Field

# Pick just one field
JustName = eval_typing(tm.Pick[User, tuple["name"]])

Use Cases

  • API responses - Exclude sensitive fields (passwords, hashes)
  • List views - Only include display fields
  • Forms - Select fields to show in a form

TypeScript Comparison

// TypeScript
type PublicUser = Pick<User, "id" | "name">;

// typemap
PublicUser = eval_typing(tm.Pick[User, tuple["id", "name"]])

See Also

  • Omit - Exclude specific fields instead

On this page