Skip to content

Commit 2764bac

Browse files
jbrockmendelproost
authored andcommitted
CLN: typing and renaming in computation.pytables (pandas-dev#29778)
1 parent 9b88acd commit 2764bac

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

pandas/core/computation/pytables.py

+29-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded
2222

2323

24-
class Scope(_scope.Scope):
24+
class PyTablesScope(_scope.Scope):
2525
__slots__ = ("queryables",)
2626

2727
queryables: Dict[str, Any]
@@ -38,13 +38,13 @@ def __init__(
3838

3939

4040
class Term(ops.Term):
41-
env: Scope
41+
env: PyTablesScope
4242

4343
def __new__(cls, name, env, side=None, encoding=None):
4444
klass = Constant if not isinstance(name, str) else cls
4545
return object.__new__(klass)
4646

47-
def __init__(self, name, env: Scope, side=None, encoding=None):
47+
def __init__(self, name, env: PyTablesScope, side=None, encoding=None):
4848
super().__init__(name, env, side=side, encoding=encoding)
4949

5050
def _resolve_name(self):
@@ -68,7 +68,8 @@ def value(self):
6868

6969

7070
class Constant(Term):
71-
def __init__(self, value, env, side=None, encoding=None):
71+
def __init__(self, value, env: PyTablesScope, side=None, encoding=None):
72+
assert isinstance(env, PyTablesScope), type(env)
7273
super().__init__(value, env, side=side, encoding=encoding)
7374

7475
def _resolve_name(self):
@@ -270,7 +271,7 @@ def evaluate(self):
270271
raise ValueError("query term is not valid [{slf}]".format(slf=self))
271272

272273
rhs = self.conform(self.rhs)
273-
values = [TermValue(v, v, self.kind).value for v in rhs]
274+
values = list(rhs)
274275

275276
if self.is_in_table:
276277

@@ -386,7 +387,7 @@ def prune(self, klass):
386387
return None
387388

388389

389-
class ExprVisitor(BaseExprVisitor):
390+
class PyTablesExprVisitor(BaseExprVisitor):
390391
const_type = Constant
391392
term_type = Term
392393

@@ -486,25 +487,29 @@ def _validate_where(w):
486487
TypeError : An invalid data type was passed in for w (e.g. dict).
487488
"""
488489

489-
if not (isinstance(w, (Expr, str)) or is_list_like(w)):
490-
raise TypeError("where must be passed as a string, Expr, or list-like of Exprs")
490+
if not (isinstance(w, (PyTablesExpr, str)) or is_list_like(w)):
491+
raise TypeError(
492+
"where must be passed as a string, PyTablesExpr, "
493+
"or list-like of PyTablesExpr"
494+
)
491495

492496
return w
493497

494498

495-
class Expr(expr.Expr):
496-
""" hold a pytables like expression, comprised of possibly multiple 'terms'
499+
class PyTablesExpr(expr.Expr):
500+
"""
501+
Hold a pytables-like expression, comprised of possibly multiple 'terms'.
497502
498503
Parameters
499504
----------
500-
where : string term expression, Expr, or list-like of Exprs
505+
where : string term expression, PyTablesExpr, or list-like of PyTablesExprs
501506
queryables : a "kinds" map (dict of column name -> kind), or None if column
502507
is non-indexable
503508
encoding : an encoding that will encode the query terms
504509
505510
Returns
506511
-------
507-
an Expr object
512+
a PyTablesExpr object
508513
509514
Examples
510515
--------
@@ -520,8 +525,8 @@ class Expr(expr.Expr):
520525
"major_axis>=20130101"
521526
"""
522527

523-
_visitor: Optional[ExprVisitor]
524-
env: Scope
528+
_visitor: Optional[PyTablesExprVisitor]
529+
env: PyTablesScope
525530

526531
def __init__(
527532
self,
@@ -542,14 +547,14 @@ def __init__(
542547
# capture the environment if needed
543548
local_dict = DeepChainMap()
544549

545-
if isinstance(where, Expr):
550+
if isinstance(where, PyTablesExpr):
546551
local_dict = where.env.scope
547552
_where = where.expr
548553

549554
elif isinstance(where, (list, tuple)):
550555
where = list(where)
551556
for idx, w in enumerate(where):
552-
if isinstance(w, Expr):
557+
if isinstance(w, PyTablesExpr):
553558
local_dict = w.env.scope
554559
else:
555560
w = _validate_where(w)
@@ -559,11 +564,11 @@ def __init__(
559564
_where = where
560565

561566
self.expr = _where
562-
self.env = Scope(scope_level + 1, local_dict=local_dict)
567+
self.env = PyTablesScope(scope_level + 1, local_dict=local_dict)
563568

564569
if queryables is not None and isinstance(self.expr, str):
565570
self.env.queryables.update(queryables)
566-
self._visitor = ExprVisitor(
571+
self._visitor = PyTablesExprVisitor(
567572
self.env,
568573
queryables=queryables,
569574
parser="pytables",
@@ -601,30 +606,31 @@ def evaluate(self):
601606
class TermValue:
602607
""" hold a term value the we use to construct a condition/filter """
603608

604-
def __init__(self, value, converted, kind: Optional[str]):
609+
def __init__(self, value, converted, kind: str):
610+
assert isinstance(kind, str), kind
605611
self.value = value
606612
self.converted = converted
607613
self.kind = kind
608614

609-
def tostring(self, encoding):
615+
def tostring(self, encoding) -> str:
610616
""" quote the string if not encoded
611617
else encode and return """
612618
if self.kind == "string":
613619
if encoding is not None:
614-
return self.converted
620+
return str(self.converted)
615621
return '"{converted}"'.format(converted=self.converted)
616622
elif self.kind == "float":
617623
# python 2 str(float) is not always
618624
# round-trippable so use repr()
619625
return repr(self.converted)
620-
return self.converted
626+
return str(self.converted)
621627

622628

623629
def maybe_expression(s) -> bool:
624630
""" loose checking if s is a pytables-acceptable expression """
625631
if not isinstance(s, str):
626632
return False
627-
ops = ExprVisitor.binary_ops + ExprVisitor.unary_ops + ("=",)
633+
ops = PyTablesExprVisitor.binary_ops + PyTablesExprVisitor.unary_ops + ("=",)
628634

629635
# make sure we have an op at least
630636
return any(op in s for op in ops)

pandas/core/computation/scope.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def has_resolvers(self) -> bool:
162162
"""
163163
return bool(len(self.resolvers))
164164

165-
def resolve(self, key, is_local):
165+
def resolve(self, key: str, is_local: bool):
166166
"""
167167
Resolve a variable name in a possibly local context.
168168

pandas/io/pytables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from pandas.core.arrays.categorical import Categorical
4949
from pandas.core.arrays.sparse import BlockIndex, IntIndex
5050
import pandas.core.common as com
51-
from pandas.core.computation.pytables import Expr, maybe_expression
51+
from pandas.core.computation.pytables import PyTablesExpr, maybe_expression
5252
from pandas.core.index import ensure_index
5353
from pandas.core.internals import BlockManager, _block_shape, make_block
5454

@@ -93,7 +93,7 @@ def _ensure_str(name):
9393
return name
9494

9595

96-
Term = Expr
96+
Term = PyTablesExpr
9797

9898

9999
def _ensure_term(where, scope_level: int):
@@ -4954,7 +4954,7 @@ def generate(self, where):
49544954

49554955
q = self.table.queryables()
49564956
try:
4957-
return Expr(where, queryables=q, encoding=self.table.encoding)
4957+
return PyTablesExpr(where, queryables=q, encoding=self.table.encoding)
49584958
except NameError:
49594959
# raise a nice message, suggesting that the user should use
49604960
# data_columns

pandas/tests/computation/test_eval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ def test_invalid_parser():
18911891

18921892
_parsers: Dict[str, Type[BaseExprVisitor]] = {
18931893
"python": PythonExprVisitor,
1894-
"pytables": pytables.ExprVisitor,
1894+
"pytables": pytables.PyTablesExprVisitor,
18951895
"pandas": PandasExprVisitor,
18961896
}
18971897

0 commit comments

Comments
 (0)