Skip to content

feat: .as_T cast methods#6100

Open
aaron-ang wants to merge 1 commit intoEventual-Inc:mainfrom
aaron-ang:as-cast
Open

feat: .as_T cast methods#6100
aaron-ang wants to merge 1 commit intoEventual-Inc:mainfrom
aaron-ang:as-cast

Conversation

@aaron-ang
Copy link
Contributor

Changes Made

Use decorator to record datatypes and generate methods at runtime. Also included function headers for intellisense.

Related Issues

Closes #4014.

@github-actions github-actions bot added the feat label Jan 29, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 29, 2026

Greptile Overview

Greptile Summary

This PR adds convenience .as_T() cast methods to the Expression class, providing syntactic sugar for .cast(DataType.T()). The implementation uses a decorator pattern to automatically register all DataType constructor methods at module load time, then dynamically generates corresponding as_* methods on the Expression class.

Key Changes:

  • Added @datatype_constructor decorator in daft/datatype.py to track all DataType constructor methods
  • Generated .as_* methods for all 30+ data types (e.g., .as_string(), .as_int64(), .as_timestamp())
  • Included type stubs in TYPE_CHECKING block for IDE autocomplete and type checking
  • Added comprehensive tests verifying the generated methods produce identical results to manual cast() calls

The implementation is clean and maintainable - adding new DataTypes in the future will automatically generate corresponding .as_* methods without additional code.

Confidence Score: 5/5

  • This PR is safe to merge with no identified issues
  • The implementation is elegant, well-tested, and follows good software engineering practices using decorators and runtime code generation
  • No files require special attention

Important Files Changed

Filename Overview
daft/datatype.py Adds @datatype_constructor decorator to register DataType constructor methods and enables dynamic method generation
daft/expressions/expressions.py Adds as_* cast methods to Expression class with type stubs for IDE support and runtime generation
tests/expressions/test_expressions.py Adds tests verifying as_* methods produce identical results to cast() for simple and parameterized types

Sequence Diagram

sequenceDiagram
    participant User
    participant Expression
    participant DataType
    participant PyDataType
    
    Note over DataType: Module Load Time
    DataType->>DataType: Apply @datatype_constructor to methods
    DataType->>DataType: Populate _DATATYPE_CONSTRUCTOR_SET
    
    Note over Expression: Module Load Time
    Expression->>DataType: Call _constructor_names()
    DataType-->>Expression: Return sorted tuple of constructor names
    Expression->>Expression: Generate as_* methods via setattr
    Expression->>Expression: Create _make_expr_as_dtype_method for each
    
    Note over User: Runtime Usage
    User->>Expression: col("x").as_string()
    Expression->>DataType: DataType.string()
    DataType->>PyDataType: Create PyDataType
    PyDataType-->>DataType: Return PyDataType instance
    DataType-->>Expression: Return DataType wrapper
    Expression->>Expression: self.cast(dtype)
    Expression-->>User: Return casted Expression
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 98.21429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 43.44%. Comparing base (aa8add2) to head (59f5e00).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
daft/datatype.py 97.61% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #6100       +/-   ##
===========================================
- Coverage   72.91%   43.44%   -29.48%     
===========================================
  Files         973      909       -64     
  Lines      126196   112794    -13402     
===========================================
- Hits        92016    49001    -43015     
- Misses      34180    63793    +29613     
Files with missing lines Coverage Δ
daft/expressions/expressions.py 95.19% <100.00%> (+0.07%) ⬆️
daft/datatype.py 93.61% <97.61%> (+0.25%) ⬆️

... and 653 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@universalmind303
Copy link
Member

honestly, i'm not a huge fan of this functionality (as .cast does the same thing), but will defer to others on if we want to merge it or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support .as_T cast methods where T is a Daft type with possible parameters

2 participants