Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow for date/datetime subclasses (e.g. pd.Timestamp, FreezeGun) in pl.lit #18497

Merged
merged 4 commits into from
Sep 7, 2024

Conversation

MarcoGorelli
Copy link
Collaborator

closes #18470
closes #18447

@MarcoGorelli MarcoGorelli changed the title fix: allow for date/datetime subclasses (e.g. pd.Timestamp, FreezeGun) in pl.lit fix: Allow for date/datetime subclasses (e.g. pd.Timestamp, FreezeGun) in pl.lit Aug 31, 2024
@github-actions github-actions bot added title needs formatting fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Aug 31, 2024
Copy link

codecov bot commented Aug 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.82%. Comparing base (e4746a5) to head (0d1347d).
Report is 19 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18497      +/-   ##
==========================================
- Coverage   79.82%   79.82%   -0.01%     
==========================================
  Files        1502     1502              
  Lines      201933   201942       +9     
  Branches     2868     2868              
==========================================
- Hits       161201   161193       -8     
- Misses      40186    40203      +17     
  Partials      546      546              

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

Comment on lines +445 to +453
Python::with_gil(|py| {
// One final attempt before erroring. Do we have a date/datetime subclass?
// E.g. pd.Timestamp, or Freezegun.
let datetime_module = PyModule::import_bound(py, "datetime")?;
let datetime_class = datetime_module.getattr("datetime")?;
let date_class = datetime_module.getattr("date")?;
if value.is_instance(&datetime_class)? || value.is_instance(&date_class)? {
let av = py_object_to_any_value(value, true)?;
Ok(Expr::Literal(LiteralValue::try_from(av).unwrap()).into())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pyo3 does have PyDateTime, but it's limited to:

Available on non-Py_LIMITED_API only

Copy link
Member

Choose a reason for hiding this comment

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

This should come before the allow_object check.

I am thinking aloud here. Why don't we go via AnyValue directly for all branches? This seems like code duplication.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yup, thanks for catching that, and sorry for not having spotted it myself

fixed, and I've added a test which would've on my previous commit to make sure this doesn't regress

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why don't we go via AnyValue directly for all branches?

Tried then, but then a float would become

[crates/polars-python/src/functions/lazy.rs:429:9] &newret = Ok(
    PyExpr {
        inner: 2.0,
    },
)

instead of

[crates/polars-python/src/functions/lazy.rs:432:9] &oldret = Ok(
    PyExpr {
        inner: dyn float: 2.0,
    },
)

, and I haven't (yet) dived deeper here

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yeap.. That's a good reason.

@mcrumiller
Copy link
Contributor

Ahh sorry Marco dropped the ball on this one. I was looking for a way to avoid acquiring the gil again since py_object_to_any_value does this check already when it traverses the ancestor types.

@MarcoGorelli MarcoGorelli marked this pull request as draft September 2, 2024 09:57
@MarcoGorelli MarcoGorelli marked this pull request as ready for review September 5, 2024 11:32
@ritchie46 ritchie46 merged commit 5d7f41e into pola-rs:main Sep 7, 2024
26 checks passed
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 rust Related to Rust Polars
Projects
None yet
4 participants