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

feat: publicly export Deferred #7613

Closed
1 task done
NickCrews opened this issue Nov 25, 2023 · 9 comments
Closed
1 task done

feat: publicly export Deferred #7613

NickCrews opened this issue Nov 25, 2023 · 9 comments
Labels
feature Features or general enhancements

Comments

@NickCrews
Copy link
Contributor

Is your feature request related to a problem?

I want to use this in type hints.

Before, I did from ibis.expr.deferred import Deferred but this recently broke, now I have to do from ibis.common.deferred import Deferred.

Describe the solution you'd like

It would be great to export this stably. just from ibis import Deferred should work?

What version of ibis are you running?

7.1.0

What backend(s) are you using, if any?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@NickCrews NickCrews added the feature Features or general enhancements label Nov 25, 2023
@tswast
Copy link
Collaborator

tswast commented Nov 27, 2023

I think this should be exposed in this API: ibis.expr.types as that's where I expect all the other expression-y types to be found.

@NickCrews
Copy link
Contributor Author

That is fine with me as long as it is canonical and stable.

@kszucs
Copy link
Member

kszucs commented Nov 28, 2023

Can you provide a couple of examples where you either use Deferred objects or check against its type? I have an assumption that you may rather need a function which transparently coerces various input objects - like Deferred - to expressions.

@tswast tswast assigned tswast and unassigned tswast Nov 28, 2023
@tswast
Copy link
Collaborator

tswast commented Nov 28, 2023

@kszucs FWIW, I encountered this here: googleapis/python-bigquery-dataframes#53 I needed to call get_name() on an object that might or might not be Deferred:

        self._column_names = {
            (
                column.resolve(table)
                # TODO(https://github.com/ibis-project/ibis/issues/7613): use
                # public API to refer to Deferred type.
                if isinstance(column, ibis.common.deferred.Deferred)
                else column
            ).get_name(): column
            for column in self._columns
        }

@NickCrews
Copy link
Contributor Author

A polymorphic function that can either take python builtins int or float, or ibis types. Perhaps I'm not doing this the best way, if you have a way of avoiding the Deferred then I'm all ears.

@overload
def round_to(nums: NumericValue | Deferred, n: int) -> NumericValue:
    ...


@overload
def round_to(nums: int | float, n: int) -> int:
    ...


def round_to(nums: NumericValue | Deferred | int | float, n: int) -> IntegerValue:
    """Round to the nearest n, eg 5."""
    if isinstance(nums, (NumericValue, Deferred)):
        return (nums / n).round().cast(int) * n
    else:
        return round(nums / n) * n


def test_round_to_deferred():
    x = ibis.memtable({"inp": [1, 2, 4], "expected": [0, 0, 5]})
    x = x.mutate(result=round_to(_.inp, 5))
    df = x.to_pandas()
    assert df["result"].equals(df["expected"])

@cpcloud
Copy link
Member

cpcloud commented Dec 4, 2023

@kszucs Any thoughts here based on feedback from Tim and Nick?

@NickCrews
Copy link
Contributor Author

I am also using it as a type annotation for a dataclass like condition: bool | Deferred | Callable[[Table], BooleanValue]

@NickCrews
Copy link
Contributor Author

Another use case I have:

I have a Table -> Table function. I have some legacy pandas code that operates on DFs, but I want to use this new Table function. So I wrote
a decorator that makes it so a function takes either a Table or DF as input, converts it to a memtable if needed, calls the function, and reverts the result back to a DF if called with a DF. In those type checks i also need to worry about checking for Deferreds.

I have a similar decorator for Columns/pd.Series.

@jcrist
Copy link
Member

jcrist commented Aug 14, 2024

This was fixed in #9008. Closing.

@jcrist jcrist closed this as completed Aug 14, 2024
@github-project-automation github-project-automation bot moved this from backlog to done in Ibis planning and roadmap Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Features or general enhancements
Projects
Archived in project
Development

No branches or pull requests

5 participants