Skip to content

Commit 635708c

Browse files
authored
fix: correct codeql warnings (jxmorris12#196)
* `__eq__` not overridden when adding attributes * Module is imported with 'import' and 'import from' * Should use a 'with' statement * Statement has no effect * Unused global variable
1 parent 80c4c71 commit 635708c

4 files changed

Lines changed: 74 additions & 36 deletions

File tree

‎src/language_tool_python/_internals/utils.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,10 @@
2424

2525
logger = logging.getLogger(__name__)
2626

27-
JAR_NAMES = [
28-
"languagetool-server.jar",
29-
"LanguageTool.jar",
30-
]
3127
FAILSAFE_LANGUAGE = "en"
3228

3329
LTP_PATH_ENV_VAR = "LTP_PATH" # LanguageTool download path
3430

35-
# Directory containing the LanguageTool jar file:
36-
LTP_JAR_DIR_PATH_ENV_VAR = "LTP_JAR_DIR_PATH"
37-
3831

3932
def parse_url(url_str: str) -> str:
4033
"""Parse the given URL string and ensure it has a scheme.
@@ -189,7 +182,6 @@ class SupportsBool(Protocol):
189182

190183
def __bool__(self) -> bool:
191184
"""Define the interface for types that can be evaluated in a boolean context."""
192-
...
193185

194186

195187
def version_tuple(v: str) -> tuple[int, int]:

‎src/language_tool_python/download_lt.py‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from ._internals.compat import toml_loads
2727
from ._internals.safe_zip import SafeZipExtractor
2828
from ._internals.utils import (
29-
LTP_JAR_DIR_PATH_ENV_VAR,
3029
get_env_int,
3130
get_language_tool_download_path,
3231
version_tuple,
@@ -80,6 +79,7 @@
8079
_LTP_DOWNLOAD_SHA256_ENV_VAR = "LTP_DOWNLOAD_SHA256"
8180
_LTP_BYPASS_VERIFIED_DOWNLOADS_ENV_VAR = "LTP_BYPASS_VERIFIED_DOWNLOADS"
8281
_LTP_MAX_DOWNLOAD_BYTES_ENV_VAR = "LTP_MAX_DOWNLOAD_BYTES"
82+
_LTP_JAR_DIR_PATH_ENV_VAR = "LTP_JAR_DIR_PATH"
8383
_DOWNLOAD_CHUNK_BYTES = 1024 * 1024
8484
_SAFE_ZIP_EXTRACTOR = SafeZipExtractor()
8585

@@ -746,7 +746,7 @@ def download(self) -> None:
746746

747747
# Use the env var to the jar directory if it is defined
748748
# otherwise look in the download directory
749-
if os.environ.get(LTP_JAR_DIR_PATH_ENV_VAR):
749+
if os.environ.get(_LTP_JAR_DIR_PATH_ENV_VAR):
750750
return
751751

752752
if self not in self.get_installed_versions():
@@ -815,6 +815,28 @@ def download_url(self) -> str:
815815
# Versions < 6.0 from archive
816816
return urljoin(_BASE_URL_ARCHIVE, filename)
817817

818+
def __eq__(self, other: object) -> bool:
819+
"""Check equality between two ReleaseLocalLanguageTool instances.
820+
821+
Two instances are considered equal if they have the same version name.
822+
823+
:param other: The object to compare with.
824+
:type other: object
825+
:return: True if the instances are equal, False otherwise.
826+
:rtype: bool
827+
"""
828+
return super().__eq__(other)
829+
830+
def __hash__(self) -> int:
831+
"""Return the hash of the ReleaseLocalLanguageTool instance.
832+
833+
If the version name is modified, the hash will change.
834+
835+
:return: The hash of the version name.
836+
:rtype: int
837+
"""
838+
return super().__hash__()
839+
818840

819841
class SnapshotLocalLanguageTool(LocalLanguageTool):
820842
"""Represents a snapshot (development) version of LanguageTool.
@@ -855,7 +877,7 @@ def download(self) -> None:
855877

856878
# Use the env var to the jar directory if it is defined
857879
# otherwise look in the download directory
858-
if os.environ.get(LTP_JAR_DIR_PATH_ENV_VAR):
880+
if os.environ.get(_LTP_JAR_DIR_PATH_ENV_VAR):
859881
return
860882

861883
if self not in self.get_installed_versions():
@@ -932,3 +954,25 @@ def download_url(self) -> str:
932954
"""
933955
filename = _FILENAME_SNAPSHOT.format(version=self._version_name)
934956
return urljoin(_BASE_URL_SNAPSHOT, filename)
957+
958+
def __eq__(self, other: object) -> bool:
959+
"""Check equality between two SnapshotLocalLanguageTool instances.
960+
961+
Two instances are considered equal if they have the same version name.
962+
963+
:param other: The object to compare with.
964+
:type other: object
965+
:return: True if the instances are equal, False otherwise.
966+
:rtype: bool
967+
"""
968+
return super().__eq__(other)
969+
970+
def __hash__(self) -> int:
971+
"""Return the hash of the SnapshotLocalLanguageTool instance.
972+
973+
If the version name is modified, the hash will change.
974+
975+
:return: The hash of the version name.
976+
:rtype: int
977+
"""
978+
return super().__hash__()

‎tests/test_cli.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,10 @@ def remote_server() -> Generator[tuple[str, int], None, None]:
7474
:return: A tuple containing the server host and port (host, port).
7575
:rtype: Generator[Tuple[str, int], None, None]
7676
"""
77-
tool = language_tool_python.LanguageTool("en-US")
78-
host = tool._host
79-
port = tool._port
80-
81-
try:
77+
with language_tool_python.LanguageTool("en-US") as tool:
78+
host = tool._host
79+
port = tool._port
8280
yield host, port
83-
finally:
84-
tool.close()
8581

8682

8783
def test_cli_remote_ok(remote_server: tuple[str, int]) -> None:

‎tests/test_download.py‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import pytest
1818

1919
import language_tool_python
20-
from language_tool_python import download_lt
2120
from language_tool_python.download_lt import (
2221
_LTP_BYPASS_VERIFIED_DOWNLOADS_ENV_VAR,
2322
_LTP_DOWNLOAD_SHA256_ENV_VAR,
@@ -186,7 +185,7 @@ def test_http_get_rejects_oversized_content_length(
186185
)
187186
response = MockDownloadResponse(payload)
188187
response.headers["Content-Length"] = "2"
189-
monkeypatch.setattr(download_lt, "_MAX_DOWNLOAD_BYTES", 1)
188+
monkeypatch.setattr(language_tool_python.download_lt, "_MAX_DOWNLOAD_BYTES", 1)
190189

191190
with (
192191
patch(
@@ -207,11 +206,14 @@ def test_max_download_bytes_uses_env_override(
207206
env.setenv(
208207
_LTP_MAX_DOWNLOAD_BYTES_ENV_VAR, str(EXPECTED_DOWNLOAD_BYTES_OVERRIDE)
209208
)
210-
importlib.reload(download_lt)
209+
importlib.reload(language_tool_python.download_lt)
211210

212-
assert download_lt._MAX_DOWNLOAD_BYTES == EXPECTED_DOWNLOAD_BYTES_OVERRIDE
211+
assert (
212+
language_tool_python.download_lt._MAX_DOWNLOAD_BYTES
213+
== EXPECTED_DOWNLOAD_BYTES_OVERRIDE
214+
)
213215
finally:
214-
importlib.reload(download_lt)
216+
importlib.reload(language_tool_python.download_lt)
215217

216218

217219
def test_http_get_rejects_oversized_stream(
@@ -223,7 +225,9 @@ def test_http_get_rejects_oversized_stream(
223225
)
224226
response = MockDownloadResponse(payload)
225227
response.headers = {}
226-
monkeypatch.setattr(download_lt, "_MAX_DOWNLOAD_BYTES", len(payload) - 1)
228+
monkeypatch.setattr(
229+
language_tool_python.download_lt, "_MAX_DOWNLOAD_BYTES", len(payload) - 1
230+
)
227231

228232
with (
229233
patch(
@@ -244,7 +248,9 @@ def test_http_get_rejects_oversized_stream_with_small_content_length(
244248
)
245249
response = MockDownloadResponse(payload)
246250
response.headers["Content-Length"] = "1"
247-
monkeypatch.setattr(download_lt, "_MAX_DOWNLOAD_BYTES", len(payload) - 1)
251+
monkeypatch.setattr(
252+
language_tool_python.download_lt, "_MAX_DOWNLOAD_BYTES", len(payload) - 1
253+
)
248254

249255
with (
250256
patch(
@@ -279,13 +285,13 @@ def test_latest_snapshot_uses_latest_download_url_and_current_date(
279285
) -> None:
280286
"""Test that latest remains a snapshot alias installed under the current date."""
281287
monkeypatch.setattr(
282-
download_lt,
288+
language_tool_python.download_lt,
283289
"_BASE_URL_SNAPSHOT",
284290
"https://example.test/snapshots/",
285291
)
286292

287293
FixedDatetime.current_datetime = datetime(2024, 5, 14, tzinfo=timezone.utc)
288-
monkeypatch.setattr(download_lt, "datetime", FixedDatetime)
294+
monkeypatch.setattr(language_tool_python.download_lt, "datetime", FixedDatetime)
289295
local_language_tool = LocalLanguageTool.from_version_name("latest")
290296

291297
assert local_language_tool.version_name == "20240514"
@@ -302,7 +308,7 @@ def test_release_download_url_uses_new_release_base_from_6_7(
302308
) -> None:
303309
"""Test that releases 6.7 and newer include the version in the base URL."""
304310
monkeypatch.setattr(
305-
download_lt,
311+
language_tool_python.download_lt,
306312
"_BASE_URL_NEW_RELEASES",
307313
"https://example.test/releases/LanguageTool-{version}/",
308314
)
@@ -322,7 +328,7 @@ def test_release_download_url_keeps_main_release_base_for_6_6(
322328
) -> None:
323329
"""Test that release 6.6 keeps using the versioned filename."""
324330
monkeypatch.setattr(
325-
download_lt,
331+
language_tool_python.download_lt,
326332
"_BASE_URL_RELEASE",
327333
"https://example.test/download/",
328334
)
@@ -340,7 +346,7 @@ def test_release_download_url_keeps_main_release_base_before_6_7(
340346
) -> None:
341347
"""Test that earlier 6.x releases keep using the versioned filename."""
342348
monkeypatch.setattr(
343-
download_lt,
349+
language_tool_python.download_lt,
344350
"_BASE_URL_RELEASE",
345351
"https://example.test/download/",
346352
)
@@ -358,7 +364,7 @@ def test_release_download_url_keeps_archive_base_before_6_0(
358364
) -> None:
359365
"""Test that older supported releases keep using the archive base URL."""
360366
monkeypatch.setattr(
361-
download_lt,
367+
language_tool_python.download_lt,
362368
"_BASE_URL_ARCHIVE",
363369
"https://example.test/archive/",
364370
)
@@ -486,7 +492,7 @@ def test_snapshot_download_renames_archive_root_to_requested_date(
486492
)
487493
local_language_tool = LocalLanguageTool.from_version_name(requested_snapshot)
488494
monkeypatch.setattr(
489-
download_lt,
495+
language_tool_python.download_lt,
490496
"_confirm_java_compatibility",
491497
skip_java_compatibility_check,
492498
)
@@ -499,7 +505,7 @@ def test_snapshot_download_renames_archive_root_to_requested_date(
499505
),
500506
):
501507
monkeypatch.setattr(
502-
download_lt,
508+
language_tool_python.download_lt,
503509
"get_language_tool_download_path",
504510
lambda: temp_dir,
505511
)
@@ -525,10 +531,10 @@ def test_latest_snapshot_download_renames_archive_root_to_current_date(
525531
{"LanguageTool-6.9-SNAPSHOT/languagetool-server.jar": b"jar"},
526532
)
527533
FixedDatetime.current_datetime = datetime(2024, 5, 14, tzinfo=timezone.utc)
528-
monkeypatch.setattr(download_lt, "datetime", FixedDatetime)
534+
monkeypatch.setattr(language_tool_python.download_lt, "datetime", FixedDatetime)
529535
local_language_tool = LocalLanguageTool.from_version_name("latest")
530536
monkeypatch.setattr(
531-
download_lt,
537+
language_tool_python.download_lt,
532538
"_confirm_java_compatibility",
533539
skip_java_compatibility_check,
534540
)
@@ -541,7 +547,7 @@ def test_latest_snapshot_download_renames_archive_root_to_current_date(
541547
),
542548
):
543549
monkeypatch.setattr(
544-
download_lt,
550+
language_tool_python.download_lt,
545551
"get_language_tool_download_path",
546552
lambda: temp_dir,
547553
)

0 commit comments

Comments
 (0)