Skip to content

[FEATURE] Implement ConvertField helper type #29

@AliiiBenn

Description

@AliiiBenn

Description

Implement the ConvertField helper type as mentioned in PEP 827.

Problem

ConvertField is a helper type referenced in PEP 827 for handling ORM-style field conversions. It transforms database field types to their underlying Python types.

Usage Examples

# Convert a Property field - returns the raw Python type
type UserId = ConvertField[User, Literal['id']]
# If id: Property[int], returns: int

# Convert a Link field - returns the related model
type UserProfile = ConvertField[User, Literal['profile']]
# If profile: Link[Profile], returns: Profile

# Convert a MultiLink field - returns list of related models
type UserPosts = ConvertField[User, Literal['posts']]
# If posts: MultiLink[Post], returns: list[Post]

Real-world context

This is designed for Prisma-style ORMs where fields can be:

  • Property[int] → raw Python type (int)
  • Link[User] → related model (User)
  • MultiLink[Post] → list of related models (list[Post])

ConvertField extracts the actual Python type from any ORM field descriptor.

Proposed Implementation

Step 1: Define operator class in typing.py

class ConvertField[T, K]:
    pass

Step 2: Implement evaluator in _eval_operators.py

@type_eval.register_evaluator(ConvertField)
def _eval_ConvertField(tp, key, *, ctx):
    # 1. Get the field type via GetMemberType
    field_type = _eval_types(GetMemberType[tp, key], ctx)
    
    # 2. Extract PointerArg (via GetArg[T, Pointer, 0])
    pointer_arg = _eval_types(GetArg[field_type, Pointer, Literal[0]], ctx)
    
    # 3. If Link/MultiLink, apply AdjustLink with PropsOnly
    if _eval_types(IsAssignable[field_type, Link], ctx):
        props_only = _eval_types(PropsOnly[pointer_arg], ctx)
        return _eval_types(AdjustLink[props_only, field_type], ctx)
    
    # 4. Otherwise return raw type
    return pointer_arg

Step 3: Export in typemap_extensions/init.py

from typemap.typing import ConvertField

Dependencies

  • Uses existing GetMemberType, GetArg, IsAssignable
  • Uses AdjustLink and PropsOnly (can be implemented first)

Priority

  • priority:critical
  • priority:high
  • priority:medium
  • priority:low

Estimated Effort

  • effort:xs
  • effort:s
  • effort:m
  • effort:l
  • effort:xl

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions