From da8a5bb1895e5434695d9dc2d844119cd8f88524 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Thu, 21 Oct 2021 19:38:39 -0700 Subject: [PATCH 1/1] Disallow any Generics on mypy except in black_primer (#2556) Only black_primer needs the disallowal - means we'll get better typing everywhere else. --- mypy.ini | 8 ++++++-- src/black/__init__.py | 10 +++++----- src/black_primer/lib.py | 2 +- tests/test_black.py | 4 +++- tests/test_primer.py | 6 ++++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/mypy.ini b/mypy.ini index 7e563e6..dabef45 100644 --- a/mypy.ini +++ b/mypy.ini @@ -22,8 +22,7 @@ strict_optional=True warn_no_return=True warn_redundant_casts=True warn_unused_ignores=True -# Until we're not supporting 3.6 primer needs this -disallow_any_generics=False +disallow_any_generics=True # The following are off by default. Flip them on if you feel # adventurous. @@ -35,6 +34,11 @@ cache_dir=/dev/null [mypy-aiohttp.*] follow_imports=skip + +[mypy-black_primer.*] +# Until we're not supporting 3.6 primer needs this +disallow_any_generics=False + [mypy-black] # The following is because of `patch_click()`. Remove when # we drop Python 3.6 support. diff --git a/src/black/__init__.py b/src/black/__init__.py index fdbaf04..f01e449 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -170,7 +170,7 @@ def validate_regex( ctx: click.Context, param: click.Parameter, value: Optional[str], -) -> Optional[Pattern]: +) -> Optional[Pattern[str]]: try: return re_compile_maybe_verbose(value) if value is not None else None except re.error: @@ -388,10 +388,10 @@ def main( quiet: bool, verbose: bool, required_version: str, - include: Pattern, - exclude: Optional[Pattern], - extend_exclude: Optional[Pattern], - force_exclude: Optional[Pattern], + include: Pattern[str], + exclude: Optional[Pattern[str]], + extend_exclude: Optional[Pattern[str]], + force_exclude: Optional[Pattern[str]], stdin_filename: Optional[str], workers: int, src: Tuple[str, ...], diff --git a/src/black_primer/lib.py b/src/black_primer/lib.py index 7494ae6..c784279 100644 --- a/src/black_primer/lib.py +++ b/src/black_primer/lib.py @@ -258,7 +258,7 @@ async def git_checkout_or_rebase( def handle_PermissionError( - func: Callable, path: Path, exc: Tuple[Any, Any, Any] + func: Callable[..., None], path: Path, exc: Tuple[Any, Any, Any] ) -> None: """ Handle PermissionError during shutil.rmtree. diff --git a/tests/test_black.py b/tests/test_black.py index beb56cf..1fc63c9 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2018,7 +2018,9 @@ with open(black.__file__, "r", encoding="utf-8") as _bf: black_source_lines = _bf.readlines() -def tracefunc(frame: types.FrameType, event: str, arg: Any) -> Callable: +def tracefunc( + frame: types.FrameType, event: str, arg: Any +) -> Callable[[types.FrameType, str, Any], Any]: """Show function calls `from black/__init__.py` as they happen. Register this with `sys.settrace()` in a test you're debugging. diff --git a/tests/test_primer.py b/tests/test_primer.py index e7f99fd..dc30a7a 100644 --- a/tests/test_primer.py +++ b/tests/test_primer.py @@ -11,7 +11,7 @@ from pathlib import Path from platform import system from subprocess import CalledProcessError from tempfile import TemporaryDirectory, gettempdir -from typing import Any, Callable, Generator, Iterator, Tuple +from typing import Any, Callable, Iterator, Tuple from unittest.mock import Mock, patch from click.testing import CliRunner @@ -44,7 +44,9 @@ FAKE_PROJECT_CONFIG = { @contextmanager -def capture_stdout(command: Callable, *args: Any, **kwargs: Any) -> Generator: +def capture_stdout( + command: Callable[..., Any], *args: Any, **kwargs: Any +) -> Iterator[str]: old_stdout, sys.stdout = sys.stdout, StringIO() try: command(*args, **kwargs) -- 2.39.5