From 05d39d9120236752cebf1b8576a5f0ac4218a8fa Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Tue, 24 Mar 2020 19:46:49 +0000 Subject: [PATCH] some mypy cleanup --- my/common.py | 19 +++++++++++-------- my/pdfs.py | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/my/common.py b/my/common.py index 3d42167..fc72142 100644 --- a/my/common.py +++ b/my/common.py @@ -1,20 +1,25 @@ from pathlib import Path import functools -from typing import Union, Callable, Dict, List, Iterable, TypeVar, Sequence, List +import types +from typing import Union, Callable, Dict, Iterable, TypeVar, Sequence, List, Optional, TYPE_CHECKING, Any # some helper functions +PathIsh = Union[Path, str] -def import_file(p: Union[str, Path], name=None): +# TODO port annotations to kython?.. +def import_file(p: PathIsh, name: Optional[str]=None) -> types.ModuleType: p = Path(p) if name is None: name = p.stem import importlib.util - spec = importlib.util.spec_from_file_location(name, p) # type: ignore + spec = importlib.util.spec_from_file_location(name, p) foo = importlib.util.module_from_spec(spec) - spec.loader.exec_module(foo) # type: ignore + loader = spec.loader; assert loader is not None + loader.exec_module(foo) # type: ignore[attr-defined] return foo -def import_from(path, name): + +def import_from(path: PathIsh, name: str) -> types.ModuleType: path = str(path) import sys try: @@ -81,11 +86,10 @@ def listify(fn=None, wrapper=list): from .kython.klogging import setup_logger, LazyLogger -PathIsh = Union[Path, str] Paths = Union[Sequence[PathIsh], PathIsh] -def get_files(pp: Paths, glob: str, sort=True) -> List[Path]: +def get_files(pp: Paths, glob: str, sort: bool=True) -> List[Path]: """ Helper function to avoid boilerplate. """ @@ -128,7 +132,6 @@ def mcachew(*args, **kwargs): return cachew.cachew(*args, **kwargs) - @functools.lru_cache(1) def _magic(): import magic # type: ignore diff --git a/my/pdfs.py b/my/pdfs.py index 67d7aa0..567304f 100755 --- a/my/pdfs.py +++ b/my/pdfs.py @@ -10,7 +10,7 @@ from typing import NamedTuple, List, Optional, Iterator from contextlib import redirect_stderr -from .common import import_file, mcachew, group_by_key +from .common import mcachew, group_by_key from .error import Res, split_errors # path to pdfannots (https://github.com/0xabu/pdfannots)