From 525c1a9e6d5a33075669085148247e2604dd092f Mon Sep 17 00:00:00 2001 From: manonBlanco <blanco@teklia.com> Date: Fri, 24 Nov 2023 13:18:51 +0100 Subject: [PATCH] Comply with ruff's PT rule --- nerval/evaluate.py | 2 +- pyproject.toml | 2 + tests/conftest.py | 20 +++--- tests/{ => fixtures}/test_mapping_file.csv | 0 tests/test_align.py | 2 +- tests/test_compute_matches.py | 2 +- tests/test_compute_scores.py | 2 +- tests/test_folder/test_demo_annot.bio | 82 ---------------------- tests/test_folder/test_demo_predict.bio | 81 --------------------- tests/test_folder/test_toy_annot.bio | 39 ---------- tests/test_folder/test_toy_predict.bio | 39 ---------- tests/test_get_labels_aligned.py | 2 +- tests/test_parse_bio.py | 30 +++++--- tests/test_run.py | 31 +++++--- 14 files changed, 56 insertions(+), 278 deletions(-) rename tests/{ => fixtures}/test_mapping_file.csv (100%) delete mode 100644 tests/test_folder/test_demo_annot.bio delete mode 100644 tests/test_folder/test_demo_predict.bio delete mode 100644 tests/test_folder/test_toy_annot.bio delete mode 100644 tests/test_folder/test_toy_predict.bio diff --git a/nerval/evaluate.py b/nerval/evaluate.py index 7feec7b..5afe7cc 100644 --- a/nerval/evaluate.py +++ b/nerval/evaluate.py @@ -374,7 +374,7 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool): recall += scores["All"]["R"] f1 += scores["All"]["F1"] else: - raise Exception(f"No file found for files {annot}, {predict}") + raise Exception(f"No file found for files {row[0]}, {row[1]}") if count: logger.info("Average score on all corpus") table = PrettyTable() diff --git a/pyproject.toml b/pyproject.toml index 28ef1df..24be6f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,4 +35,6 @@ select = [ "ARG", # flake8-use-pathlib "PTH", + # flake8-pytest-style + "PT", ] diff --git a/tests/conftest.py b/tests/conftest.py index 5381d25..61b7300 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,46 +5,46 @@ import pytest FIXTURES = Path(__file__).parent / "fixtures" -@pytest.fixture +@pytest.fixture() def fake_annot_bio(): return FIXTURES / "test_annot.bio" -@pytest.fixture +@pytest.fixture() def fake_predict_bio(): return FIXTURES / "test_predict.bio" -@pytest.fixture +@pytest.fixture() def empty_bio(): return FIXTURES / "test_empty.bio" -@pytest.fixture +@pytest.fixture() def bad_bio(): return FIXTURES / "test_bad.bio" -@pytest.fixture +@pytest.fixture() def bioeslu_bio(): return FIXTURES / "bioeslu.bio" -@pytest.fixture +@pytest.fixture() def end_of_file_bio(): return FIXTURES / "end_of_file.bio" -@pytest.fixture +@pytest.fixture() def nested_bio(): return FIXTURES / "test_nested.bio" -@pytest.fixture +@pytest.fixture() def folder_bio(): - return Path("test_folder") + return FIXTURES @pytest.fixture() def csv_file(): - return Path("test_mapping_file.csv") + return FIXTURES / "test_mapping_file.csv" diff --git a/tests/test_mapping_file.csv b/tests/fixtures/test_mapping_file.csv similarity index 100% rename from tests/test_mapping_file.csv rename to tests/fixtures/test_mapping_file.csv diff --git a/tests/test_align.py b/tests/test_align.py index 35be3ba..14725f2 100644 --- a/tests/test_align.py +++ b/tests/test_align.py @@ -3,7 +3,7 @@ import pytest @pytest.mark.parametrize( - "query,target", + ("query", "target"), [ ( "Gérard de Nerval was born in Paris in 1808 .", diff --git a/tests/test_compute_matches.py b/tests/test_compute_matches.py index 270c448..2cd8586 100644 --- a/tests/test_compute_matches.py +++ b/tests/test_compute_matches.py @@ -202,7 +202,7 @@ fake_predict_tags_bk_boundary_2 = [ @pytest.mark.parametrize( - "test_input, expected", + ("test_input", "expected"), [ ( ( diff --git a/tests/test_compute_scores.py b/tests/test_compute_scores.py index d51af08..239754f 100644 --- a/tests/test_compute_scores.py +++ b/tests/test_compute_scores.py @@ -4,7 +4,7 @@ from nerval import evaluate @pytest.mark.parametrize( - "annot,predict,matches", + ("annot", "predict", "matches"), [ ( {"All": 3, "DAT": 1, "LOC": 1, "PER": 1}, diff --git a/tests/test_folder/test_demo_annot.bio b/tests/test_folder/test_demo_annot.bio deleted file mode 100644 index cf16200..0000000 --- a/tests/test_folder/test_demo_annot.bio +++ /dev/null @@ -1,82 +0,0 @@ -Césaire B-PER -Alphonse I-PER -Garon I-PER -marraine O -Adeline B-PER -Dionne I-PER -, O -soussignés O -Lecture O -faite O -Adéline O -Dionne O -Arsène O -Côté O -Arpin O -R O -Le O -onze B-DAT -aout I-DAT -mil I-DAT -neuf I-DAT -cent I-DAT -un I-DAT -nous O -prêtre O -soussigné O -avons O -baptisé O -Marie B-PER -Luce I-PER -Louise I-PER -, O -née O -la B-DAT -veille I-DAT -, O -fille O -légitime O -de O -Carmel B-PER -Côté I-PER -, O -cordonnier B-OCC -, O -pré O -- O -sent O -, O -déclarant O -ne O -savoir O -signer O -, O -et O -de O -Eugé B-PER -nie I-PER -Fréchette I-PER -, O -de O -cette B-LOC -paroisse I-LOC -. O -Parrain O -Napoléon B-PER -Fréchette I-PER -, O -marraine O -Adeline B-PER -Tremblay I-PER -, O -soussignés O -, O -de O -Ste B-LOC -Luce I-LOC -, O -Lec O -- O -ture O -faite O -. O diff --git a/tests/test_folder/test_demo_predict.bio b/tests/test_folder/test_demo_predict.bio deleted file mode 100644 index 7e01c2d..0000000 --- a/tests/test_folder/test_demo_predict.bio +++ /dev/null @@ -1,81 +0,0 @@ -Césaire B-PER -Alphonse O -Garon B-PER -marraine O -Adeline B-PER -Dionne I-PER -, O -soussignés O -Lecture O -faite O -Adéline O -Dionne O -Arsène O -Côté O -Arpin O -R O -Le O -onze B-DAT -aout I-DAT -mil I-DAT -neuf I-DAT -cent I-DAT -un O -nous O -pretre O -soussigné O -avons O -baptisé O -Marie B-PER -Luce I-PER -Louise I-PER -, O -née O -la B-DAT -veille I-DAT -, O -fille O -légitime O -de O -Carmel B-PER -Côté I-PER -, O -cordonnier B-OCC -, O -pré O -- O -sent O -, O -déclarant O -ne O -savoir O -signer O -, O -et O -de O -Eugé B-PER -nie I-PER -Fréchette I-PER -, O -de O -cette B-LOC -paroisse I-LOC -. O -Parrain O -Napoléon B-PER -Fréchette I-PER -, O -marraine O -Adéline B-PER -Tremblay I-PER -, O -sousignés O -, O -de O -St B-LOC -. I-LOC -Luce O -, O -Lec O -ture O -faite O diff --git a/tests/test_folder/test_toy_annot.bio b/tests/test_folder/test_toy_annot.bio deleted file mode 100644 index 5a941ee..0000000 --- a/tests/test_folder/test_toy_annot.bio +++ /dev/null @@ -1,39 +0,0 @@ -John B-PER -Ronald I-PER -Reuel I-PER -Tolkien I-PER -was O -born O -on O -three B-DAT -January I-DAT -eighteen I-DAT -ninety I-DAT -- I-DAT -two I-DAT -in O -Bloemfontein B-LOC -in O -the O -Orange B-LOC -Free I-LOC -State I-LOC -, O -to O -Arthur B-PER -Reuel I-PER -Tolkien I-PER -, O -an O -English O -bank B-OCC -manager I-OCC -, O -and O -his O -wife O -Mabel B-PER -, O -née O -Suffield B-PER -. O diff --git a/tests/test_folder/test_toy_predict.bio b/tests/test_folder/test_toy_predict.bio deleted file mode 100644 index 5a941ee..0000000 --- a/tests/test_folder/test_toy_predict.bio +++ /dev/null @@ -1,39 +0,0 @@ -John B-PER -Ronald I-PER -Reuel I-PER -Tolkien I-PER -was O -born O -on O -three B-DAT -January I-DAT -eighteen I-DAT -ninety I-DAT -- I-DAT -two I-DAT -in O -Bloemfontein B-LOC -in O -the O -Orange B-LOC -Free I-LOC -State I-LOC -, O -to O -Arthur B-PER -Reuel I-PER -Tolkien I-PER -, O -an O -English O -bank B-OCC -manager I-OCC -, O -and O -his O -wife O -Mabel B-PER -, O -née O -Suffield B-PER -. O diff --git a/tests/test_get_labels_aligned.py b/tests/test_get_labels_aligned.py index b4daa39..4e0650e 100644 --- a/tests/test_get_labels_aligned.py +++ b/tests/test_get_labels_aligned.py @@ -97,7 +97,7 @@ expected_predict_tags_aligned = [ @pytest.mark.parametrize( - "test_input, expected", + ("test_input", "expected"), [ ( (fake_annot_original, fake_annot_aligned, fake_annot_tags_original), diff --git a/tests/test_parse_bio.py b/tests/test_parse_bio.py index 66f8957..5f80734 100644 --- a/tests/test_parse_bio.py +++ b/tests/test_parse_bio.py @@ -1,3 +1,5 @@ +import re + import pytest from nerval import evaluate @@ -168,7 +170,7 @@ expected_parsed_end_of_file = { @pytest.mark.parametrize( - "test_input, expected", + ("test_input", "expected"), [ (pytest.lazy_fixture("fake_annot_bio"), expected_parsed_annot), (pytest.lazy_fixture("fake_predict_bio"), expected_parsed_predict), @@ -183,18 +185,21 @@ def test_parse_bio(test_input, expected): def test_parse_bio_bad_input(bad_bio): - with pytest.raises(Exception): - evaluate.parse_bio(bad_bio) + lines = bad_bio.read_text().strip().splitlines() + with pytest.raises( + Exception, match=re.escape("The file is not in BIO format: check line 1 (file)") + ): + evaluate.parse_bio(lines) @pytest.mark.parametrize( - "line, word, label", - ( + ("line", "word", "label"), + [ ("Hi B-ORG", "Hi", "B-ORG"), ("Hi B-Org or maybe not org", "Hi", "B-Org or maybe not org"), ("1258 B-Date et Lieu", "1258", "B-Date et Lieu"), ("Devoti B-Sous-titre", "Devoti", "B-Sous-titre"), - ), + ], ) def test_parse_line(line, word, label): assert parse_line(index=0, line=line) == (word, label) @@ -202,21 +207,24 @@ def test_parse_line(line, word, label): @pytest.mark.parametrize( "line", - (("HiB-ORG"), ("HiB-ORG or maybe not")), + [("HiB-ORG"), ("HiB-ORG or maybe not")], ) def test_parse_line_crash(line): - with pytest.raises(Exception): + with pytest.raises( + Exception, + match=re.escape(f"The file is not in BIO format: check line 0 ({line})"), + ): parse_line(index=0, line=line) @pytest.mark.parametrize( - "label, expected_type", - ( + ("label", "expected_type"), + [ ("B-ORG", "ORG"), ("B-Date et Lieu", "Date et Lieu"), ("I-Date et Lieu", "Date et Lieu"), ("B-Sous-titre", "Sous-titre"), - ), + ], ) def test_get_type_label(label, expected_type): assert get_type_label(label) == expected_type diff --git a/tests/test_run.py b/tests/test_run.py index 2381dd6..63b8ebe 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,11 +1,14 @@ +import re +from pathlib import Path + import pytest from nerval import evaluate @pytest.mark.parametrize( - "annotation, prediction, expected", - ( + ("annotation", "prediction", "expected"), + [ ( pytest.lazy_fixture("fake_annot_bio"), pytest.lazy_fixture("fake_predict_bio"), @@ -82,7 +85,7 @@ from nerval import evaluate }, }, ), - ), + ], ) def test_run(annotation, prediction, expected): assert ( @@ -97,16 +100,22 @@ def test_run(annotation, prediction, expected): def test_run_empty_bio(empty_bio): - with pytest.raises(Exception): - evaluate.run(empty_bio, empty_bio, 0.3) + with pytest.raises( + Exception, match="No content found in annotation or prediction files." + ): + evaluate.run(empty_bio, empty_bio, 0.3, False) def test_run_empty_entry(): - with pytest.raises(TypeError): - evaluate.run(None, None, 0.3) + with pytest.raises( + AssertionError, + match=re.escape("Error: Input file invalid.bio does not exist"), + ): + evaluate.run(Path("invalid.bio"), Path("invalid.bio"), 0.3, False) -@pytest.mark.parametrize("threshold", ([0.3])) -def test_run_multiple(csv_file, folder_bio, threshold): - with pytest.raises(Exception): - evaluate.run_multiple(csv_file, folder_bio, threshold) +def test_run_multiple(csv_file, folder_bio): + with pytest.raises( + Exception, match="No file found for files demo_annot.bio, demo_predict.bio" + ): + evaluate.run_multiple(csv_file, folder_bio, 0.3, False) -- GitLab