Skip to content

feat(typing): Add IntoDType alias #2654

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

Merged
merged 13 commits into from
Jun 12, 2025
Merged

feat(typing): Add IntoDType alias #2654

merged 13 commits into from
Jun 12, 2025

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Jun 7, 2025

Closes #2626

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

I think selectors are safe as-is, but will follow that up separately if needed

MarcoGorelli and others added 6 commits June 7, 2025 12:33
(DaskExpr doesn't have this function)

This reverts commit 709a4b1.
Wasn't sure about going alphabetical, since there others form more of a group
I *think* `selectors` are safe as-is, but will follow that up separately
@dangotbanned dangotbanned added enhancement New feature or request dtypes typing pyspark Issue is related to pyspark backend pyspark-connect and removed pyspark-connect pyspark Issue is related to pyspark backend labels Jun 7, 2025
@dangotbanned
Copy link
Member Author

#2654 (commits)

@MarcoGorelli sorry I butchered a rebase 😭

Comment on lines +365 to +391
IntoDType: TypeAlias = "dtypes.DType | type[NonNestedDType]"
"""Anything that can be converted into a Narwhals DType.

Examples:
>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame({"a": [1, 2, 3], "b": [4.0, 5.0, 6.0]})
>>> df = nw.from_native(df_native)
>>> df.select(
... nw.col("a").cast(nw.Int32),
... nw.col("b").cast(nw.String()).str.split(".").cast(nw.List(nw.Int8)),
... )
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
|Narwhals DataFrame|
|------------------|
|shape: (3, 2) |
|β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”|
|β”‚ a ┆ b β”‚|
|β”‚ --- ┆ --- β”‚|
|β”‚ i32 ┆ list[i8] β”‚|
|β•žβ•β•β•β•β•β•ͺ══════════║|
|β”‚ 1 ┆ [4, 0] β”‚|
|β”‚ 2 ┆ [5, 0] β”‚|
|β”‚ 3 ┆ [6, 0] β”‚|
|β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜|
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
"""
Copy link
Member Author

Choose a reason for hiding this comment

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

The logic I'm following for this is that we use IntoDType for anything that can trigger a cast.

Or another way to look at it is if the call reaches into _<backend>.utils.<thing>_to_<other>_dtype then this should warn when you're likely to raise

@dangotbanned dangotbanned marked this pull request as ready for review June 7, 2025 13:13
@@ -125,7 +125,7 @@ def test_cast_to_enum_vmain(
with pytest.raises(
ValueError, match="Can not cast / initialize Enum without categories present"
):
df_nw.select(col_a.cast(nw.Enum))
df_nw.select(col_a.cast(nw.Enum)) # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure if this got lost, but needing these ignores is a positive outcome πŸ™‚

Copy link
Member

Choose a reason for hiding this comment

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

Nice one

@dangotbanned dangotbanned requested a review from FBruzzesi June 11, 2025 21:58
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @dangotbanned - it's look pretty straightforward and definitly a nice improvement to be able to differentiate nested vs non-nested dtypes.

I only have a tiny question

@@ -125,7 +125,7 @@ def test_cast_to_enum_vmain(
with pytest.raises(
ValueError, match="Can not cast / initialize Enum without categories present"
):
df_nw.select(col_a.cast(nw.Enum))
df_nw.select(col_a.cast(nw.Enum)) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

Nice one

Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @dangotbanned 🚒🚒

It has been quite some time since last time I approved a PR πŸ˜‚

NarwhalChubbiwhalGIF

@dangotbanned dangotbanned merged commit 1376ecc into main Jun 12, 2025
31 of 32 checks passed
@dangotbanned dangotbanned deleted the into-dtype branch June 12, 2025 13:19
@MarcoGorelli
Copy link
Member

nice one, thanks both!

dangotbanned added a commit that referenced this pull request Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dtypes enhancement New feature or request typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add IntoDType alias, replace DType | type[DType]
3 participants