Skip to content

Commit 510bddd

Browse files
jbrockmendeljreback
authored andcommitted
TYP: annotate queryables (#29754)
1 parent 32d541e commit 510bddd

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

pandas/core/computation/ops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class UndefinedVariableError(NameError):
5555
NameError subclass for local variables.
5656
"""
5757

58-
def __init__(self, name, is_local):
58+
def __init__(self, name, is_local: bool):
5959
if is_local:
6060
msg = "local variable {0!r} is not defined"
6161
else:
@@ -69,6 +69,8 @@ def __new__(cls, name, env, side=None, encoding=None):
6969
supr_new = super(Term, klass).__new__
7070
return supr_new(klass)
7171

72+
is_local: bool
73+
7274
def __init__(self, name, env, side=None, encoding=None):
7375
# name is a str for Term, but may be something else for subclasses
7476
self._name = name

pandas/core/computation/pytables.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import ast
44
from functools import partial
5-
from typing import Any, Optional, Tuple
5+
from typing import Any, Dict, Optional, Tuple
66

77
import numpy as np
88

@@ -24,17 +24,27 @@
2424
class Scope(_scope.Scope):
2525
__slots__ = ("queryables",)
2626

27-
def __init__(self, level: int, global_dict=None, local_dict=None, queryables=None):
27+
queryables: Dict[str, Any]
28+
29+
def __init__(
30+
self,
31+
level: int,
32+
global_dict=None,
33+
local_dict=None,
34+
queryables: Optional[Dict[str, Any]] = None,
35+
):
2836
super().__init__(level + 1, global_dict=global_dict, local_dict=local_dict)
2937
self.queryables = queryables or dict()
3038

3139

3240
class Term(ops.Term):
41+
env: Scope
42+
3343
def __new__(cls, name, env, side=None, encoding=None):
3444
klass = Constant if not isinstance(name, str) else cls
3545
return object.__new__(klass)
3646

37-
def __init__(self, name, env, side=None, encoding=None):
47+
def __init__(self, name, env: Scope, side=None, encoding=None):
3848
super().__init__(name, env, side=side, encoding=encoding)
3949

4050
def _resolve_name(self):
@@ -69,7 +79,10 @@ class BinOp(ops.BinOp):
6979

7080
_max_selectors = 31
7181

72-
def __init__(self, op, lhs, rhs, queryables, encoding):
82+
op: str
83+
queryables: Dict[str, Any]
84+
85+
def __init__(self, op: str, lhs, rhs, queryables: Dict[str, Any], encoding):
7386
super().__init__(op, lhs, rhs)
7487
self.queryables = queryables
7588
self.encoding = encoding
@@ -373,9 +386,6 @@ def prune(self, klass):
373386
return None
374387

375388

376-
_op_classes = {"unary": UnaryOp}
377-
378-
379389
class ExprVisitor(BaseExprVisitor):
380390
const_type = Constant
381391
term_type = Term
@@ -510,7 +520,16 @@ class Expr(expr.Expr):
510520
"major_axis>=20130101"
511521
"""
512522

513-
def __init__(self, where, queryables=None, encoding=None, scope_level: int = 0):
523+
_visitor: Optional[ExprVisitor]
524+
env: Scope
525+
526+
def __init__(
527+
self,
528+
where,
529+
queryables: Optional[Dict[str, Any]] = None,
530+
encoding=None,
531+
scope_level: int = 0,
532+
):
514533

515534
where = _validate_where(where)
516535

pandas/core/computation/scope.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pprint
1010
import struct
1111
import sys
12+
from typing import List
1213

1314
import numpy as np
1415

@@ -203,7 +204,7 @@ def resolve(self, key, is_local):
203204

204205
raise UndefinedVariableError(key, is_local)
205206

206-
def swapkey(self, old_key, new_key, new_value=None):
207+
def swapkey(self, old_key: str, new_key: str, new_value=None):
207208
"""
208209
Replace a variable name, with a potentially new value.
209210
@@ -228,7 +229,7 @@ def swapkey(self, old_key, new_key, new_value=None):
228229
mapping[new_key] = new_value
229230
return
230231

231-
def _get_vars(self, stack, scopes):
232+
def _get_vars(self, stack, scopes: List[str]):
232233
"""
233234
Get specifically scoped variables from a list of stack frames.
234235

pandas/io/pytables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,7 @@ def convert(
20402040
Table row number: the end of the sub-selection. Values larger than
20412041
the underlying table's row count are normalized to that.
20422042
"""
2043+
assert self.table is not None # for mypy
20432044

20442045
assert self.table is not None
20452046

@@ -3417,7 +3418,7 @@ def data_orientation(self):
34173418
)
34183419
)
34193420

3420-
def queryables(self):
3421+
def queryables(self) -> Dict[str, Any]:
34213422
""" return a dict of the kinds allowable columns for this object """
34223423

34233424
# compute the values_axes queryables

0 commit comments

Comments
 (0)