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

Disallow .__call__() as workaround for non-named functions #32460

Merged
merged 7 commits into from
May 13, 2020

Conversation

alexmojaki
Copy link
Contributor

Currently this script:

import pandas as pd
funcs = [lambda: 1]
pd.eval("funcs[0]()")

Fails with:

TypeError: Only named functions are supported

however this can easily be worked around by adding .__call__:

pd.eval("funcs[0].__call__()")

I'm assuming we don't want to allow this workaround. This PR ensures that it will fail with the same error.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

I am not sure what you are suggesting here? in theory lambda functions could be supported, but to be honest i think the error is fine here. Always start with a test.

@jreback jreback added Error Reporting Incorrect or improved errors from pandas Numeric Operations Arithmetic, Comparison, and Logical operations labels Mar 15, 2020
@alexmojaki
Copy link
Contributor Author

It's not about lambdas. In this context "named" means a variable name or an attribute - pandas doesn't allow calling arbitrary expressions. For example funcs[0] is a subscript expression so it can't be called.

@alexmojaki alexmojaki requested a review from jreback March 15, 2020 11:18
@alexmojaki alexmojaki force-pushed the disallow-call branch 2 times, most recently from 0a3a36d to f8fe5ff Compare March 17, 2020 06:41
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

needs a whatsnew note, bug fixes for 1.1 under the reshaping section. pls merge master and ping on green.


df.eval("@func()")

with pytest.raises(TypeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you test the exception message as well, it is not clear what the user is going to see here

@alexmojaki
Copy link
Contributor Author

@jreback ping!

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

looks ok, ping on green.

@@ -1181,3 +1181,17 @@ def test_failing_character_outside_range(self, df):
def test_failing_hashtag(self, df):
with pytest.raises(SyntaxError):
df.query("`foo#bar` > 4")

def test_call_non_named_expression(self, df):
def func(*_):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the issue number here as a comment.

@jreback jreback added this to the 1.1 milestone May 10, 2020
@pep8speaks
Copy link

pep8speaks commented May 10, 2020

Hello @alexmojaki! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-05-10 17:52:29 UTC

@alexmojaki
Copy link
Contributor Author

@jreback pong!

@jreback jreback merged commit 6c0be4b into pandas-dev:master May 13, 2020
@jreback
Copy link
Contributor

jreback commented May 13, 2020

thanks @alexmojaki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants