Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update click and mypy #23

Merged
merged 5 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/constraints.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pip==21.1.2
nox==2021.6.6
nox-poetry==0.8.5
nox==2021.6.12
nox-poetry==0.8.6
poetry==1.1.6
virtualenv==20.4.7
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ repos:
language: system
types: [text]
stages: [commit, push, manual]
- repo: https://github.com/prettier/pre-commit
rev: v2.1.2
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.3.0
hooks:
- id: prettier
20 changes: 0 additions & 20 deletions mypy.ini

This file was deleted.

23 changes: 15 additions & 8 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
from textwrap import dedent

import nox
from nox_poetry import Session
from nox_poetry import session

try:
from nox_poetry import Session
from nox_poetry import session
except ImportError:
message = f"""\
Nox failed to import the 'nox-poetry' package.
Please install it using the following command:
{sys.executable} -m pip install nox-poetry"""
raise SystemExit(dedent(message))


package = "aalto_asr_preprocessor"
python_versions = ["3.9", "3.8"]
nox.needs_version = ">= 2021.6.6"
nox.options.sessions = "pre-commit", "safety", "mypy", "tests", "docs-build"


Expand Down Expand Up @@ -112,20 +122,17 @@ def tests(session: Session) -> None:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
finally:
if session.interactive:
session.notify("coverage")
session.notify("coverage", posargs=[])


@session
def coverage(session: Session) -> None:
"""Produce the coverage report."""
# Do not use session.posargs unless this is the only session.
nsessions = len(session._runner.manifest)
has_args = session.posargs and nsessions == 1
args = session.posargs if has_args else ["report"]
args = session.posargs or ["report"]

session.install("coverage[toml]")

if not has_args and any(Path().glob(".coverage.*")):
if not session.posargs and any(Path().glob(".coverage.*")):
session.run("coverage", "combine")

session.run("coverage", *args)
Expand Down
131 changes: 48 additions & 83 deletions poetry.lock

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ repository = "https://github.com/aalto-speech/aalto-asr-preprocessor"
version = "2021.6.16"

[tool.poetry.dependencies]
click = "^7.1.2"
click = "^8.0.1"
python = "^3.8"

[tool.poetry.dev-dependencies]
black = "^21.5b2"
black = "^21.6b0"
coverage = {extras = ["toml"], version = "^5.4"}
darglint = "^1.7.0"
flake8 = "^3.8.4"
darglint = "^1.8.0"
flake8 = "^3.9.2"
flake8-bandit = "^2.1.2"
flake8-bugbear = "^21.4.3"
flake8-docstrings = "^1.6.0"
flake8-rst-docstrings = "^0.2.3"
mypy = "^0.812"
mypy = "^0.902"
pre-commit = "^2.12.1"
pre-commit-hooks = "^4.0.1"
pytest = "^6.2.4"
reorder-python-imports = "^2.5.0"
safety = "^1.10.3"
sphinx = "^4.0.2"
sphinx-autobuild = "^2021.3.14"
sphinx-click = "^3.0.0"
sphinx-click = "^3.0.1"
sphinx-rtd-theme = "^0.5.2"

[tool.poetry.scripts]
Expand All @@ -47,12 +47,19 @@ source = ["aalto_asr_preprocessor"]
fail_under = 100
show_missing = true

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0"]
[tool.mypy]
strict = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true

[tool.pytest.ini_options]
addopts = "-ra -q"
testpaths = [
"tests",
]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
33 changes: 16 additions & 17 deletions src/aalto_asr_preprocessor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
The client expects three arguments: input, output, and a recipe file.
Run ``aalto-prep --help`` to see more.
"""
from importlib.abc import Loader
from importlib.util import module_from_spec
from importlib.util import spec_from_file_location
import importlib
from typing import Any

import click
Expand All @@ -29,20 +27,21 @@ def main(input: Any, output: Any, recipefile: Any, add_linefeed: Any) -> None:
if add_linefeed:
line_separator = "\n"

spec = spec_from_file_location(recipefile, recipefile)
assert isinstance(spec.loader, Loader) # for mypy
recipe = module_from_spec(spec)
spec.loader.exec_module(recipe)

texts = input.readlines()
for text in texts:
try:
result = prep.apply(
text, recipe.REGEXPS, recipe.UNACCEPTED_CHARS, recipe.TRANSLATIONS # type: ignore
)
except AttributeError:
result = prep.apply(text, recipe.REGEXPS, recipe.UNACCEPTED_CHARS) # type: ignore
output.write(result + line_separator)
if spec := importlib.util.spec_from_file_location("recipe", recipefile):
recipe = importlib.util.module_from_spec(spec)
spec.loader.exec_module(recipe) # type: ignore

texts = input.readlines()
for text in texts:
try:
result = prep.apply(
text, recipe.REGEXPS, recipe.UNACCEPTED_CHARS, recipe.TRANSLATIONS # type: ignore
)
except AttributeError:
result = prep.apply(text, recipe.REGEXPS, recipe.UNACCEPTED_CHARS) # type: ignore
output.write(result + line_separator)
else:
raise click.ClickException(f"Failed to import recipe '{recipefile}', is it a python file?")


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_fail_without_arguments(runner: CliRunner) -> None:
assert result.exit_code == 2


def test_fail_with_bad_recipe_file(runner: CliRunner) -> None:
"""It exits if recipe file is not a python file."""
result = runner.invoke(
__main__.main, ["tests/console_test_input.txt", "-", "tests/console_test_input.txt"]
)
assert result.exit_code == 1


def test_help_option(runner: CliRunner) -> None:
"""It exits with status code 0 if help option is passed."""
result = runner.invoke(__main__.main, ["--help"])
Expand Down