Skip to content

Commit

Permalink
Apply ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Sep 28, 2024
1 parent c730d12 commit db87864
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 66 deletions.
51 changes: 25 additions & 26 deletions grayskull/license/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pathlib import Path
from subprocess import check_output
from tempfile import mkdtemp
from typing import List, Optional, Tuple, Union

import requests
from colorama import Fore
Expand All @@ -29,12 +28,12 @@
@dataclass
class ShortLicense:
name: str
path: Union[str, Path, None]
path: str | Path | None
is_packaged: bool


@lru_cache(maxsize=10)
def get_all_licenses_from_spdx() -> List:
def get_all_licenses_from_spdx() -> list:
"""Get all licenses available on spdx.org
:return: List with all licenses information on spdx.org
Expand Down Expand Up @@ -165,7 +164,7 @@ def get_short_license_id(name: str) -> str:
return recipe_license["licenseId"]


def _get_license(license_id: str, all_licenses: List) -> dict:
def _get_license(license_id: str, all_licenses: list) -> dict:

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
"""Search for the license identification in all licenses received
:param license_id: license identification
Expand All @@ -177,7 +176,7 @@ def _get_license(license_id: str, all_licenses: List) -> dict:
return one_license


def _get_all_names_from_api(one_license: dict) -> List:
def _get_all_names_from_api(one_license: dict) -> list:
"""Get the names and other names which each license has.
:param one_license: License name
Expand All @@ -193,7 +192,7 @@ def _get_all_names_from_api(one_license: dict) -> List:
return list(result)


def get_other_names_from_opensource(license_spdx: str) -> List:
def get_other_names_from_opensource(license_spdx: str) -> list:
lic = get_opensource_license(license_spdx)
return [_license["name"] for _license in lic.get("other_names", [])]

Expand All @@ -215,7 +214,7 @@ def read_licence_cache():


@lru_cache(maxsize=10)
def get_opensource_license_data() -> List:
def get_opensource_license_data() -> list:
try:
response = requests.get(url="https://api.opensource.org/licenses/", timeout=5)
except requests.exceptions.RequestException:
Expand All @@ -225,7 +224,7 @@ def get_opensource_license_data() -> List:
return response.json()


def _get_all_license_choice(all_licenses: List) -> List:
def _get_all_license_choice(all_licenses: list) -> list:
"""Function responsible to get the whole licenses name
:param all_licenses: list with all licenses
Expand All @@ -239,11 +238,11 @@ def _get_all_license_choice(all_licenses: List) -> List:

def search_license_file(
folder_path: str,
git_url: Optional[str] = None,
version: Optional[str] = None,
license_name_metadata: Optional[str] = None,
folders_exclude_search: Tuple[str] = tuple(),
) -> List[ShortLicense]:
git_url: str | None = None,
version: str | None = None,
license_name_metadata: str | None = None,
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
clone the repository and it will search for the license there.
Expand Down Expand Up @@ -288,8 +287,8 @@ def search_license_file(

@lru_cache(maxsize=13)
def search_license_api_github(
github_url: str, version: Optional[str] = None, default: Optional[str] = "Other"
) -> Optional[ShortLicense]:
github_url: str, version: str | None = None, default: str | None = "Other"
) -> ShortLicense | None:
"""Search for the license asking in the github api
:param github_url: GitHub URL
Expand Down Expand Up @@ -317,7 +316,7 @@ def search_license_api_github(
)


def _get_api_github_url(github_url: str, version: Optional[str] = None) -> str:
def _get_api_github_url(github_url: str, version: str | None = None) -> str:
"""Try to presume the github url
:param github_url: GitHub URL
Expand All @@ -333,10 +332,10 @@ def _get_api_github_url(github_url: str, version: Optional[str] = None) -> str:


def search_license_folder(
path: Union[str, Path],
default: Optional[str] = None,
folders_exclude_search: Tuple[str] = tuple(),
) -> List[ShortLicense]:
path: str | Path,
default: str | None = None,
folders_exclude_search: tuple[str] = tuple(),
) -> list[ShortLicense]:
"""Search for the license in the given folder
:param path: Sdist folder
Expand Down Expand Up @@ -368,10 +367,10 @@ def search_license_folder(

def search_license_repo(
git_url: str,
version: Optional[str],
default: Optional[str] = None,
folders_exclude_search: Tuple[str] = tuple(),
) -> Optional[List[ShortLicense]]:
version: str | None,
default: str | None = None,
folders_exclude_search: tuple[str] = tuple(),
) -> list[ShortLicense] | None:
"""Search for the license file in the given github repository
:param git_url: GitHub URL
Expand Down Expand Up @@ -401,7 +400,7 @@ def search_license_repo(
)


def _get_git_cmd(git_url: str, version: str, dest) -> List[str]:
def _get_git_cmd(git_url: str, version: str, dest) -> list[str]:
"""Return the full git command to clone the repository
:param git_url: GitHub URL
Expand All @@ -415,7 +414,7 @@ def _get_git_cmd(git_url: str, version: str, dest) -> List[str]:
return git_cmd + [git_url, str(dest)]


def get_license_type(path_license: str, default: Optional[str] = None) -> Optional[str]:
def get_license_type(path_license: str, default: str | None = None) -> str | None:
"""Function tries to match the license with one of the models present in
grayskull/license/data
Expand Down
3 changes: 1 addition & 2 deletions grayskull/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import sys
from pathlib import Path
from typing import List, Optional

import requests
from colorama import Fore, Style, init
Expand Down Expand Up @@ -447,7 +446,7 @@ def create_r_recipe(pkg_name, sections_populate=None, **kwargs):
)


def add_extra_section(recipe, maintainers: Optional[List] = None):
def add_extra_section(recipe, maintainers: list | None = None):
maintainers = maintainers or [get_git_current_user()]
if "extra" in recipe:
recipe["extra"]["recipe-maintainers"] = maintainers
Expand Down
3 changes: 1 addition & 2 deletions grayskull/strategy/cran.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from copy import deepcopy
from os.path import basename
from tempfile import mkdtemp
from typing import Optional
from urllib.request import Request, urlopen

import requests
Expand Down Expand Up @@ -237,7 +236,7 @@ def get_webpage(cran_url):
return BeautifulSoup(html_page, features="html.parser")


def get_cran_index(cran_url: str, pkg_name: str, pkg_version: Optional[str] = None):
def get_cran_index(cran_url: str, pkg_name: str, pkg_version: str | None = None):
"""Fetch the entire CRAN index and store it."""
print_msg(f"Fetching main index from {cran_url}")

Expand Down
39 changes: 19 additions & 20 deletions grayskull/strategy/py_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pathlib import Path
from subprocess import check_output
from tempfile import mkdtemp
from typing import Dict, List, Optional, Tuple, Union
from urllib.parse import urlparse

import requests
Expand Down Expand Up @@ -43,7 +42,7 @@
PIN_PKG_COMPILER = {"numpy": "<{ pin_compatible('numpy') }}"}


def search_setup_root(path_folder: Union[Path, str]) -> Path:
def search_setup_root(path_folder: Path | str) -> Path:

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
if setup_py := list(Path(path_folder).rglob("setup.py")):
return setup_py[0]
if setup_cfg := list(Path(path_folder).rglob("setup.cfg")):
Expand All @@ -52,7 +51,7 @@ def search_setup_root(path_folder: Union[Path, str]) -> Path:
return pyproject_toml[0]


def clean_deps_for_conda_forge(list_deps: List, py_ver_min: PyVer) -> List:
def clean_deps_for_conda_forge(list_deps: list, py_ver_min: PyVer) -> list:
"""Remove dependencies which conda-forge is not supporting anymore.
For example Python 2.7, Python version less than 3.6"""
re_delimiter = re.compile(r"#\s+\[py\s*(?:([<>=!]+))?\s*(\d+)\]\s*$", re.DOTALL)
Expand Down Expand Up @@ -119,7 +118,7 @@ def parse_extra_metadata_to_selector(option: str, operation: str, value: str) ->
return value_lower


def get_extra_from_requires_dist(string_parse: str) -> Union[List]:
def get_extra_from_requires_dist(string_parse: str) -> list:
"""Receives the extra metadata e parse it to get the option, operation
and value.
Expand All @@ -133,7 +132,7 @@ def get_extra_from_requires_dist(string_parse: str) -> Union[List]:
)


def get_name_version_from_requires_dist(string_parse: str) -> Tuple[str, str]:
def get_name_version_from_requires_dist(string_parse: str) -> tuple[str, str]:
"""Extract the name and the version from `requires_dist` present in
PyPi`s metadata
Expand All @@ -150,7 +149,7 @@ def get_name_version_from_requires_dist(string_parse: str) -> Tuple[str, str]:

def generic_py_ver_to(
metadata: dict, config: Configuration, is_selector: bool = False
) -> Optional[str]: # sourcery no-metrics
) -> str | None: # sourcery no-metrics
"""Generic function which abstract the parse of the requires_python
present in the PyPi metadata. Basically it can generate the selectors
for Python or the constrained version if it is a `noarch: python` python package"""
Expand Down Expand Up @@ -346,7 +345,7 @@ def __fake_distutils_setup(*args, **kwargs):
if not isinstance(kwargs, dict) or not kwargs:
return

def _fix_list_requirements(key_deps: str) -> List:
def _fix_list_requirements(key_deps: str) -> list:
"""Fix when dependencies have lists inside of another sequence"""
val_deps = kwargs.get(key_deps)
if not val_deps:
Expand Down Expand Up @@ -469,8 +468,8 @@ def __run_setup_py(path_setup: str, data_dist: dict, run_py=False, deps_installe


def get_compilers(
requires_dist: List, sdist_metadata: dict, config: Configuration
) -> List:
requires_dist: list, sdist_metadata: dict, config: Configuration
) -> list:
"""Return which compilers are necessary"""
compilers = set(sdist_metadata.get("compilers", []))
for pkg in requires_dist:
Expand All @@ -484,10 +483,10 @@ def get_compilers(


def get_py_multiple_selectors(
selectors: Dict[PyVer, bool],
selectors: dict[PyVer, bool],
config: Configuration,
is_selector: bool = False,
) -> List:
) -> list:
"""Get python selectors available.
:param selectors: Dict with the Python version and if it is selected
Expand All @@ -513,11 +512,11 @@ def get_py_multiple_selectors(
return all_selector


def py_version_to_selector(pypi_metadata: dict, config) -> Optional[str]:
def py_version_to_selector(pypi_metadata: dict, config) -> str | None:
return generic_py_ver_to(pypi_metadata, is_selector=True, config=config)


def py_version_to_limit_python(pypi_metadata: dict, config=None) -> Optional[str]:
def py_version_to_limit_python(pypi_metadata: dict, config=None) -> str | None:
config = config or Configuration(pypi_metadata["name"])
result = generic_py_ver_to(pypi_metadata, is_selector=False, config=config)
if not result and config.is_strict_cf:
Expand Down Expand Up @@ -557,7 +556,7 @@ def clean_list_pkg(pkg, list_pkgs):
requirements["run"].append(PIN_PKG_COMPILER[pkg_name])


def discover_license(metadata: dict) -> List[ShortLicense]:
def discover_license(metadata: dict) -> list[ShortLicense]:
"""Based on the metadata this method will try to discover what is the
right license for the package
Expand All @@ -581,13 +580,13 @@ def discover_license(metadata: dict) -> List[ShortLicense]:
)


def get_test_entry_points(entry_points: Union[List, str]) -> List:
def get_test_entry_points(entry_points: list | str) -> list:
if entry_points and isinstance(entry_points, str):
entry_points = [entry_points]
return [f"{ep.split('=')[0].strip()} --help" for ep in entry_points]


def get_test_imports(metadata: dict, default: Optional[str] = None) -> List:
def get_test_imports(metadata: dict, default: str | None = None) -> list:
if default:
default = default.replace("-", "_")
if "packages" not in metadata or not metadata["packages"]:
Expand All @@ -611,7 +610,7 @@ def get_test_imports(metadata: dict, default: Optional[str] = None) -> List:
return result


def get_entry_points_from_sdist(sdist_metadata: dict) -> List:
def get_entry_points_from_sdist(sdist_metadata: dict) -> list:
"""Extract entry points from sdist metadata
:param sdist_metadata: sdist metadata
Expand Down Expand Up @@ -666,7 +665,7 @@ def get_entry_points_from_sdist(sdist_metadata: dict) -> List:
return []


def download_sdist_pkg(sdist_url: str, dest: str, name: Optional[str] = None):
def download_sdist_pkg(sdist_url: str, dest: str, name: str | None = None):
"""Download the sdist package
:param sdist_url: sdist url
Expand Down Expand Up @@ -819,11 +818,11 @@ def get_sdist_metadata(
return merge_setup_toml_metadata(metadata, pyproject_metadata)


def ensure_pep440_in_req_list(list_req: List[str]) -> List[str]:
def ensure_pep440_in_req_list(list_req: list[str]) -> list[str]:
return [ensure_pep440(pkg) for pkg in list_req]


def split_deps(deps: str) -> List[str]:
def split_deps(deps: str) -> list[str]:
result = []
for d in deps.split(","):
constrain = ""
Expand Down
Loading

0 comments on commit db87864

Please sign in to comment.