some mypy cleanup
This commit is contained in:
parent
bf667ef58b
commit
05d39d9120
2 changed files with 12 additions and 9 deletions
19
my/common.py
19
my/common.py
|
@ -1,20 +1,25 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import functools
|
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
|
# 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)
|
p = Path(p)
|
||||||
if name is None:
|
if name is None:
|
||||||
name = p.stem
|
name = p.stem
|
||||||
import importlib.util
|
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)
|
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
|
return foo
|
||||||
|
|
||||||
def import_from(path, name):
|
|
||||||
|
def import_from(path: PathIsh, name: str) -> types.ModuleType:
|
||||||
path = str(path)
|
path = str(path)
|
||||||
import sys
|
import sys
|
||||||
try:
|
try:
|
||||||
|
@ -81,11 +86,10 @@ def listify(fn=None, wrapper=list):
|
||||||
|
|
||||||
from .kython.klogging import setup_logger, LazyLogger
|
from .kython.klogging import setup_logger, LazyLogger
|
||||||
|
|
||||||
PathIsh = Union[Path, str]
|
|
||||||
|
|
||||||
Paths = Union[Sequence[PathIsh], PathIsh]
|
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.
|
Helper function to avoid boilerplate.
|
||||||
"""
|
"""
|
||||||
|
@ -128,7 +132,6 @@ def mcachew(*args, **kwargs):
|
||||||
return cachew.cachew(*args, **kwargs)
|
return cachew.cachew(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache(1)
|
@functools.lru_cache(1)
|
||||||
def _magic():
|
def _magic():
|
||||||
import magic # type: ignore
|
import magic # type: ignore
|
||||||
|
|
|
@ -10,7 +10,7 @@ from typing import NamedTuple, List, Optional, Iterator
|
||||||
from contextlib import redirect_stderr
|
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
|
from .error import Res, split_errors
|
||||||
|
|
||||||
# path to pdfannots (https://github.com/0xabu/pdfannots)
|
# path to pdfannots (https://github.com/0xabu/pdfannots)
|
||||||
|
|
Loading…
Add table
Reference in a new issue