You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
❌ 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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes Made
Use decorator to record datatypes and generate methods at runtime. Also included function headers for intellisense.
Related Issues
Closes #4014.