From 4986b314b25c3537942870983f37f9c690109684 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 17 Nov 2019 11:48:49 -0800 Subject: [PATCH 1/2] CLN: parts of #29667 --- pandas/core/computation/eval.py | 4 ++-- pandas/core/computation/pytables.py | 4 ++-- pandas/core/computation/scope.py | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/computation/eval.py b/pandas/core/computation/eval.py index de2133f64291d..72f2e1d8e23e5 100644 --- a/pandas/core/computation/eval.py +++ b/pandas/core/computation/eval.py @@ -11,7 +11,7 @@ from pandas.core.computation.engines import _engines from pandas.core.computation.expr import Expr, _parsers, tokenize_string -from pandas.core.computation.scope import _ensure_scope +from pandas.core.computation.scope import ensure_scope from pandas.io.formats.printing import pprint_thing @@ -309,7 +309,7 @@ def eval( _check_for_locals(expr, level, parser) # get our (possibly passed-in) scope - env = _ensure_scope( + env = ensure_scope( level + 1, global_dict=global_dict, local_dict=local_dict, diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 13a4814068d6a..13133d04ccd5d 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -13,7 +13,7 @@ import pandas as pd import pandas.core.common as com -from pandas.core.computation import expr, ops +from pandas.core.computation import expr, ops, scope as _scope from pandas.core.computation.common import _ensure_decoded from pandas.core.computation.expr import BaseExprVisitor from pandas.core.computation.ops import UndefinedVariableError, is_term @@ -21,7 +21,7 @@ from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded -class Scope(expr.Scope): +class Scope(_scope.Scope): __slots__ = ("queryables",) def __init__(self, level, global_dict=None, local_dict=None, queryables=None): diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index ee82664f6cb21..2c5c687a44680 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -16,9 +16,9 @@ from pandas.compat.chainmap import DeepChainMap -def _ensure_scope( - level, global_dict=None, local_dict=None, resolvers=(), target=None, **kwargs -): +def ensure_scope( + level: int, global_dict=None, local_dict=None, resolvers=(), target=None, **kwargs +) -> "Scope": """Ensure that we are grabbing the correct scope.""" return Scope( level + 1, @@ -119,7 +119,7 @@ def __init__( self.scope.update(local_dict.scope) if local_dict.target is not None: self.target = local_dict.target - self.update(local_dict.level) + self._update(local_dict.level) frame = sys._getframe(self.level) @@ -251,7 +251,7 @@ def _get_vars(self, stack, scopes): # scope after the loop del frame - def update(self, level: int): + def _update(self, level: int): """ Update the current scope by going back `level` levels. From 042303c45edf30cf62823b2dd2204e74d83151b6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 17 Nov 2019 13:59:02 -0800 Subject: [PATCH 2/2] couple i missed --- pandas/core/computation/ops.py | 4 +++- pandas/core/computation/pytables.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index ce67c3152ecd0..524013ceef5ff 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -197,7 +197,9 @@ class Op: Hold an operator of arbitrary arity. """ - def __init__(self, op, operands, *args, **kwargs): + op: str + + def __init__(self, op: str, operands, *args, **kwargs): self.op = _bool_op_map.get(op, op) self.operands = operands self.encoding = kwargs.get("encoding", None) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 13133d04ccd5d..8306c634d14e8 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -24,7 +24,7 @@ class Scope(_scope.Scope): __slots__ = ("queryables",) - def __init__(self, level, global_dict=None, local_dict=None, queryables=None): + def __init__(self, level: int, global_dict=None, local_dict=None, queryables=None): super().__init__(level + 1, global_dict=global_dict, local_dict=local_dict) self.queryables = queryables or dict() @@ -40,6 +40,7 @@ def __init__(self, name, env, side=None, encoding=None): def _resolve_name(self): # must be a queryables if self.side == "left": + # Note: The behavior of __new__ ensures that self.name is a str here if self.name not in self.env.queryables: raise NameError("name {name!r} is not defined".format(name=self.name)) return self.name