Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT remove check for deprecated behavior in test.py #16109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions sklearn/feature_extraction/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_array_equal
from sklearn.utils import IS_PYPY
from sklearn.exceptions import ChangedBehaviorWarning
from sklearn.utils._testing import (assert_almost_equal,
assert_warns_message, assert_raise_message,
clean_warning_registry,
Expand Down Expand Up @@ -1294,12 +1293,8 @@ def test_callable_analyzer_error(Estimator, input_type, err_type, err_msg):
@pytest.mark.parametrize('input_type', ['file', 'filename'])
def test_callable_analyzer_change_behavior(Estimator, analyzer, input_type):
data = ['this is text, not file or filename']
warn_msg = 'Since v0.21, vectorizer'
with pytest.raises((FileNotFoundError, AttributeError)):
with pytest.warns(ChangedBehaviorWarning, match=warn_msg) as records:
Estimator(analyzer=analyzer, input=input_type).fit_transform(data)
assert len(records) == 1
assert warn_msg in str(records[0])
Estimator(analyzer=analyzer, input=input_type).fit_transform(data)


@pytest.mark.parametrize(
Expand Down
26 changes: 1 addition & 25 deletions sklearn/feature_extraction/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ..utils.validation import check_is_fitted, check_array, FLOAT_DTYPES
from ..utils import _IS_32BIT, deprecated
from ..utils.fixes import _astype_copy_false
from ..exceptions import ChangedBehaviorWarning, NotFittedError
from ..exceptions import NotFittedError


__all__ = ['HashingVectorizer',
Expand Down Expand Up @@ -390,28 +390,6 @@ def _check_stop_words_consistency(self, stop_words, preprocess, tokenize):
self._stop_words_id = id(self.stop_words)
return 'error'

def _validate_custom_analyzer(self):
# This is to check if the given custom analyzer expects file or a
# filename instead of data.
# Behavior changed in v0.21, function could be removed in v0.23
import tempfile
with tempfile.NamedTemporaryFile() as f:
fname = f.name
# now we're sure fname doesn't exist

msg = ("Since v0.21, vectorizers pass the data to the custom analyzer "
"and not the file names or the file objects. This warning "
"will be removed in v0.23.")
try:
self.analyzer(fname)
except FileNotFoundError:
warnings.warn(msg, ChangedBehaviorWarning)
except AttributeError as e:
if str(e) == "'str' object has no attribute 'read'":
warnings.warn(msg, ChangedBehaviorWarning)
except Exception:
pass

def build_analyzer(self):
"""Return a callable that handles preprocessing, tokenization
and n-grams generation.
Expand All @@ -424,8 +402,6 @@ def build_analyzer(self):
"""

if callable(self.analyzer):
if self.input in ['file', 'filename']:
self._validate_custom_analyzer()
return partial(
_analyze, analyzer=self.analyzer, decoder=self.decode
)
Expand Down