Skip to content

Commit caadbc7

Browse files
author
rhliang
committed
Some work on fixing the tests under older Python versions.
Also a bit of bookkeeping to keep the build process happy.
1 parent 9fd6c71 commit caadbc7

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ package = true
102102

103103
[tool.uv-dynamic-versioning]
104104
vcs = "git"
105-
style = "semver"
105+
style = "pep440"
106106
fallback-version = "0.0.0"
107107

108108
[tool.pytest.ini_options]

src/hla_algorithm/hla_algorithm.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def load_default_hla_standards() -> LoadedStandards:
134134
:return: List of known HLA standards
135135
:rtype: list[HLAStandard]
136136
"""
137-
standards_filename: str = os.path.join(
137+
standards_filename: str = HLAAlgorithm._path_join_shim(
138138
os.path.dirname(__file__),
139139
"default_data",
140140
"hla_standards.yaml",
@@ -192,6 +192,13 @@ def read_hla_frequencies(
192192
hla_freqs[locus][protein_pair] += 1
193193
return hla_freqs
194194

195+
@staticmethod
196+
def _path_join_shim(*args) -> str:
197+
"""
198+
A shim for os.path.join which allows us to mock out the method easily in testing.
199+
"""
200+
return os.path.join(*args)
201+
195202
@staticmethod
196203
def load_default_hla_frequencies() -> dict[HLA_LOCUS, dict[HLAProteinPair, int]]:
197204
"""
@@ -201,7 +208,7 @@ def load_default_hla_frequencies() -> dict[HLA_LOCUS, dict[HLAProteinPair, int]]
201208
:rtype: dict[HLA_LOCUS, dict[HLAProteinPair, int]]
202209
"""
203210
hla_freqs: dict[HLA_LOCUS, dict[HLAProteinPair, int]]
204-
default_frequencies_filename: str = os.path.join(
211+
default_frequencies_filename: str = HLAAlgorithm._path_join_shim(
205212
os.path.dirname(__file__),
206213
"default_data",
207214
"hla_frequencies.csv",

tests/hla_algorithm_test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from io import StringIO
55
from pathlib import Path
66
from typing import Optional, cast
7-
from unittest.mock import MagicMock, _Call
7+
from unittest.mock import MagicMock, _Call, patch
88

99
import numpy as np
1010
import pytest
@@ -1630,6 +1630,12 @@ def test_interpret_error_cases(
16301630
}
16311631

16321632

1633+
def test_path_join_shim():
1634+
expected_result: str = "/foo/bar/baz"
1635+
result: str = HLAAlgorithm._path_join_shim("/foo/bar", "baz")
1636+
assert expected_result == result
1637+
1638+
16331639
@pytest.mark.parametrize(
16341640
"raw_standards, raw_expected_result",
16351641
[
@@ -1743,9 +1749,7 @@ def test_read_hla_standards(
17431749
# Also try reading it from a file.
17441750
p = tmp_path / "hla_standards.yaml"
17451751
p.write_text(standards_file_str)
1746-
dirname_return_mock: MagicMock = mocker.MagicMock()
1747-
mocker.patch.object(os.path, "dirname", return_value=dirname_return_mock)
1748-
mocker.patch.object(os.path, "join", return_value=str(p))
1752+
mocker.patch.object(HLAAlgorithm, "_path_join_shim", return_value=str(p))
17491753
load_result: LoadedStandards = HLAAlgorithm.load_default_hla_standards()
17501754
assert load_result == expected_result
17511755

@@ -2078,9 +2082,7 @@ def test_read_hla_frequencies(
20782082
# Now try loading these from a file.
20792083
p = tmp_path / "hla_frequencies.csv"
20802084
p.write_text(frequencies_str)
2081-
dirname_return_mock: MagicMock = mocker.MagicMock()
2082-
mocker.patch.object(os.path, "dirname", return_value=dirname_return_mock)
2083-
mocker.patch.object(os.path, "join", return_value=str(p))
2085+
mocker.patch.object(HLAAlgorithm, "_path_join_shim", return_value=str(p))
20842086
load_result: dict[HLA_LOCUS, dict[HLAProteinPair, int]] = (
20852087
HLAAlgorithm.load_default_hla_frequencies()
20862088
)
@@ -2176,10 +2178,13 @@ def test_use_config_all_defaults(
21762178
freq_path.write_text(fake_frequencies_str)
21772179

21782180
mocker.patch.object(
2179-
os.path, "join", side_effect=[str(standards_path), str(freq_path)]
2181+
HLAAlgorithm,
2182+
"_path_join_shim",
2183+
side_effect=[os.fspath(standards_path), os.fspath(freq_path)],
21802184
)
21812185

21822186
hla_algorithm: HLAAlgorithm = HLAAlgorithm.use_config()
2187+
21832188
assert hla_algorithm.tag == fake_stored_standards.tag
21842189
assert hla_algorithm.last_updated == fake_stored_standards.last_updated
21852190
assert hla_algorithm.hla_standards == READ_HLA_STANDARDS_TYPICAL_CASE_OUTPUT

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)