From 4b2c56340a9e501e46c7489f2d91f9de6396e8cd Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 9 Oct 2021 15:42:09 +0200 Subject: [PATCH 1/3] LGTM.com error: Modification of parameter with default Default value flows to here and is mutated. --- nltk/util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nltk/util.py b/nltk/util.py index da843c3dc4..2b41871fa3 100644 --- a/nltk/util.py +++ b/nltk/util.py @@ -1013,7 +1013,7 @@ def skipgrams(sequence, n, k, **kwargs): ###################################################################### # inherited from pywordnet, by Oliver Steele -def binary_search_file(file, key, cache={}, cacheDepth=-1): +def binary_search_file(file, key, cache=None, cacheDepth=-1): """ Return the line from the file with first word key. Searches through a sorted file using the binary search algorithm. @@ -1036,6 +1036,9 @@ def binary_search_file(file, key, cache={}, cacheDepth=-1): end = file.tell() - 1 file.seek(0) + if cache is None: + cache = {} + while start < end: lastState = start, end middle = (start + end) // 2 From f5541290614892aad411561e2bf0e951c0b5b314 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 9 Oct 2021 15:45:34 +0200 Subject: [PATCH 2/3] LGTM.com error: Wrong number of arguments in a class instantiation Call to ReadError.__init__ with too few arguments; should be no fewer than 2. --- nltk/internals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nltk/internals.py b/nltk/internals.py index 0aa36b7ccf..1d5dbad474 100644 --- a/nltk/internals.py +++ b/nltk/internals.py @@ -242,7 +242,7 @@ def read_str(s, start_position): try: return eval(s[start_position : match.end()]), match.end() except ValueError as e: - raise ReadError("invalid string (%s)" % e) from e + raise ReadError("valid escape sequence", start_position) from e _READ_INT_RE = re.compile(r"-?\d+") From 8ce97b27877bb3a177954393f58fea9f74c0ffec Mon Sep 17 00:00:00 2001 From: Danny Sepler Date: Sun, 10 Oct 2021 16:28:37 -0400 Subject: [PATCH 3/3] Add a unit test, fix typos --- nltk/internals.py | 2 +- nltk/test/internals.doctest | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/nltk/internals.py b/nltk/internals.py index 1d5dbad474..ba92adb168 100644 --- a/nltk/internals.py +++ b/nltk/internals.py @@ -238,7 +238,7 @@ def read_str(s, start_position): break # Process it, using eval. Strings with invalid escape sequences - # might raise ValueEerror. + # might raise ValueError. try: return eval(s[start_position : match.end()]), match.end() except ValueError as e: diff --git a/nltk/test/internals.doctest b/nltk/test/internals.doctest index 742db1c287..38688c3272 100644 --- a/nltk/test/internals.doctest +++ b/nltk/test/internals.doctest @@ -138,3 +138,23 @@ It works for classic classes, too: False >>> overridden(D().f) True + + +read_str() +~~~~~~~~~~~~ + >>> from nltk.internals import read_str + +Test valid scenarios + + >>> read_str("'valid string'", 0) + ('valid string', 14) + +Now test invalid scenarios + >>> read_str("should error", 0) + Traceback (most recent call last): + ... + nltk.internals.ReadError: Expected open quote at 0 + >>> read_str("'should error", 0) + Traceback (most recent call last): + ... + nltk.internals.ReadError: Expected close quote at 1