Skip to content

Commit

Permalink
style: Moved to ruff as the main linter
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethEnevoldsen committed Dec 9, 2023
1 parent 367ff15 commit 05c3de8
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 69 deletions.
6 changes: 2 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"sphinx_design",
]

# autodoc_mock_imports = ["dacy"]
# autodoc_mock_imports = ["dacy"] # noqa

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down Expand Up @@ -92,7 +92,7 @@
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
</svg>
""", # noqa: E501
""",
"class": "",
},
],
Expand All @@ -113,5 +113,3 @@
"navigation_with_keys": True,
}

# pygments_style = "monokai"
# pygments_dark_style = "monokai"
11 changes: 5 additions & 6 deletions docs/create_augmenters_table.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os

from wasabi import MarkdownRenderer

import augmenty
from wasabi import MarkdownRenderer

print("Running create_augmenters_table.py")

os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.chdir(os.path.dirname(os.path.abspath(__file__))) # noqa

md = MarkdownRenderer()
meta = augmenty.meta()
Expand Down Expand Up @@ -40,8 +39,8 @@
if not isinstance(r, list):
r = [r]
for ref_dict in r:
ref = f"{ref}: "
ref += md.link(
ref = f"{ref}: " # noqa
ref += md.link( # noqa
f"{ref_dict['authors']} ({ref_dict['year']})",
ref_dict["link"],
)
Expand Down Expand Up @@ -83,5 +82,5 @@
md.add(table)


with open("../docs/augmenters_overview.md", "w") as f: # type: ignore
with open("../docs/augmenters_overview.md", "w") as f: # type: ignore # noqa
f.write(md.text) # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,22 @@
"\n",
"import augmenty\n",
"\n",
"\n",
"# register the augmenter such with the name you want to specify in the config\n",
"@spacy.registry.augmenters(\"my_augmenter\")\n",
"def my_augmenters():\n",
" # create the augmenters you wish to use\n",
" keystroke_augmenter = augmenty.load(\n",
" \"keystroke_error_v1\", keyboard=\"en_qwerty_v1\", level=0.05 # 5% of characters might be too much\n",
" \"keystroke_error_v1\",\n",
" keyboard=\"en_qwerty_v1\",\n",
" level=0.05, # 5% of characters might be too much\n",
" )\n",
"\n",
" char_swap_augmenter = augmenty.load(\"char_swap_v1\", level=0.03)\n",
"\n",
" # combine them into a single augmenter to be used for training\n",
" # the order of the augmenters is important, as the first augmenter will be applied first\n",
" return augmenty.combine([keystroke_augmenter, char_swap_augmenter])\n",
"\n"
" return augmenty.combine([keystroke_augmenter, char_swap_augmenter])"
]
},
{
Expand Down Expand Up @@ -156,7 +158,7 @@
" augmented_texts = augmenty.texts(texts, augmenter, nlp=nlp)\n",
"\n",
" for text in augmented_texts:\n",
" print(text)\n"
" print(text)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
install:
@echo "--- 🚀 Installing project ---"
pip install -e ".[dev, docs, tests]"
pip install -e ".[dev, docs, tests,tutorials,all]"

static-type-check:
@echo "--- 🔍 Running static type check ---"
Expand Down
4 changes: 1 addition & 3 deletions src/augmenty/span/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ def ent_augmenter_v1(
if isinstance(ent_dict[ent.label_], Generator):
new_ent = next(ent_dict[ent.label_]) # type: ignore
else:
new_ent = random.sample(ent_dict[ent.label_], k=1)[ # type: ignore
0
]
new_ent = random.sample(ent_dict[ent.label_], k=1)[0] # type: ignore
if replace_consistency:
replaced_ents[ent.text] = new_ent

Expand Down
17 changes: 10 additions & 7 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
from typing import Any

import pytest
import spacy
from dacy.datasets import dane
from spacy.language import Language
from spacy.training import Example

from .books import BOOKS


# pipelines
@pytest.fixture()
def nlp_en():
def nlp_en() -> Language:
return spacy.blank("en")


@pytest.fixture()
def nlp_da():
def nlp_da() -> Language:
return spacy.blank("da")


@pytest.fixture()
def nlp_en_md():
def nlp_en_md() -> Language:
return spacy.load("en_core_web_md")


@pytest.fixture()
def dane_test(nlp_da):
return dane(splits=["test"])(nlp_da)
def dane_test(nlp_da: Language) -> Any:
return dane(splits=["test"])(nlp_da) # type: ignore


@pytest.fixture()
def books_w_annotations(nlp_en_md):
def books_w_annotations(nlp_en_md: Language) -> list:
docs = nlp_en_md.pipe(BOOKS)
return [Example(doc, doc) for doc in docs]


@pytest.fixture()
def books_without_annotations(nlp_en):
def books_without_annotations(nlp_en: Language) -> list:
docs = nlp_en.pipe(BOOKS)
return [Example(doc, doc) for doc in docs]
30 changes: 18 additions & 12 deletions tests/test_all_augmenters.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
"""Pytest script for testing all augmenters in a variety of cases."""

import numpy as np
import pytest
from typing import Callable

import augmenty
import numpy as np
import pytest
from spacy.language import Language
from spacy.tokens import Token

from .fixtures import books_without_annotations # noqa
from .fixtures import nlp_en # noqa
from .fixtures import books_w_annotations, dane_test, nlp_da, nlp_en_md # noqa
from .fixtures import (
books_w_annotations,
books_without_annotations,
dane_test,
nlp_da,
nlp_en,
nlp_en_md,
)

np.seterr(divide="raise", invalid="raise")


def is_pronoun(token):
def is_pronoun(token: Token) -> bool:
if token.pos_ == "PRON":
return True
return False
Expand Down Expand Up @@ -79,11 +87,11 @@ def is_pronoun(token):
}


@pytest.mark.parametrize("aug,args", [(k, augmenters_args[k]) for k in augmenters_args])
@pytest.mark.parametrize("aug,args", [(k, augmenters_args[k]) for k in augmenters_args]) # noqa
@pytest.mark.parametrize("level", [0.1, 0.5, 1])
@pytest.mark.timeout(100)
@pytest.mark.parametrize(
"examples,nlp",
"examples,nlp", # noqa
[
(pytest.lazy_fixture("dane_test"), pytest.lazy_fixture("nlp_da")),
(
Expand All @@ -92,12 +100,10 @@ def is_pronoun(token):
),
],
)
def test_augmenters(aug, args, examples, nlp, level):
def test_augmenters(aug: Callable, args: dict, examples, nlp: Language, level: float): # noqa
args["level"] = level
aug = augmenty.load(aug, **args)
augmented_examples = [ # noqa
e for ex in examples for e in aug(nlp=nlp, example=ex)
]
augmented_examples = [e for ex in examples for e in aug(nlp=nlp, example=ex)]


def test_check_untested():
Expand Down
7 changes: 3 additions & 4 deletions tests/test_augmentation_utilities.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from spacy.tokens import Doc, Span

import augmenty
from spacy.tokens import Doc, Span

from .fixtures import nlp_en, nlp_en_md # noqa
from .fixtures import nlp_en, nlp_en_md


def test_combine(nlp_en_md): # noqa F811
Expand Down Expand Up @@ -58,4 +57,4 @@ def test_set_doc_level(nlp_en): # noqa F811
aug = augmenty.set_doc_level(aug, level=0.5)

# simply testing it it runs
augmented_docs = list(augmenty.texts(texts, augmenter=aug, nlp=nlp_en)) # noqa
augmented_docs = list(augmenty.texts(texts, augmenter=aug, nlp=nlp_en))
5 changes: 2 additions & 3 deletions tests/test_character.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import spacy # type: ignore

import augmenty
import spacy # type: ignore

from .fixtures import nlp_da, nlp_en # noqa
from .fixtures import nlp_da, nlp_en


def test_create_random_casing_augmenter(nlp_en): # noqa F811
Expand Down
7 changes: 3 additions & 4 deletions tests/test_doc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from spacy.tokens import Span

import augmenty
from spacy.tokens import Span

from .fixtures import nlp_en # noqa
from .fixtures import nlp_en


def test_create_spongebob_augmenter(nlp_en): # noqa F811
Expand Down Expand Up @@ -39,7 +38,7 @@ def test_paragraph_subset_augmenter(nlp_en): # noqa F811
)

for t in doc:
t.is_sent_start = True if t.text == "." else False
t.is_sent_start = True if t.text == "." else False # noqa

p_subset_aug = augmenty.load(
"paragraph_subset_augmenter_v1",
Expand Down
16 changes: 8 additions & 8 deletions tests/test_issue_170.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
https://github.com/KennethEnevoldsen/augmenty/issues/170.
"""

import augmenty
import pytest
import spacy
from spacy.language import Language
from spacy.tokens import Doc, Span

import augmenty


@pytest.fixture
def nlp():
@pytest.fixture()
def nlp() -> Language:
nlp_ = spacy.blank("en")
nlp_.add_pipe("sentencizer")
return nlp_


@pytest.fixture
def example_doc(nlp) -> Doc:
@pytest.fixture()
def example_doc(nlp: Language) -> Doc:
text = "Joc Pederson and Thairo Estrada (concussion protocol) are each progressing. SS Brandon Crawford"
doc = nlp(text)
doc.ents = [
Expand Down Expand Up @@ -55,7 +55,7 @@ def example_doc(nlp) -> Doc:
return doc


def test_entity_with_no_dep(nlp, example_doc: Doc):
def test_entity_with_no_dep(nlp: Language, example_doc: Doc):
level = 1.0
docs = [example_doc]
augmenter = augmenty.load(
Expand All @@ -65,7 +65,7 @@ def test_entity_with_no_dep(nlp, example_doc: Doc):
replace_consistency=True,
resolve_dependencies=True,
)
aug_doc = list(augmenty.docs(docs, augmenter, nlp))[0]
aug_doc = list(augmenty.docs(docs, augmenter, nlp))[0] # noqa
assert len(aug_doc.ents) == len(docs[0].ents)
assert (
aug_doc.text
Expand Down
11 changes: 5 additions & 6 deletions tests/test_spans.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from typing import Callable

import augmenty
import pytest
from spacy.language import Language
from spacy.tokens import Doc

import augmenty

from .fixtures import nlp_en, nlp_en_md # noqa
from .fixtures import nlp_en, nlp_en_md


@pytest.fixture
@pytest.fixture()
def doc(nlp_en: Language) -> Doc: # noqa
doc = Doc(
nlp_en.vocab,
Expand All @@ -29,8 +28,8 @@ def doc(nlp_en: Language) -> Doc: # noqa
return doc


@pytest.fixture
def ent_augmenter():
@pytest.fixture()
def ent_augmenter() -> Callable:
ent_augmenter = augmenty.load(
"ents_replace_v1", # type: ignore
level=1.00,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_static_embedding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from augmenty.token.static_embedding_util import static_embedding

from .fixtures import nlp_en_md # noqa
from .fixtures import nlp_en_md


def test_static_embedding_util(nlp_en_md): # noqa F811
Expand Down
11 changes: 5 additions & 6 deletions tests/test_token.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import spacy # type: ignore
from spacy.tokens import Doc # type: ignore

import augmenty
import spacy # type: ignore
from augmenty.token.insert import create_token_insert_random_augmenter_v1
from spacy.tokens import Doc # type: ignore

from .books import BOOKS
from .fixtures import nlp_da, nlp_en, nlp_en_md # noqa
from .fixtures import nlp_da, nlp_en, nlp_en_md


def test_create_starting_case_augmenter(nlp_en): # noqa F811
Expand All @@ -30,7 +29,7 @@ def test_create_conditional_token_casing_augmenter(nlp_en): # noqa F811

doc = Doc(nlp_en.vocab, words=tokens, pos=pos, spaces=spaces)

def conditional(token):
def conditional(token): # noqa
if token.pos_ == "PRON":
return True
return False
Expand Down Expand Up @@ -181,7 +180,7 @@ def test_create_token_insert_augmenter(nlp_en): # noqa F811
words = ["cat"]
spaces = [False]
doc = Doc(nlp_en.vocab, words=words, spaces=spaces, pos=["NOUN"])
insert_fun = lambda t: {"ORTH": "word"} # noqa: E731
insert_fun = lambda: {"ORTH": "word"} # noqa: E731
aug = augmenty.load("token_insert_v1", level=1, insert=insert_fun)
docs = list(augmenty.docs([doc], augmenter=aug, nlp=nlp_en))
assert len(docs[0]) == 2
Expand Down

0 comments on commit 05c3de8

Please sign in to comment.