Skip to content

Commit

Permalink
Enforce mypy strict mode
Browse files Browse the repository at this point in the history
ghstack-source-id: 62f4095af5f2a40fd63030a34753a26443d69488
Pull Request resolved: #200
  • Loading branch information
amyreese committed Feb 28, 2024
1 parent 2fd000b commit 29074cc
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 87 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ skip_covered = true

[tool.mypy]
python_version = "3.8"
# strict = true
strict = true
ignore_missing_imports = true

[tool.thx]
Expand Down
2 changes: 2 additions & 0 deletions ufmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
)

__all__ = [
"__author__",
"__version__",
"BlackConfig",
"BlackConfigFactory",
"Encoding",
Expand Down
12 changes: 6 additions & 6 deletions ufmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from moreorless.click import echo_color_precomputed_diff

from .__version__ import __version__
from .core import Result, ufmt_paths
from .types import Options
from .core import ufmt_paths
from .types import Options, Result
from .util import enable_libcst_native


Expand Down Expand Up @@ -113,7 +113,7 @@ def main(
debug: Optional[bool],
concurrency: Optional[int],
root: Optional[Path],
):
) -> None:
init_logging(debug=debug)

ctx.obj = Options(
Expand All @@ -130,7 +130,7 @@ def main(
@click.argument(
"names", type=click.Path(allow_dash=True), nargs=-1, metavar="[PATH] ..."
)
def check(ctx: click.Context, names: List[str]):
def check(ctx: click.Context, names: List[str]) -> None:
"""Check formatting of one or more paths"""
options: Options = ctx.obj
paths = [Path(name) for name in names] if names else [Path(".")]
Expand All @@ -147,7 +147,7 @@ def check(ctx: click.Context, names: List[str]):
@click.argument(
"names", type=click.Path(allow_dash=True), nargs=-1, metavar="[PATH] ..."
)
def diff(ctx: click.Context, names: List[str]):
def diff(ctx: click.Context, names: List[str]) -> None:
"""Generate diffs for any files that need formatting"""
options: Options = ctx.obj
paths = [Path(name) for name in names] if names else [Path(".")]
Expand All @@ -168,7 +168,7 @@ def diff(ctx: click.Context, names: List[str]):
@click.argument(
"names", type=click.Path(allow_dash=True), nargs=-1, metavar="[PATH] ..."
)
def format(ctx: click.Context, names: List[str]):
def format(ctx: click.Context, names: List[str]) -> None:
"""Format one or more paths in place"""
options: Options = ctx.obj
paths = [Path(name) for name in names] if names else [Path(".")]
Expand Down
7 changes: 4 additions & 3 deletions ufmt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

import black
import black.mode
from moreorless.click import unified_diff
import black.report
from moreorless import unified_diff
from trailrunner import Trailrunner
from usort import usort

Expand Down Expand Up @@ -123,7 +124,7 @@ def ufmt_bytes(
fast=False,
mode=black_config,
)
except black.NothingChanged:
except black.report.NothingChanged:
content = result.output
elif ufmt_config.formatter == Formatter.ruff_api:
options = ruff_api.FormatOptions(
Expand Down Expand Up @@ -346,7 +347,7 @@ def ufmt_stdin(
if result.diff and path != STDIN:
real_path_str = path.as_posix()

def replacement(match: Match) -> str:
def replacement(match: Match[str]) -> str:
return match.group(1) + real_path_str

pattern = re.compile(r"^((?:---|\+\+\+)\s+).+$", re.M)
Expand Down
20 changes: 10 additions & 10 deletions ufmt/tests/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import skipIf, TestCase
from unittest.mock import call, patch
from unittest.mock import call, Mock, patch

import trailrunner
from click.testing import CliRunner
from libcst import ParserSyntaxError

from ufmt.cli import echo_results, main
from ufmt.core import Result
from ufmt.types import Result

from .core import CORRECTLY_FORMATTED_CODE, POORLY_FORMATTED_CODE


@patch.object(trailrunner.core.Trailrunner, "DEFAULT_EXECUTOR", ThreadPoolExecutor)
class CliTest(TestCase):
def setUp(self):
def setUp(self) -> None:
self.runner = CliRunner(mix_stderr=False)
self.cwd = os.getcwd()
self.td = TemporaryDirectory()
self.tdp = Path(self.td.name).resolve()
os.chdir(self.tdp)

def tearDown(self):
def tearDown(self) -> None:
os.chdir(self.cwd)
self.td.cleanup()

@patch("ufmt.cli.echo_color_precomputed_diff")
@patch("ufmt.cli.click.secho")
def test_echo(self, echo_mock, mol_mock):
def test_echo(self, echo_mock: Mock, mol_mock: Mock) -> None:
f1 = Path("foo/bar.py")
f2 = Path("fuzz/buzz.py")
f3 = Path("make/rake.py")
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_echo(self, echo_mock, mol_mock):
mol_mock.reset_mock()

@patch("ufmt.cli.ufmt_paths")
def test_check(self, ufmt_mock):
def test_check(self, ufmt_mock: Mock) -> None:
with self.subTest("no paths given"):
ufmt_mock.reset_mock()
ufmt_mock.return_value = []
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_check(self, ufmt_mock):
self.assertEqual(0, result.exit_code)

@patch("ufmt.cli.ufmt_paths")
def test_diff(self, ufmt_mock):
def test_diff(self, ufmt_mock: Mock) -> None:
with self.subTest("no paths given"):
ufmt_mock.reset_mock()
ufmt_mock.return_value = []
Expand Down Expand Up @@ -289,7 +289,7 @@ def test_diff(self, ufmt_mock):
self.assertEqual(2, result.exit_code)

@patch("ufmt.cli.ufmt_paths")
def test_format(self, ufmt_mock):
def test_format(self, ufmt_mock: Mock) -> None:
with self.subTest("no paths given"):
ufmt_mock.reset_mock()
ufmt_mock.return_value = []
Expand Down Expand Up @@ -418,14 +418,14 @@ def test_stdin(self) -> None:
self.assertIn("Formatted hello.py\n", result.stderr)
self.assertEqual(0, result.exit_code)

def test_end_to_end(self):
def test_end_to_end(self) -> None:
alpha = self.tdp / "alpha.py"
beta = self.tdp / "beta.py"
(self.tdp / "sub").mkdir()
gamma = self.tdp / "sub" / "gamma.py"
kappa = self.tdp / "sub" / "kappa.py"

def reset():
def reset() -> None:
alpha.write_text(CORRECTLY_FORMATTED_CODE)
beta.write_text(POORLY_FORMATTED_CODE)
gamma.write_text(CORRECTLY_FORMATTED_CODE)
Expand Down
17 changes: 10 additions & 7 deletions ufmt/tests/config.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
# Copyright 2022 Amethyst Reese
# Licensed under the MIT license

from contextlib import AbstractContextManager
from pathlib import Path
from tempfile import TemporaryDirectory
from textwrap import dedent
from typing import Any
from unittest import TestCase
from unittest.mock import ANY, patch
from unittest.mock import ANY, Mock, patch

from trailrunner.tests.core import cd

from ufmt.config import load_config, UfmtConfig
from ufmt.config import load_config
from ufmt.types import UfmtConfig


class ConfigTest(TestCase):
maxDiff = None

def setUp(self):
def setUp(self) -> None:
self._td = TemporaryDirectory()
self.addCleanup(self._td.cleanup)

self.td = Path(self._td.name).resolve()
self.pyproject = self.td / "pyproject.toml"

def subTest(self, *args, **kwargs):
def subTest(self, *args: Any, **kwargs: Any) -> AbstractContextManager[None]:
load_config.cache_clear()
return super().subTest(*args, **kwargs)

def test_ufmt_config(self):
def test_ufmt_config(self) -> None:
fake_config = dedent(
"""
[tool.ufmt]
Expand Down Expand Up @@ -111,7 +114,7 @@ def test_ufmt_config(self):
)

@patch("ufmt.config.LOG")
def test_invalid_config(self, log_mock):
def test_invalid_config(self, log_mock: Mock) -> None:
with self.subTest("string"):
self.pyproject.write_text(
dedent(
Expand Down Expand Up @@ -190,7 +193,7 @@ def test_invalid_config(self, log_mock):
load_config(self.td / "fake.py")

@patch("ufmt.config.LOG")
def test_config_excludes(self, log_mock):
def test_config_excludes(self, log_mock: Mock) -> None:
with self.subTest("missing"):
self.pyproject.write_text(
dedent(
Expand Down
Loading

0 comments on commit 29074cc

Please sign in to comment.