Skip to content

fix: change empty to property#425

Merged
FabianHofmann merged 4 commits intomasterfrom
breaking/change-empty-to-property
Mar 12, 2025
Merged

fix: change empty to property#425
FabianHofmann merged 4 commits intomasterfrom
breaking/change-empty-to-property

Conversation

@coroa
Copy link
Copy Markdown
Member

@coroa coroa commented Mar 12, 2025

In view of #424 (ie. empty being mostly True automatically), i would argue the current use of empty is quite limited and i'd prefer to take this chance to make it a property as it is in pandas, since it is today too easy to misuse as expr.empty which is always truthy.

Contrarily the upgrade path from expr.empty() to expr.empty will always raise a clear Exception.

Update: Added the wrapper that @FabianHofmann sketched below to smooth the transition.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@coroa coroa requested review from FabianHofmann and lkstrp and removed request for FabianHofmann March 12, 2025 08:09
@FabianHofmann
Copy link
Copy Markdown
Collaborator

I like that and wanted to do this for a long time, was scared of the breaking change though. could we instead define a callable Empty class returned by the property, which if called raises a deprecation warning? Something like

import warnings
import functools

class Empty:
    def __init__(self, value):
        self._value = bool(value)
    
    def __bool__(self):
        return self._value
    
    def __call__(self):
        warnings.warn(
            "Calling `.empty()` is deprecated, use `.empty` property instead",
            DeprecationWarning,
            stacklevel=2
        )
        return self._value
    

and then in the property

    @property
    def empty(self):
        is_empty = ...
        return Empty(is_empty)

@coroa
Copy link
Copy Markdown
Member Author

coroa commented Mar 12, 2025

I guess that would work, quite a bit of boilerplate for that simple change, but everyone loves downward compatibility!

Copy link
Copy Markdown
Member

@lkstrp lkstrp left a comment

Choose a reason for hiding this comment

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

We might also want to add __repr__ to the descriptor. Otherwise the memory reference is shown, which is confusing.

Even cleaner, which would not mock a simple boolean, the process would probably be to have a module wide option setting, raise a deprecation warning for .empty() without breaking behaviour, and allow opting in to use .empty.

@coroa coroa force-pushed the breaking/change-empty-to-property branch from d8ef651 to df4cbfd Compare March 12, 2025 12:47
@coroa
Copy link
Copy Markdown
Member Author

coroa commented Mar 12, 2025

Added the __repr__, but went with the wrapper. I am not a big fan of opt-in. I directly want to use the good shit.

@coroa coroa changed the title Breaking: change empty to property ~Breaking:~ change empty to property Mar 12, 2025
@coroa coroa changed the title ~Breaking:~ change empty to property fix: change empty to property Mar 12, 2025
Copy link
Copy Markdown
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

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

great!

@FabianHofmann FabianHofmann merged commit 6fdf678 into master Mar 12, 2025
21 checks passed
@FabianHofmann FabianHofmann deleted the breaking/change-empty-to-property branch March 12, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants