From 071a313bdd27f0d368b470bb5fc6aa291264afda Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Tue, 27 Jun 2023 10:15:54 +0200 Subject: [PATCH 1/7] add type hints --- .gitignore | 1 + scooby/__main__.py | 5 +-- scooby/knowledge.py | 21 ++++++------- scooby/report.py | 74 ++++++++++++++++++++++++--------------------- scooby/tracker.py | 9 ++++-- 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index 8072ba6..d48e6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ coverage.xml *,cover .tox +venv \ No newline at end of file diff --git a/scooby/__main__.py b/scooby/__main__.py index c8e3b7b..077fc59 100644 --- a/scooby/__main__.py +++ b/scooby/__main__.py @@ -2,12 +2,13 @@ import argparse import importlib import sys +from typing import Any, Dict, Optional import scooby from scooby.report import Report -def main(args=None): +def main(args: Optional[list[str]] = None): """Parse command line inputs of CLI interface.""" # If not explicitly called, catch arguments if args is None: @@ -51,7 +52,7 @@ def main(args=None): act(vars(parser.parse_args(args))) -def act(args_dict): +def act(args_dict: Dict[str, Any]) -> None: """Act upon CLI inputs.""" # Quick exit if only scooby version. if args_dict.pop('version'): diff --git a/scooby/knowledge.py b/scooby/knowledge.py index 84228ab..760add0 100644 --- a/scooby/knowledge.py +++ b/scooby/knowledge.py @@ -11,6 +11,7 @@ import os import sys import sysconfig +from typing import Callable, Dict, Literal, Tuple # Define unusual version locations VERSION_ATTRIBUTES = { @@ -21,14 +22,14 @@ } -def get_pyqt5_version(): +def get_pyqt5_version() -> str: """Return the PyQt5 version.""" from PyQt5.Qt import PYQT_VERSION_STR return PYQT_VERSION_STR -VERSION_METHODS = { +VERSION_METHODS: Dict[str, Callable[[], str]] = { 'PyQt5': get_pyqt5_version, } @@ -81,13 +82,9 @@ def get_standard_lib_modules(): if os.path.isdir(lib_path): names = os.listdir(lib_path) else: - names = [] + names: list[str] = [] - stdlib_pkgs = [] - for name in names: - if name.endswith(".py"): - stdlib_pkgs.append(name[:-3]) - stdlib_pkgs = set(stdlib_pkgs) + stdlib_pkgs = {name[:-3] for name in names if name.endswith(".py")} else: names = os.listdir(site_path) @@ -116,7 +113,7 @@ def get_standard_lib_modules(): return stdlib_pkgs -def version_tuple(v): +def version_tuple(v: str) -> Tuple[int, ...]: """Convert a version string to a tuple containing ints. Non-numeric version strings will be converted to 0. For example: @@ -135,7 +132,7 @@ def version_tuple(v): if len(split_v) > 3: raise ValueError('Version strings containing more than three parts ' 'cannot be parsed') - vals = [] + vals: list[int] = [] for item in split_v: if item.isnumeric(): vals.append(int(item)) @@ -145,7 +142,7 @@ def version_tuple(v): return tuple(vals) -def meets_version(version, meets): +def meets_version(version: str, meets: str) -> bool: """Check if a version string meets a minimum version. This is a simplified way to compare version strings. For a more robust @@ -190,7 +187,7 @@ def meets_version(version, meets): return True -def get_filesystem_type(): +def get_filesystem_type() -> str | Literal[False]: """Get the type of the file system at the path of the scooby package.""" try: import psutil # lazy-load see PR#85 diff --git a/scooby/report.py b/scooby/report.py index 7bda5dd..d9526b8 100644 --- a/scooby/report.py +++ b/scooby/report.py @@ -4,6 +4,7 @@ import sys import time from types import ModuleType +from typing import Any, Dict, Literal, Optional, Tuple, cast from .knowledge import ( VERSION_ATTRIBUTES, @@ -22,8 +23,11 @@ class PlatformInfo: """Internal helper class to access details about the computer platform.""" + def __init__(self): + self._mkl_info: Optional[str] # for typing purpose + @property - def system(self): + def system(self) -> str: """Return the system/OS name. E.g. ``'Linux'``, ``'Windows'``, or ``'Java'``. An empty string is @@ -32,12 +36,12 @@ def system(self): return platform().system() @property - def platform(self): + def platform(self) -> str: """Return the platform.""" return platform().platform() @property - def machine(self): + def machine(self) -> str: """Return the machine type, e.g. 'i386'. An empty string is returned if the value cannot be determined. @@ -45,12 +49,12 @@ def machine(self): return platform().machine() @property - def architecture(self): + def architecture(self) -> str: """Return the bit architecture used for the executable.""" return platform().architecture()[0] @property - def cpu_count(self): + def cpu_count(self) -> int: """Return the number of CPUs in the system.""" if not hasattr(self, '_cpu_count'): import multiprocessing # lazy-load see PR#85 @@ -59,7 +63,7 @@ def cpu_count(self): return self._cpu_count @property - def total_ram(self): + def total_ram(self) -> str: """Return total RAM info. If not available, returns 'unknown'. @@ -77,7 +81,7 @@ def total_ram(self): return self._total_ram @property - def mkl_info(self): + def mkl_info(self) -> Optional[str]: """Return MKL info. If not available, returns 'unknown'. @@ -98,16 +102,16 @@ def mkl_info(self): # Get mkl info from numexpr or mkl, if available if mkl: - self._mkl_info = mkl.get_version_string() + self._mkl_info = cast(str, mkl.get_version_string()) elif numexpr: - self._mkl_info = numexpr.get_vml_version() + self._mkl_info = cast(str, numexpr.get_vml_version()) else: self._mkl_info = None return self._mkl_info @property - def date(self): + def date(self) -> str: """Return the date formatted as a string.""" return time.strftime('%a %b %d %H:%M:%S %Y %Z') @@ -122,9 +126,9 @@ def filesystem(self): class PythonInfo: """Internal helper class to access Python info and package versions.""" - def __init__(self, additional, core, optional, sort): + def __init__(self, additional: Optional[list[str | ModuleType]], core: Optional[list[str | ModuleType]], optional: Optional[list[str | ModuleType]], sort: bool): """Initialize python info.""" - self._packages = {} # Holds name of packages and their version + self._packages: Dict[str, Any] = {} # Holds name of packages and their version self._sort = sort # Add packages in the following order: @@ -132,7 +136,7 @@ def __init__(self, additional, core, optional, sort): self._add_packages(core) # Provided by a module dev self._add_packages(optional, optional=True) # Optional packages - def _add_packages(self, packages, optional=False): + def _add_packages(self, packages: Optional[list[str | ModuleType]], optional: bool = False): """Add all packages to list; optional ones only if available.""" # Ensure arguments are a list if isinstance(packages, (str, ModuleType)): @@ -140,7 +144,7 @@ def _add_packages(self, packages, optional=False): packages, ] elif packages is None or len(packages) < 1: - pckgs = list() + pckgs: list[str | ModuleType] = list() else: pckgs = list(packages) @@ -151,12 +155,12 @@ def _add_packages(self, packages, optional=False): self._packages[name] = version @property - def sys_version(self): + def sys_version(self) -> str: """Return the system version.""" return sys.version @property - def python_environment(self): + def python_environment(self) -> Literal['Jupyter', 'IPython', 'Python']: """Return the python environment.""" if in_ipykernel(): return 'Jupyter' @@ -165,7 +169,7 @@ def python_environment(self): return 'Python' @property - def packages(self): + def packages(self) -> dict[str, Any]: """Return versions of all packages. Includes available and unavailable/unknown. @@ -173,7 +177,7 @@ def packages(self): """ pckg_dict = dict(self._packages) if self._sort: - packages = {} + packages: Dict[str, Any] = {} for name in sorted(pckg_dict.keys(), key=lambda x: x.lower()): packages[name] = pckg_dict[name] pckg_dict = packages @@ -220,14 +224,14 @@ class Report(PlatformInfo, PythonInfo): def __init__( self, - additional=None, - core=None, - optional=None, - ncol=4, - text_width=80, - sort=False, - extra_meta=None, - max_width=None, + additional: Optional[list[str | ModuleType]] = None, + core: Optional[list[str | ModuleType]] = None, + optional: Optional[list[str | ModuleType]] = None, + ncol: int = 4, + text_width: int = 80, + sort: bool = False, + extra_meta: Optional[Tuple[str, str]] = None, + max_width: Optional[int] = None, ): """Initialize report.""" # Set default optional packages to investigate @@ -251,7 +255,7 @@ def __init__( extra_meta = [] self._extra_meta = extra_meta - def __repr__(self): + def __repr__(self) -> str: """Return Plain-text version information.""" import textwrap # lazy-load see PR#85 @@ -303,12 +307,12 @@ def __repr__(self): return text - def _repr_html_(self): + def _repr_html_(self) -> str: """Return HTML-rendered version information.""" # Define html-styles border = "border: 1px solid;'" - def colspan(html, txt, ncol, nrow): + def colspan(html: str, txt: str, ncol: int, nrow: int) -> str: r"""Print txt in a row spanning whole table.""" html += " \n" html += " Tuple[str, Optional[str]]: """Get the version of ``module`` by passing the package or it's name. Parameters @@ -506,7 +510,7 @@ def get_version(module): return name, VERSION_NOT_FOUND -def platform(): +def platform() -> ModuleType: """Return platform as lazy load; see PR#85.""" import platform diff --git a/scooby/tracker.py b/scooby/tracker.py index 327968d..afa294b 100644 --- a/scooby/tracker.py +++ b/scooby/tracker.py @@ -1,4 +1,7 @@ """Track imports.""" +from types import ModuleType +from typing import TYPE_CHECKING, Mapping, Optional, Sequence + from scooby.knowledge import get_standard_lib_modules from scooby.report import Report @@ -41,7 +44,7 @@ def _criterion(name): if TRACKING_SUPPORTED: - def scooby_import(name, globals=None, locals=None, fromlist=(), level=0): + def scooby_import(name: str, globals: Optional[Mapping[str, object]] = None, locals: Optional[Mapping[str, object]] = None, fromlist: Sequence[str] = (), level: int = 0) -> ModuleType: """Override of the import method to track package names.""" m = CLASSIC_IMPORT(name, globals=globals, locals=locals, fromlist=fromlist, level=level) name = name.split(".")[0] @@ -50,7 +53,7 @@ def scooby_import(name, globals=None, locals=None, fromlist=(), level=0): return m -def track_imports(): +def track_imports() -> None: """Track all imported modules for the remainder of this session.""" if not TRACKING_SUPPORTED: raise RuntimeError(SUPPORT_MESSAGE) @@ -60,7 +63,7 @@ def track_imports(): return -def untrack_imports(): +def untrack_imports() -> None: """Stop tracking imports and return to the builtin import method. This will also clear the tracked imports. From a018991305169801d4abaa8fad5714445642d7d2 Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Tue, 27 Jun 2023 10:32:13 +0200 Subject: [PATCH 2/7] finish with types and add py.typed file --- scooby/knowledge.py | 8 ++++---- scooby/py.typed | 0 scooby/report.py | 7 ++++--- scooby/tracker.py | 8 ++++---- setup.py | 1 + 5 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 scooby/py.typed diff --git a/scooby/knowledge.py b/scooby/knowledge.py index 760add0..ec63981 100644 --- a/scooby/knowledge.py +++ b/scooby/knowledge.py @@ -35,7 +35,7 @@ def get_pyqt5_version() -> str: # Check the environments -def in_ipython(): +def in_ipython() -> bool: """Check if we are in a IPython environment. Returns @@ -50,7 +50,7 @@ def in_ipython(): return False -def in_ipykernel(): +def in_ipykernel() -> bool: """Check if in a ipykernel (most likely Jupyter) environment. Warning @@ -68,13 +68,13 @@ def in_ipykernel(): ipykernel = False if in_ipython(): try: - ipykernel = type(get_ipython()).__module__.startswith('ipykernel.') + ipykernel: bool = type(get_ipython()).__module__.startswith('ipykernel.') except NameError: pass return ipykernel -def get_standard_lib_modules(): +def get_standard_lib_modules() -> set[str]: """Return a set of the names of all modules in the standard library.""" site_path = sysconfig.get_path('stdlib') if getattr(sys, 'frozen', False): # within pyinstaller diff --git a/scooby/py.typed b/scooby/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/scooby/report.py b/scooby/report.py index d9526b8..8acc014 100644 --- a/scooby/report.py +++ b/scooby/report.py @@ -25,6 +25,7 @@ class PlatformInfo: def __init__(self): self._mkl_info: Optional[str] # for typing purpose + self._filesystem: str | Literal[False] @property def system(self) -> str: @@ -116,7 +117,7 @@ def date(self) -> str: return time.strftime('%a %b %d %H:%M:%S %Y %Z') @property - def filesystem(self): + def filesystem(self) -> str | Literal[False]: """Get the type of the file system at the path of the scooby package.""" if not hasattr(self, '_filesystem'): self._filesystem = get_filesystem_type() @@ -232,7 +233,7 @@ def __init__( sort: bool = False, extra_meta: Optional[Tuple[str, str]] = None, max_width: Optional[int] = None, - ): + ) -> None: """Initialize report.""" # Set default optional packages to investigate if optional is None: @@ -423,7 +424,7 @@ def to_dict(self) -> dict[str, str]: return out -def pkg_resources_version_fallback(name: str): +def pkg_resources_version_fallback(name: str) -> Optional[str]: """Use package-resources to get the distribution version.""" try: from pkg_resources import DistributionNotFound, get_distribution diff --git a/scooby/tracker.py b/scooby/tracker.py index afa294b..3443809 100644 --- a/scooby/tracker.py +++ b/scooby/tracker.py @@ -1,6 +1,6 @@ """Track imports.""" from types import ModuleType -from typing import TYPE_CHECKING, Mapping, Optional, Sequence +from typing import Mapping, Optional, Sequence, cast from scooby.knowledge import get_standard_lib_modules from scooby.report import Report @@ -18,7 +18,7 @@ pass # The variable we track all imports in -TRACKED_IMPORTS = ["scooby"] +TRACKED_IMPORTS: list[str | ModuleType] = ["scooby"] MODULES_TO_IGNORE = { "pyMKL", @@ -31,7 +31,7 @@ STDLIB_PKGS = None -def _criterion(name): +def _criterion(name: str): if ( len(name) > 0 and name not in STDLIB_PKGS @@ -83,7 +83,7 @@ class TrackedReport(Report): ``globals()`` dictionary. """ - def __init__(self, additional=None, ncol=3, text_width=80, sort=False): + def __init__(self, additional: Optional[list[str | ModuleType]] = None, ncol: int = 3, text_width: int = 80, sort: bool = False): """Initialize.""" if not TRACKING_SUPPORTED: raise RuntimeError(SUPPORT_MESSAGE) diff --git a/setup.py b/setup.py index 42c803d..727bb84 100644 --- a/setup.py +++ b/setup.py @@ -38,4 +38,5 @@ "write_to": os.path.join("scooby", "version.py"), }, setup_requires=["setuptools_scm"], + package_data={"scooby": ["py.typed"]}, ) From a10e363c2c3e9c9ff2bebeba38a6794b23b4d889 Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Mon, 17 Jul 2023 20:37:09 +0200 Subject: [PATCH 3/7] replace pipe by union and list by typing.List --- scooby/knowledge.py | 7 ++++--- scooby/report.py | 28 ++++++++++++++-------------- scooby/tracker.py | 8 ++++---- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/scooby/knowledge.py b/scooby/knowledge.py index ec63981..08e9257 100644 --- a/scooby/knowledge.py +++ b/scooby/knowledge.py @@ -11,7 +11,7 @@ import os import sys import sysconfig -from typing import Callable, Dict, Literal, Tuple +from typing import Callable, Dict, Literal, Tuple, Union, List # Define unusual version locations VERSION_ATTRIBUTES = { @@ -82,7 +82,7 @@ def get_standard_lib_modules() -> set[str]: if os.path.isdir(lib_path): names = os.listdir(lib_path) else: - names: list[str] = [] + names = [] stdlib_pkgs = {name[:-3] for name in names if name.endswith(".py")} @@ -132,7 +132,7 @@ def version_tuple(v: str) -> Tuple[int, ...]: if len(split_v) > 3: raise ValueError('Version strings containing more than three parts ' 'cannot be parsed') - vals: list[int] = [] + vals: List[int] = [] for item in split_v: if item.isnumeric(): vals.append(int(item)) @@ -197,6 +197,7 @@ def get_filesystem_type() -> str | Literal[False]: import platform # lazy-load see PR#85 # Skip Windows due to https://github.com/banesullivan/scooby/issues/75 + fs_type: Union[str, Literal[False]] if psutil and platform.system() != 'Windows': # Code by https://stackoverflow.com/a/35291824/10504481 my_path = str(Path(__file__).resolve()) diff --git a/scooby/report.py b/scooby/report.py index 8acc014..53e8078 100644 --- a/scooby/report.py +++ b/scooby/report.py @@ -4,7 +4,7 @@ import sys import time from types import ModuleType -from typing import Any, Dict, Literal, Optional, Tuple, cast +from typing import Any, Dict, Literal, Optional, Tuple, cast, Union, List from .knowledge import ( VERSION_ATTRIBUTES, @@ -25,7 +25,7 @@ class PlatformInfo: def __init__(self): self._mkl_info: Optional[str] # for typing purpose - self._filesystem: str | Literal[False] + self._filesystem: Union[str, Literal[False]] @property def system(self) -> str: @@ -117,7 +117,7 @@ def date(self) -> str: return time.strftime('%a %b %d %H:%M:%S %Y %Z') @property - def filesystem(self) -> str | Literal[False]: + def filesystem(self) -> Union[str, Literal[False]]: """Get the type of the file system at the path of the scooby package.""" if not hasattr(self, '_filesystem'): self._filesystem = get_filesystem_type() @@ -127,7 +127,7 @@ def filesystem(self) -> str | Literal[False]: class PythonInfo: """Internal helper class to access Python info and package versions.""" - def __init__(self, additional: Optional[list[str | ModuleType]], core: Optional[list[str | ModuleType]], optional: Optional[list[str | ModuleType]], sort: bool): + def __init__(self, additional: Optional[List[Union[str, ModuleType]]], core: Optional[List[Union[str, ModuleType]]], optional: Optional[List[Union[str, ModuleType]]], sort: bool): """Initialize python info.""" self._packages: Dict[str, Any] = {} # Holds name of packages and their version self._sort = sort @@ -137,15 +137,15 @@ def __init__(self, additional: Optional[list[str | ModuleType]], core: Optional[ self._add_packages(core) # Provided by a module dev self._add_packages(optional, optional=True) # Optional packages - def _add_packages(self, packages: Optional[list[str | ModuleType]], optional: bool = False): + def _add_packages(self, packages: Optional[List[Union[str, ModuleType]]], optional: bool = False): """Add all packages to list; optional ones only if available.""" # Ensure arguments are a list if isinstance(packages, (str, ModuleType)): - pckgs = [ + pckgs: List[Union[str, ModuleType]] = [ packages, ] elif packages is None or len(packages) < 1: - pckgs: list[str | ModuleType] = list() + pckgs = list() else: pckgs = list(packages) @@ -215,7 +215,7 @@ class Report(PlatformInfo, PythonInfo): sort : bool, optional Sort the packages when the report is shown. - extra_meta : tuple(str, str), optional + extra_meta : tuple(tuple(str, str), ...), optional Additional two component pairs of meta information to display. max_width : int, optional @@ -225,13 +225,13 @@ class Report(PlatformInfo, PythonInfo): def __init__( self, - additional: Optional[list[str | ModuleType]] = None, - core: Optional[list[str | ModuleType]] = None, - optional: Optional[list[str | ModuleType]] = None, + additional: Optional[List[Union[str, ModuleType]]] = None, + core: Optional[List[Union[str, ModuleType]]] = None, + optional: Optional[List[Union[str, ModuleType]]] = None, ncol: int = 4, text_width: int = 80, sort: bool = False, - extra_meta: Optional[Tuple[str, str]] = None, + extra_meta: Optional[Union[Tuple[Tuple[str, str], ...], List[Tuple[str, str]]]] = None, max_width: Optional[int] = None, ) -> None: """Initialize report.""" @@ -429,7 +429,7 @@ def pkg_resources_version_fallback(name: str) -> Optional[str]: try: from pkg_resources import DistributionNotFound, get_distribution except ImportError: - return + return None try: return get_distribution(name).version except (DistributionNotFound, Exception): # pragma: no cover @@ -438,7 +438,7 @@ def pkg_resources_version_fallback(name: str) -> Optional[str]: # This functionaliy might also be of interest on its own. -def get_version(module: str | ModuleType) -> Tuple[str, Optional[str]]: +def get_version(module: Union[str, ModuleType]) -> Tuple[str, Optional[str]]: """Get the version of ``module`` by passing the package or it's name. Parameters diff --git a/scooby/tracker.py b/scooby/tracker.py index 3443809..e681ef1 100644 --- a/scooby/tracker.py +++ b/scooby/tracker.py @@ -1,6 +1,6 @@ """Track imports.""" from types import ModuleType -from typing import Mapping, Optional, Sequence, cast +from typing import Mapping, Optional, Sequence, Union, Set, List from scooby.knowledge import get_standard_lib_modules from scooby.report import Report @@ -18,7 +18,7 @@ pass # The variable we track all imports in -TRACKED_IMPORTS: list[str | ModuleType] = ["scooby"] +TRACKED_IMPORTS: List[str | ModuleType] = ["scooby"] MODULES_TO_IGNORE = { "pyMKL", @@ -28,7 +28,7 @@ } -STDLIB_PKGS = None +STDLIB_PKGS: Optional[Set[str]] = None def _criterion(name: str): @@ -83,7 +83,7 @@ class TrackedReport(Report): ``globals()`` dictionary. """ - def __init__(self, additional: Optional[list[str | ModuleType]] = None, ncol: int = 3, text_width: int = 80, sort: bool = False): + def __init__(self, additional: Optional[List[Union[str, ModuleType]]] = None, ncol: int = 3, text_width: int = 80, sort: bool = False): """Initialize.""" if not TRACKING_SUPPORTED: raise RuntimeError(SUPPORT_MESSAGE) From 74bb909415b46b5458937adef4bbb91b6672c5fc Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Mon, 17 Jul 2023 20:38:11 +0200 Subject: [PATCH 4/7] missing Union --- scooby/knowledge.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scooby/knowledge.py b/scooby/knowledge.py index 08e9257..b18feee 100644 --- a/scooby/knowledge.py +++ b/scooby/knowledge.py @@ -187,7 +187,7 @@ def meets_version(version: str, meets: str) -> bool: return True -def get_filesystem_type() -> str | Literal[False]: +def get_filesystem_type() -> Union[str, Literal[False]]: """Get the type of the file system at the path of the scooby package.""" try: import psutil # lazy-load see PR#85 From c3dac81696e3a22a332050b44cebaf8222e9e602 Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Sun, 24 Sep 2023 22:23:54 +0200 Subject: [PATCH 5/7] use Set instead of set for typing --- scooby/knowledge.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scooby/knowledge.py b/scooby/knowledge.py index 336ac3b..caca6c6 100644 --- a/scooby/knowledge.py +++ b/scooby/knowledge.py @@ -11,7 +11,7 @@ import os import sys import sysconfig -from typing import Callable, Dict, Literal, Tuple, Union, List +from typing import Callable, Dict, List, Literal, Set, Tuple, Union # Define unusual version locations VERSION_ATTRIBUTES = { @@ -77,7 +77,7 @@ def in_ipykernel() -> bool: return ipykernel -def get_standard_lib_modules() -> set[str]: +def get_standard_lib_modules() -> Set[str]: """Return a set of the names of all modules in the standard library.""" site_path = sysconfig.get_path('stdlib') if getattr(sys, 'frozen', False): # within pyinstaller From 61558e06be65f495304b95bd006fe09350da72e2 Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Sun, 1 Oct 2023 18:14:11 +0200 Subject: [PATCH 6/7] fix typing and format issue --- scooby/report.py | 19 ++++++++++++++----- scooby/tracker.py | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/scooby/report.py b/scooby/report.py index f43eeed..ffdc1be 100644 --- a/scooby/report.py +++ b/scooby/report.py @@ -4,7 +4,7 @@ import sys import time from types import ModuleType -from typing import Any, Dict, Literal, Optional, Tuple, cast, Union, List +from typing import Any, Dict, List, Literal, Optional, Tuple, Union, cast from .knowledge import ( VERSION_ATTRIBUTES, @@ -24,6 +24,7 @@ class PlatformInfo: """Internal helper class to access details about the computer platform.""" def __init__(self): + """Initialize.""" self._mkl_info: Optional[str] # for typing purpose self._filesystem: Union[str, Literal[False]] @@ -126,7 +127,13 @@ def filesystem(self) -> Union[str, Literal[False]]: class PythonInfo: """Internal helper class to access Python info and package versions.""" - def __init__(self, additional: Optional[List[Union[str, ModuleType]]], core: Optional[List[Union[str, ModuleType]]], optional: Optional[List[Union[str, ModuleType]]], sort: bool): + def __init__( + self, + additional: Optional[List[Union[str, ModuleType]]], + core: Optional[List[Union[str, ModuleType]]], + optional: Optional[List[Union[str, ModuleType]]], + sort: bool, + ): """Initialize python info.""" self._packages: Dict[str, Any] = {} # Holds name of packages and their version self._sort = sort @@ -136,7 +143,9 @@ def __init__(self, additional: Optional[List[Union[str, ModuleType]]], core: Opt self._add_packages(core) # Provided by a module dev self._add_packages(optional, optional=True) # Optional packages - def _add_packages(self, packages: Optional[List[Union[str, ModuleType]]], optional: bool = False): + def _add_packages( + self, packages: Optional[List[Union[str, ModuleType]]], optional: bool = False + ): """Add all packages to list; optional ones only if available.""" # Ensure arguments are a list if isinstance(packages, (str, ModuleType)): @@ -169,7 +178,7 @@ def python_environment(self) -> Literal['Jupyter', 'IPython', 'Python']: return 'Python' @property - def packages(self) -> dict[str, Any]: + def packages(self) -> Dict[str, Any]: """Return versions of all packages. Includes available and unavailable/unknown. @@ -389,7 +398,7 @@ def cols(html: str, version: str, name: str, ncol: int, i: int) -> tuple[str, in return html - def to_dict(self) -> dict[str, str]: + def to_dict(self) -> Dict[str, str]: """Return report as dict for storage.""" out: Dict[str, str] = {} diff --git a/scooby/tracker.py b/scooby/tracker.py index e681ef1..635bc36 100644 --- a/scooby/tracker.py +++ b/scooby/tracker.py @@ -1,6 +1,6 @@ """Track imports.""" from types import ModuleType -from typing import Mapping, Optional, Sequence, Union, Set, List +from typing import List, Mapping, Optional, Sequence, Set, Union from scooby.knowledge import get_standard_lib_modules from scooby.report import Report @@ -18,7 +18,7 @@ pass # The variable we track all imports in -TRACKED_IMPORTS: List[str | ModuleType] = ["scooby"] +TRACKED_IMPORTS: List[Union[str, ModuleType]] = ["scooby"] MODULES_TO_IGNORE = { "pyMKL", @@ -44,7 +44,13 @@ def _criterion(name: str): if TRACKING_SUPPORTED: - def scooby_import(name: str, globals: Optional[Mapping[str, object]] = None, locals: Optional[Mapping[str, object]] = None, fromlist: Sequence[str] = (), level: int = 0) -> ModuleType: + def scooby_import( + name: str, + globals: Optional[Mapping[str, object]] = None, + locals: Optional[Mapping[str, object]] = None, + fromlist: Sequence[str] = (), + level: int = 0, + ) -> ModuleType: """Override of the import method to track package names.""" m = CLASSIC_IMPORT(name, globals=globals, locals=locals, fromlist=fromlist, level=level) name = name.split(".")[0] @@ -83,7 +89,13 @@ class TrackedReport(Report): ``globals()`` dictionary. """ - def __init__(self, additional: Optional[List[Union[str, ModuleType]]] = None, ncol: int = 3, text_width: int = 80, sort: bool = False): + def __init__( + self, + additional: Optional[List[Union[str, ModuleType]]] = None, + ncol: int = 3, + text_width: int = 80, + sort: bool = False, + ): """Initialize.""" if not TRACKING_SUPPORTED: raise RuntimeError(SUPPORT_MESSAGE) From d0f06661b263c23e42669d667521d4a53dc8d2c1 Mon Sep 17 00:00:00 2001 From: Gryfenfer97 Date: Sun, 1 Oct 2023 18:58:15 +0200 Subject: [PATCH 7/7] fix typing for python 3.8 for tuple and list --- scooby/__main__.py | 4 ++-- scooby/report.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scooby/__main__.py b/scooby/__main__.py index 746769d..ec56dea 100644 --- a/scooby/__main__.py +++ b/scooby/__main__.py @@ -2,13 +2,13 @@ import argparse import importlib import sys -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional import scooby from scooby.report import Report -def main(args: Optional[list[str]] = None): +def main(args: Optional[List[str]] = None): """Parse command line inputs of CLI interface.""" # If not explicitly called, catch arguments if args is None: diff --git a/scooby/report.py b/scooby/report.py index ffdc1be..09a2678 100644 --- a/scooby/report.py +++ b/scooby/report.py @@ -336,7 +336,7 @@ def colspan(html: str, txt: str, ncol: int, nrow: int) -> str: html += " \n" return html - def cols(html: str, version: str, name: str, ncol: int, i: int) -> tuple[str, int]: + def cols(html: str, version: str, name: str, ncol: int, i: int) -> Tuple[str, int]: r"""Print package information in two cells.""" # Check if we have to start a new row if i > 0 and i % ncol == 0: