fix: Use metaclass for safe DType attr access#2025
Merged
MarcoGorelli merged 13 commits intomainfrom Feb 19, 2025
Merged
Conversation
Both `Duration` and `Datetime` are working with `polars` now. From this point it should just be reducing code for all the other backends
They're all noops, but good to keep consistent
dangotbanned
commented
Feb 17, 2025
| return pl.Duration(time_unit=du_time_unit) # type: ignore[arg-type] | ||
| if isinstance_or_issubclass(dtype, dtypes.Datetime): | ||
| # [s] is not allowed for polars | ||
| return pl.Datetime(dtype.time_unit, dtype.time_zone) # type: ignore[arg-type] |
Member
Author
There was a problem hiding this comment.
time_unit="s" isn't allowed for polars, but we pass it through anyway?
The metaclass is much narrower than `type` previously
Member
MarcoGorelli
left a comment
There was a problem hiding this comment.
I didn't think i liked metaclasses, but this is pretty neat - thanks @dangotbanned !
Member
Author
Yeah I know - they can be complex some times - but this is probably the simplest use case for them 🙂 Really wasn't expecting everything to go so smoothly |
DType attr accessDType attr access
10 tasks
10 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

What type of PR is this? (check all applicable)
Related issues
Mentioned in
Checklist
If you have comments or can explain your changes, please do so below
Datetime, when used in_polarsonlyThis gives you a read-only default for:
Which is unaffected by the instance-level version:
I imagined this would also be useful
Duration:narwhals/narwhals/dtypes.py
Line 527 in 43d072b
Originally thought this would be beneficial for the
NestedTypeclasses, but I'm seeing that they don't have defaults