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
Final py3.9 deprecation #2801
Merged
Merged
Final py3.9 deprecation #2801
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,134 @@ | ||
""" | ||
Unit tests for nltk.util. | ||
""" | ||
|
||
import unittest | ||
|
||
from nltk.util import everygrams | ||
|
||
|
||
class TestEverygrams(unittest.TestCase): | ||
def setUp(self): | ||
"""Form test data for tests.""" | ||
self.test_data = iter("a b c".split()) | ||
|
||
def test_everygrams_without_padding(self): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = everygrams(self.test_data) | ||
self.assertCountEqual(output, expected_output) | ||
|
||
def test_everygrams_max_len(self): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = everygrams(self.test_data, max_len=2) | ||
self.assertCountEqual(output, expected_output) | ||
|
||
def test_everygrams_min_len(self): | ||
expected_output = [ | ||
("a", "b"), | ||
("b", "c"), | ||
("a", "b", "c"), | ||
] | ||
output = everygrams(self.test_data, min_len=2) | ||
self.assertCountEqual(output, expected_output) | ||
|
||
def test_everygrams_pad_right(self): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("b", "c", None), | ||
("c",), | ||
("c", None), | ||
("c", None, None), | ||
(None,), | ||
(None, None), | ||
(None,), | ||
] | ||
output = everygrams(self.test_data, max_len=3, pad_right=True) | ||
self.assertCountEqual(output, expected_output) | ||
|
||
def test_everygrams_pad_left(self): | ||
expected_output = [ | ||
(None,), | ||
(None, None), | ||
(None, None, "a"), | ||
(None,), | ||
(None, "a"), | ||
(None, "a", "b"), | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = everygrams(self.test_data, max_len=3, pad_left=True) | ||
self.assertCountEqual(output, expected_output) | ||
import pytest | ||
|
||
from nltk.util import everygrams, usage | ||
|
||
|
||
def test_usage_with_self(capsys): | ||
class MyClass: | ||
def kwargs(self, a=1): | ||
... | ||
|
||
def no_args(self): | ||
... | ||
|
||
def pos_args(self, a, b): | ||
... | ||
|
||
def pos_args_and_kwargs(self, a, b, c=1): | ||
... | ||
|
||
usage(MyClass) | ||
|
||
captured = capsys.readouterr() | ||
assert captured.out == ( | ||
"MyClass supports the following operations:\n" | ||
" - self.kwargs(a=1)\n" | ||
" - self.no_args()\n" | ||
" - self.pos_args(a, b)\n" | ||
" - self.pos_args_and_kwargs(a, b, c=1)\n" | ||
) | ||
|
||
|
||
def test_usage_with_cls(capsys): | ||
class MyClass: | ||
@classmethod | ||
def clsmethod(cls): | ||
... | ||
|
||
@classmethod | ||
def clsmethod_with_args(cls, a, b, c=1): | ||
... | ||
|
||
usage(MyClass) | ||
|
||
captured = capsys.readouterr() | ||
assert captured.out == ( | ||
"MyClass supports the following operations:\n" | ||
" - cls.clsmethod()\n" | ||
" - cls.clsmethod_with_args(a, b, c=1)\n" | ||
) | ||
|
||
|
||
def test_usage_on_builtin(): | ||
# just check the func passes, since | ||
# builtins change each python version | ||
usage(dict) | ||
|
||
|
||
@pytest.fixture | ||
def everygram_input(): | ||
"""Form test data for tests.""" | ||
return iter(["a", "b", "c"]) | ||
|
||
|
||
def test_everygrams_without_padding(everygram_input): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = list(everygrams(everygram_input)) | ||
assert output == expected_output | ||
|
||
|
||
def test_everygrams_max_len(everygram_input): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = list(everygrams(everygram_input, max_len=2)) | ||
assert output == expected_output | ||
|
||
|
||
def test_everygrams_min_len(everygram_input): | ||
expected_output = [ | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b", "c"), | ||
] | ||
output = list(everygrams(everygram_input, min_len=2)) | ||
assert output == expected_output | ||
|
||
|
||
def test_everygrams_pad_right(everygram_input): | ||
expected_output = [ | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("b", "c", None), | ||
("c",), | ||
("c", None), | ||
("c", None, None), | ||
(None,), | ||
(None, None), | ||
(None,), | ||
] | ||
output = list(everygrams(everygram_input, max_len=3, pad_right=True)) | ||
assert output == expected_output | ||
|
||
|
||
def test_everygrams_pad_left(everygram_input): | ||
expected_output = [ | ||
(None,), | ||
(None, None), | ||
(None, None, "a"), | ||
(None,), | ||
(None, "a"), | ||
(None, "a", "b"), | ||
("a",), | ||
("a", "b"), | ||
("a", "b", "c"), | ||
("b",), | ||
("b", "c"), | ||
("c",), | ||
] | ||
output = list(everygrams(everygram_input, max_len=3, pad_left=True)) | ||
assert output == expected_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could alternatively delete this method? it's only used in
nltk/test/classify.doctest
and that usage feels unnecessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unaware of this function, but I could definitely see how it has a nice educatory use. Being able to quickly see the useful methods of a class can be quite useful if you're not entirely aware of the codebase, especially considering that NLTK is definitely not as well documented as I'd like.
There are some classes in NLTK for which users definitely might like to get an overview of the methods that it supports.
nltk.tree.Tree
,nltk.BigramCollocationFinder
andnltk.FreqDist
come to mind, but upon trying the last one, I get the following exception:Potentially this is because
FreqDist
subclasses collections'Counter
, which again subclassesdict
. Note that I also got a similar exception before this PR.As far as I'm concerned, this can still be merged, as the bug existed before this already regardless. We can just open an issue for it then. However, if you feel like it, you can also include a fix for this in this PR already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!! you're totally right, thanks!
turns out that
inspect
and some builtin functions don't always work out together. i can't really figure out how to get the failing ones working, my best workaround is to skip them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, what about Python's built-in
help
command? It gives you both methods and attributes as well as their docstrings. I use it extensively with all kinds of codebases.I completely agree that the docs need some love, but that's a separate issue imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's indeed a separate issue I'd say.
I'd even consider
usage
failing on some builtin functions to be a separate issue. If you'd like to get this merged, then we can just open an issue saying thatusage
is broken in some cases. That is, assuming that the bug still exists.Regarding the tests, it might be a good idea to check if the output
.startswith("MyClass supports the following operations:")
or something, or whether the output is indeed a string with at least length ~35 or something. It's not much, but then at least we have some tests to verify that it prints a string with"... supports the following operations: ..."
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's just treat that as a separate issue.