Skip to content

fix(python): Allow DataTypeExpr in pl.lit()#26740

Merged
nameexhaustion merged 3 commits intopola-rs:mainfrom
Kevin-Patyk:fix/datatypexpr_pl_lit
Mar 13, 2026
Merged

fix(python): Allow DataTypeExpr in pl.lit()#26740
nameexhaustion merged 3 commits intopola-rs:mainfrom
Kevin-Patyk:fix/datatypexpr_pl_lit

Conversation

@Kevin-Patyk
Copy link
Copy Markdown
Contributor

@Kevin-Patyk Kevin-Patyk commented Feb 27, 2026

Resolves #26644.

Implemented the fix on the Python side since the Rust function pub fn lit(...) in crates/polars-python/src/functions/lazy.rs does not contain an equivalent dtype argument.

Used DataTypeExpr.collect_dtype() to resolve the DataTypeExpr into a concrete DataType prior to running the rest of the function. Passed an empty schema ({}) to .collect_dtype() since it seems like it can resolve itself (there were many examples of just passing {} to .collect_dtype() in the method docstrings located in Class DataTypeExpr).

@github-actions github-actions Bot added fix Bug fix python Related to Python Polars labels Feb 27, 2026
@coastalwhite
Copy link
Copy Markdown
Collaborator

I think this is the wrong solution and would break on even the most trivial of DataTypeExprs and the cases where you would actually want to use it. For example:

pl.lit(1, pl.dtype_of(pl.col.a))

I laid out some possible solutions in #26644.

@Kevin-Patyk
Copy link
Copy Markdown
Contributor Author

@coastalwhite Thanks! Will check out the issue. Wasn't sure if this was the proper fix myself

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.74%. Comparing base (032a765) to head (8862d3d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26740      +/-   ##
==========================================
+ Coverage   81.72%   81.74%   +0.01%     
==========================================
  Files        1806     1806              
  Lines      248702   248705       +3     
  Branches     3132     3133       +1     
==========================================
+ Hits       203257   203295      +38     
+ Misses      44640    44605      -35     
  Partials      805      805              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@nameexhaustion nameexhaustion left a comment

Choose a reason for hiding this comment

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

IIUC this used to work with a cast - can we just add this casting back when we get a DataTypeExpr?

@Kevin-Patyk Kevin-Patyk force-pushed the fix/datatypexpr_pl_lit branch from 39b14cc to 8862d3d Compare March 13, 2026 09:41
@Kevin-Patyk
Copy link
Copy Markdown
Contributor Author

Kevin-Patyk commented Mar 13, 2026

Updated it to call return lit(value).cast(DataTypeExpr). Also rebased 👍

@nameexhaustion nameexhaustion merged commit 620e724 into pola-rs:main Mar 13, 2026
21 checks passed
ritchie46 pushed a commit that referenced this pull request Mar 16, 2026
davanstrien pushed a commit to davanstrien/polars that referenced this pull request Apr 2, 2026
@Kevin-Patyk Kevin-Patyk deleted the fix/datatypexpr_pl_lit branch April 27, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix python Related to Python Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow DataTypeExpr in pl.lit dtype argument

3 participants