From b7780500966a224ae9568594000da788df3e3a80 Mon Sep 17 00:00:00 2001 From: Ben Mares Date: Sun, 10 Nov 2024 10:40:25 +0100 Subject: [PATCH] Improve some type hints --- grayskull/config.py | 8 ++++---- grayskull/license/discovery.py | 6 +++--- grayskull/strategy/cran.py | 2 +- grayskull/strategy/py_base.py | 5 +++-- grayskull/strategy/pypi.py | 11 +++++------ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/grayskull/config.py b/grayskull/config.py index e9df19ca3..c278f9634 100644 --- a/grayskull/config.py +++ b/grayskull/config.py @@ -1,5 +1,5 @@ from dataclasses import dataclass, field -from typing import Dict, List, Optional, Tuple +from typing import Dict, Iterable, List, Optional, Tuple from grayskull.cli.parser import parse_pkg_name_version from grayskull.utils import PyVer @@ -50,11 +50,11 @@ class Configuration: missing_deps: set = field(default_factory=set) extras_require_test: Optional[str] = None github_release_tag: Optional[str] = None - extras_require_include: Tuple[str] = tuple() - extras_require_exclude: Tuple[str] = tuple() + extras_require_include: Iterable[str] = tuple() + extras_require_exclude: Iterable[str] = tuple() extras_require_all: bool = False extras_require_split: bool = False - licence_exclude_folders: Tuple[str] = tuple() + licence_exclude_folders: Iterable[str] = tuple() def get_oldest_py3_version(self, list_py_ver: List[PyVer]) -> PyVer: list_py_ver = sorted(list_py_ver) diff --git a/grayskull/license/discovery.py b/grayskull/license/discovery.py index 111152c66..4f9edec24 100644 --- a/grayskull/license/discovery.py +++ b/grayskull/license/discovery.py @@ -241,7 +241,7 @@ def search_license_file( git_url: str | None = None, version: str | None = None, license_name_metadata: str | None = None, - folders_exclude_search: tuple[str] = tuple(), + folders_exclude_search: tuple[str, ...] = tuple(), ) -> list[ShortLicense]: """Search for the license file. First it will try to find it in the given folder, after that it will search on the github api and for the last it will @@ -334,7 +334,7 @@ def _get_api_github_url(github_url: str, version: str | None = None) -> str: def search_license_folder( path: str | Path, default: str | None = None, - folders_exclude_search: tuple[str] = tuple(), + folders_exclude_search: tuple[str, ...] = tuple(), ) -> list[ShortLicense]: """Search for the license in the given folder @@ -369,7 +369,7 @@ def search_license_repo( git_url: str, version: str | None, default: str | None = None, - folders_exclude_search: tuple[str] = tuple(), + folders_exclude_search: tuple[str, ...] = tuple(), ) -> list[ShortLicense] | None: """Search for the license file in the given github repository diff --git a/grayskull/strategy/cran.py b/grayskull/strategy/cran.py index b831a2017..5bf33461d 100644 --- a/grayskull/strategy/cran.py +++ b/grayskull/strategy/cran.py @@ -181,7 +181,7 @@ def get_archive_metadata(path): def scrap_main_page_cran_find_latest_package( - cran_url: str, pkg_name: str, pkg_version: str + cran_url: str, pkg_name: str, pkg_version: str | None ): pkg_name = pkg_name.strip().lower() pkg_version = pkg_version.strip().lower() if pkg_version else None diff --git a/grayskull/strategy/py_base.py b/grayskull/strategy/py_base.py index dcd9f1cac..c9f0808c8 100644 --- a/grayskull/strategy/py_base.py +++ b/grayskull/strategy/py_base.py @@ -13,6 +13,7 @@ from pathlib import Path from subprocess import check_output from tempfile import mkdtemp +from typing import ContextManager from urllib.parse import urlparse import requests @@ -319,7 +320,7 @@ def get_setup_cfg(source_path: str) -> dict: @contextmanager -def injection_distutils(folder: str) -> dict: +def injection_distutils(folder: str) -> ContextManager[dict]: """This is a bit of "dark magic", please don't do it at home. It is injecting code in the distutils.core.setup and replacing the setup function by the inner function __fake_distutils_setup. @@ -836,7 +837,7 @@ def split_deps(deps: str) -> list[str]: return result -def ensure_pep440(pkg: str) -> str: +def ensure_pep440(pkg: str | None) -> str | None: if not pkg or RE_PEP725_PURL.match(pkg): return pkg pkg = pkg.strip() diff --git a/grayskull/strategy/pypi.py b/grayskull/strategy/pypi.py index 9802c78c7..bf6160de5 100644 --- a/grayskull/strategy/pypi.py +++ b/grayskull/strategy/pypi.py @@ -5,10 +5,9 @@ import logging import os import re -from collections.abc import Mapping from pathlib import Path from tempfile import mkdtemp -from typing import Iterable +from typing import Iterable, MutableMapping import requests from colorama import Fore @@ -155,7 +154,7 @@ def get_sha256_from_pypi_metadata(pypi_metadata: dict) -> str: raise AttributeError("Hash information for sdist was not found on PyPi metadata.") -def get_sdist_url_from_pypi(metadata: dict) -> str: +def get_sdist_url_from_pypi(metadata: dict) -> str | None: """Return the sdist url looking for the pypi metadata :param metadata: pypi metadata @@ -489,9 +488,9 @@ def get_metadata(recipe, config) -> dict: } -def remove_all_inner_nones(metadata: dict) -> dict: +def remove_all_inner_nones(metadata): """Remove all inner None values from a dictionary.""" - if not isinstance(metadata, Mapping): + if not isinstance(metadata, MutableMapping): return metadata for k, v in metadata.items(): if not isinstance(v, list): @@ -500,7 +499,7 @@ def remove_all_inner_nones(metadata: dict) -> dict: return metadata -def update_recipe(recipe: Recipe, config: Configuration, all_sections: list[str]): +def update_recipe(recipe: Recipe, config: Configuration, all_sections: Iterable[str]): """Update one specific section.""" from souschef.section import Section