Skip to content

Commit 3dd747c

Browse files
check_untyped_defs pandas.core.computation.ops xref pandas-dev#28446
1 parent 2e78276 commit 3dd747c

File tree

6 files changed

+23
-44
lines changed

6 files changed

+23
-44
lines changed

pandas/core/computation/engines.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ def _evaluate(self):
117117
try:
118118
env = self.expr.env
119119
scope = env.full_scope
120-
truediv = scope["truediv"]
121120
_check_ne_builtin_clash(self.expr)
122-
return ne.evaluate(s, local_dict=scope, truediv=truediv)
121+
return ne.evaluate(s, local_dict=scope)
123122
except KeyError as e:
124123
# python 3 compat kludge
125124
try:

pandas/core/computation/eval.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ def eval(
171171
expr,
172172
parser="pandas",
173173
engine: Optional[str] = None,
174-
truediv=True,
175174
local_dict=None,
176175
global_dict=None,
177176
resolvers=(),
@@ -219,8 +218,6 @@ def eval(
219218
220219
More backends may be available in the future.
221220
222-
truediv : bool, optional
223-
Whether to use true division, like in Python >= 3
224221
local_dict : dict or None, optional
225222
A dictionary of local variables, taken from locals() by default.
226223
global_dict : dict or None, optional
@@ -320,7 +317,7 @@ def eval(
320317
target=target,
321318
)
322319

323-
parsed_expr = Expr(expr, engine=engine, parser=parser, env=env, truediv=truediv)
320+
parsed_expr = Expr(expr, engine=engine, parser=parser, env=env)
324321

325322
# construct the engine and evaluate the parsed expression
326323
eng = _engines[engine]

pandas/core/computation/expr.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,7 @@ def visit_BinOp(self, node, **kwargs):
565565
return self._maybe_evaluate_binop(op, op_class, left, right)
566566

567567
def visit_Div(self, node, **kwargs):
568-
truediv = self.env.scope["truediv"]
569-
return lambda lhs, rhs: Div(lhs, rhs, truediv)
568+
return lambda lhs, rhs: Div(lhs, rhs)
570569

571570
def visit_UnaryOp(self, node, **kwargs):
572571
op = self.visit(node.op)
@@ -812,18 +811,14 @@ class Expr:
812811
engine : str, optional, default 'numexpr'
813812
parser : str, optional, default 'pandas'
814813
env : Scope, optional, default None
815-
truediv : bool, optional, default True
816814
level : int, optional, default 2
817815
"""
818816

819-
def __init__(
820-
self, expr, engine="numexpr", parser="pandas", env=None, truediv=True, level=0
821-
):
817+
def __init__(self, expr, engine="numexpr", parser="pandas", env=None, level=0):
822818
self.expr = expr
823819
self.env = env or Scope(level=level + 1)
824820
self.engine = engine
825821
self.parser = parser
826-
self.env.scope["truediv"] = truediv
827822
self._visitor = _parsers[parser](self.env, self.engine, self.parser)
828823
self.terms = self.parse()
829824

pandas/core/computation/ops.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from distutils.version import LooseVersion
66
from functools import partial
77
import operator as op
8+
from typing import Callable, Iterable, Union
89

910
import numpy as np
1011

@@ -196,10 +197,10 @@ class Op:
196197
Hold an operator of arbitrary arity.
197198
"""
198199

199-
def __init__(self, op, operands, *args, **kwargs):
200+
def __init__(self, op: str, operands: Iterable[Union[Term, "Op"]], encoding=None):
200201
self.op = _bool_op_map.get(op, op)
201202
self.operands = operands
202-
self.encoding = kwargs.get("encoding", None)
203+
self.encoding = encoding
203204

204205
def __iter__(self):
205206
return iter(self.operands)
@@ -333,11 +334,11 @@ class BinOp(Op):
333334
Parameters
334335
----------
335336
op : str
336-
left : Term or Op
337-
right : Term or Op
337+
lhs : Term or Op
338+
rhs : Term or Op
338339
"""
339340

340-
def __init__(self, op, lhs, rhs, **kwargs):
341+
def __init__(self, op: str, lhs: Union[Term, Op], rhs: Union[Term, Op]):
341342
super().__init__(op, (lhs, rhs))
342343
self.lhs = lhs
343344
self.rhs = rhs
@@ -369,10 +370,6 @@ def __call__(self, env):
369370
object
370371
The result of an evaluated expression.
371372
"""
372-
# handle truediv
373-
if self.op == "/" and env.scope["truediv"]:
374-
self.func = op.truediv
375-
376373
# recurse over the left/right nodes
377374
left = self.lhs(env)
378375
right = self.rhs(env)
@@ -431,6 +428,7 @@ def convert_values(self):
431428
"""
432429

433430
def stringify(value):
431+
encoder: Callable
434432
if self.encoding is not None:
435433
encoder = partial(pprint_thing_encoded, encoding=self.encoding)
436434
else:
@@ -483,13 +481,10 @@ class Div(BinOp):
483481
----------
484482
lhs, rhs : Term or Op
485483
The Terms or Ops in the ``/`` expression.
486-
truediv : bool
487-
Whether or not to use true division. With Python 3 this happens
488-
regardless of the value of ``truediv``.
489484
"""
490485

491-
def __init__(self, lhs, rhs, truediv, *args, **kwargs):
492-
super().__init__("/", lhs, rhs, *args, **kwargs)
486+
def __init__(self, lhs: Union[Term, Op], rhs: Union[Term, Op]):
487+
super().__init__("/", lhs, rhs)
493488

494489
if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
495490
raise TypeError(

pandas/tests/computation/test_eval.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from functools import reduce
33
from itertools import product
44
import operator
5+
import re
56
import warnings
67

78
import numpy as np
@@ -1196,32 +1197,27 @@ def test_single_variable(self):
11961197
df2 = self.eval("df", local_dict={"df": df})
11971198
assert_frame_equal(df, df2)
11981199

1199-
def test_truediv(self):
1200+
def test_div(self):
12001201
s = np.array([1])
12011202
ex = "s / 1"
12021203
d = {"s": s} # noqa
12031204

1204-
res = self.eval(ex, truediv=False)
1205+
res = self.eval(ex)
12051206
tm.assert_numpy_array_equal(res, np.array([1.0]))
12061207

1207-
res = self.eval(ex, truediv=True)
1208-
tm.assert_numpy_array_equal(res, np.array([1.0]))
1209-
1210-
res = self.eval("1 / 2", truediv=True)
1211-
expec = 0.5
1212-
assert res == expec
1213-
1214-
res = self.eval("1 / 2", truediv=False)
1208+
res = self.eval("1 / 2")
12151209
expec = 0.5
12161210
assert res == expec
12171211

1218-
res = self.eval("s / 2", truediv=False)
1212+
res = self.eval("s / 2")
12191213
expec = 0.5
12201214
assert res == expec
12211215

1222-
res = self.eval("s / 2", truediv=True)
1223-
expec = 0.5
1224-
assert res == expec
1216+
def test_truediv_kwarg_raises(self):
1217+
# gh-28446
1218+
msg = re.escape("eval() got an unexpected keyword argument 'truediv'")
1219+
with pytest.raises(TypeError, match=msg):
1220+
self.eval("1 / 2", truediv=False)
12251221

12261222
def test_failing_subscript_with_name_error(self):
12271223
df = DataFrame(np.random.randn(5, 3)) # noqa

setup.cfg

-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ check_untyped_defs=False
200200
[mypy-pandas.core.computation.expressions]
201201
check_untyped_defs=False
202202

203-
[mypy-pandas.core.computation.ops]
204-
check_untyped_defs=False
205-
206203
[mypy-pandas.core.computation.pytables]
207204
check_untyped_defs=False
208205

0 commit comments

Comments
 (0)