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: Use metaclass for safe DType attr access #2025

Merged
merged 13 commits into from
Feb 19, 2025
Merged

fix: Use metaclass for safe DType attr access #2025

merged 13 commits into from
Feb 19, 2025

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Feb 16, 2025

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

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

Related issues

Mentioned in

Checklist

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

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

This gives you a read-only default for:

Datetime.time_unit

Which is unaffected by the instance-level version:

dtype = Datetime("ms")
dtype.time_unit

I imagined this would also be useful Duration:

class Duration(TemporalType):

Originally thought this would be beneficial for the NestedType classes, but I'm seeing that they don't have defaults

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
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]
Copy link
Member Author

Choose a reason for hiding this comment

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

time_unit="s" isn't allowed for polars, but we pass it through anyway?

@dangotbanned dangotbanned marked this pull request as ready for review February 17, 2025 17:58
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

I didn't think i liked metaclasses, but this is pretty neat - thanks @dangotbanned !

@dangotbanned
Copy link
Member Author

I didn't think i liked metaclasses, but this is pretty neat - thanks @dangotbanned !

@MarcoGorelli

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

Copy link
Member

@MarcoGorelli MarcoGorelli 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 !

@MarcoGorelli MarcoGorelli merged commit 8cfab26 into main Feb 19, 2025
28 checks passed
@MarcoGorelli MarcoGorelli deleted the dtype-meta branch February 19, 2025 12:57
@dangotbanned dangotbanned changed the title fix(RFC): Use metaclass for safe DType attr access fix: Use metaclass for safe DType attr access Feb 19, 2025
dangotbanned added a commit that referenced this pull request Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants