Skip to content

Commit 675e715

Browse files
committed
Refactor: push eval_env error checking into EvalEnvironment.capture where it belongs
1 parent 3819ae9 commit 675e715

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

patsy/build.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ def _make_term_column_builders(terms,
615615
term_to_column_builders[term] = column_builders
616616
return new_term_order, term_to_column_builders
617617

618-
def design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action="drop"):
618+
def design_matrix_builders(termlists, data_iter_maker, eval_env,
619+
NA_action="drop"):
619620
"""Construct several :class:`DesignMatrixBuilders` from termlists.
620621
621622
This is one of Patsy's fundamental functions. This function and
@@ -654,14 +655,10 @@ def design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action="drop
654655
.. versionadded:: 0.4.0
655656
The ``eval_env`` argument.
656657
"""
657-
# Check type of eval_env to help people migrating to 0.4.0. Third
658-
# argument used to be NA_action (a string). Having the check for
659-
# eval_env's type gives people migrating to 0.4.0 who used NA_action
660-
# not as a keyword argument a nice error message here, instead of a
661-
# more obscure backtrace later on.
662-
if not isinstance(eval_env, six.integer_types + (EvalEnvironment,)):
663-
raise TypeError("Parameter 'eval_env' must be either an integer or an instance "
664-
"of patsy.EvalEnvironment.")
658+
# People upgrading from versions prior to 0.4.0 could potentially have
659+
# passed NA_action as the 3rd positional argument. Fortunately
660+
# EvalEnvironment.capture only accepts int and EvalEnvironment objects,
661+
# and we improved its error messages to make this clear.
665662
eval_env = EvalEnvironment.capture(eval_env, reference=1)
666663
if isinstance(NA_action, str):
667664
NA_action = NAAction(NA_action)

patsy/eval.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import __future__
1717
import inspect
1818
import tokenize
19-
import six
2019
import ast
20+
import numbers
21+
import six
2122
from patsy import PatsyError
2223
from patsy.util import PushbackAdapter
2324
from patsy.tokens import (pretty_untokenize, normalize_token_spacing,
@@ -213,8 +214,11 @@ def my_model(formula_like, data, eval_env=0):
213214
"""
214215
if isinstance(eval_env, cls):
215216
return eval_env
216-
else:
217+
elif isinstance(eval_env, numbers.Integral):
217218
depth = eval_env + reference
219+
else:
220+
raise TypeError("Parameter 'eval_env' must be either an integer "
221+
"or an instance of patsy.EvalEnvironment.")
218222
frame = inspect.currentframe()
219223
try:
220224
for i in range(depth + 1):
@@ -293,6 +297,8 @@ def test_EvalEnvironment_capture_namespace():
293297

294298
assert EvalEnvironment.capture(b1) is b1
295299

300+
assert_raises(TypeError, EvalEnvironment.capture, 1.2)
301+
296302
def test_EvalEnvironment_capture_flags():
297303
if sys.version_info >= (3,):
298304
# This is the only __future__ feature currently usable in Python

0 commit comments

Comments
 (0)