Skip to content

Commit

Permalink
Typing conda_build.api (#5234)
Browse files Browse the repository at this point in the history
Co-authored-by: Bianca Henderson <bhenderson@anaconda.com>
  • Loading branch information
kenodegard and beeankha authored Apr 18, 2024
1 parent f2c3f3b commit e4434d9
Show file tree
Hide file tree
Showing 25 changed files with 716 additions and 497 deletions.
241 changes: 135 additions & 106 deletions conda_build/api.py

Large diffs are not rendered by default.

26 changes: 18 additions & 8 deletions conda_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from collections import OrderedDict, deque
from os.path import dirname, isdir, isfile, islink, join
from pathlib import Path
from typing import TYPE_CHECKING

import conda_package_handling.api
import yaml
Expand Down Expand Up @@ -85,6 +86,9 @@
if on_win:
from . import windows

if TYPE_CHECKING:
from typing import Any, Iterable

if "bsd" in sys.platform:
shell_path = "/bin/sh"
elif utils.on_win:
Expand Down Expand Up @@ -3322,12 +3326,12 @@ def write_test_scripts(


def test(
recipedir_or_package_or_metadata,
config,
stats,
move_broken=True,
provision_only=False,
):
recipedir_or_package_or_metadata: str | os.PathLike | Path | MetaData,
config: Config,
stats: dict,
move_broken: bool = True,
provision_only: bool = False,
) -> bool:
"""
Execute any test scripts for the given package.
Expand Down Expand Up @@ -3641,8 +3645,14 @@ def check_external():


def build_tree(
recipe_list, config, stats, build_only=False, post=None, notest=False, variants=None
):
recipe_list: Iterable[str | MetaData],
config: Config,
stats: dict,
build_only: bool = False,
post: bool | None = None,
notest: bool = False,
variants: dict[str, Any] | None = None,
) -> list[str]:
to_build_recursive = []
recipe_list = deque(recipe_list)

Expand Down
15 changes: 12 additions & 3 deletions conda_build/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

if TYPE_CHECKING:
from pathlib import Path
from typing import Any

invocation_time = ""

Expand Down Expand Up @@ -821,7 +822,7 @@ def clean_pkgs(self):
for folder in self.bldpkgs_dirs:
rm_rf(folder)

def copy(self):
def copy(self) -> Config:
new = copy.copy(self)
new.variant = copy.deepcopy(self.variant)
if hasattr(self, "variants"):
Expand All @@ -847,7 +848,11 @@ def __exit__(self, e_type, e_value, traceback):
self.clean(remove_folders=False)


def _get_or_merge_config(config, variant=None, **kwargs):
def _get_or_merge_config(
config: Config | None,
variant: dict[str, Any] | None = None,
**kwargs,
) -> Config:
# This function should only ever be called via get_or_merge_config.
# It only exists for us to monkeypatch a default config when running tests.
if not config:
Expand All @@ -863,7 +868,11 @@ def _get_or_merge_config(config, variant=None, **kwargs):
return config


def get_or_merge_config(config, variant=None, **kwargs):
def get_or_merge_config(
config: Config | None,
variant: dict[str, Any] | None = None,
**kwargs,
) -> Config:
"""Always returns a new object - never changes the config that might be passed in."""
return _get_or_merge_config(config, variant=variant, **kwargs)

Expand Down
36 changes: 23 additions & 13 deletions conda_build/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Tools for converting conda packages
"""

from __future__ import annotations

import glob
import hashlib
import json
Expand All @@ -14,8 +16,12 @@
import tarfile
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING

from .utils import ensure_list, filter_info_files, walk

from .utils import filter_info_files, walk
if TYPE_CHECKING:
from typing import Iterable


def retrieve_c_extensions(file_path, show_imports=False):
Expand Down Expand Up @@ -776,31 +782,35 @@ def convert_from_windows_to_unix(


def conda_convert(
file_path,
output_dir=".",
show_imports=False,
platforms=None,
force=False,
dependencies=None,
verbose=False,
quiet=False,
dry_run=False,
):
file_path: str,
output_dir: str = ".",
show_imports: bool = False,
platforms: str | Iterable[str] | None = None,
force: bool = False,
dependencies: str | Iterable[str] | None = None,
verbose: bool = False,
quiet: bool = False,
dry_run: bool = False,
) -> None:
"""Convert a conda package between different platforms and architectures.
Positional arguments:
file_path (str) -- the file path to the source package's tar file
output_dir (str) -- the file path to where to output the converted tar file
show_imports (bool) -- show all C extensions found in the source package
platforms (str) -- the platforms to convert to: 'win-64', 'win-32', 'linux-64',
platforms list[str] -- the platforms to convert to: 'win-64', 'win-32', 'linux-64',
'linux-32', 'osx-64', or 'all'
force (bool) -- force conversion of packages that contain C extensions
dependencies (List[str]) -- the new dependencies to add to the source package's
dependencies (list[str]) -- the new dependencies to add to the source package's
existing dependencies
verbose (bool) -- show output of items that are updated
quiet (bool) -- hide all output except warnings and errors
dry_run (bool) -- show which conversions will take place
"""

platforms = ensure_list(platforms)
dependencies = ensure_list(dependencies)

if show_imports:
imports = retrieve_c_extensions(file_path)
if len(imports) == 0:
Expand Down
16 changes: 9 additions & 7 deletions conda_build/develop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (C) 2014 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations

import shutil
import sys
from os.path import abspath, exists, expanduser, isdir, join
Expand Down Expand Up @@ -126,13 +128,13 @@ def _uninstall(sp_dir, pkg_path):


def execute(
recipe_dirs,
prefix=sys.prefix,
no_pth_file=False,
build_ext=False,
clean=False,
uninstall=False,
):
recipe_dirs: list[str],
prefix: str = sys.prefix,
no_pth_file: bool = False,
build_ext: bool = False,
clean: bool = False,
uninstall: bool = False,
) -> None:
if not isdir(prefix):
sys.exit(
"""\
Expand Down
4 changes: 2 additions & 2 deletions conda_build/inspect_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ def inspect_linkages(
all_packages: bool = False,
show_files: bool = False,
groupby: Literal["package", "dependency"] = "package",
sysroot="",
):
sysroot: str = "",
) -> str:
if not packages and not untracked and not all_packages:
sys.exit("At least one package or --untracked or --all must be provided")
elif on_win:
Expand Down
25 changes: 17 additions & 8 deletions conda_build/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import OrderedDict
from functools import lru_cache
from os.path import isfile, join
from typing import TYPE_CHECKING, overload
from typing import TYPE_CHECKING, NamedTuple, overload

from bs4 import UnicodeDammit
from conda.base.context import context
Expand Down Expand Up @@ -907,7 +907,10 @@ def toposort(output_metadata_map):
return result


def get_output_dicts_from_metadata(metadata, outputs=None):
def get_output_dicts_from_metadata(
metadata: MetaData,
outputs: list[dict[str, Any]] | None = None,
) -> list[dict[str, Any]]:
outputs = outputs or metadata.get_section("outputs")

if not outputs:
Expand Down Expand Up @@ -2514,16 +2517,16 @@ def get_reduced_variant_set(self, used_variables):

def get_output_metadata_set(
self,
permit_undefined_jinja=False,
permit_unsatisfiable_variants=False,
bypass_env_check=False,
):
permit_undefined_jinja: bool = False,
permit_unsatisfiable_variants: bool = False,
bypass_env_check: bool = False,
) -> list[tuple[dict[str, Any], MetaData]]:
from .source import provide

out_metadata_map = {}
if self.final:
outputs = get_output_dicts_from_metadata(self)[0]
output_tuples = [(outputs, self)]
outputs = get_output_dicts_from_metadata(self)
output_tuples = [(outputs[0], self)]
else:
all_output_metadata = OrderedDict()

Expand Down Expand Up @@ -2972,3 +2975,9 @@ def get_test_deps(self, py_files, pl_files, lua_files, r_files):

specs.extend(utils.ensure_list(self.config.extra_deps))
return specs


class MetaDataTuple(NamedTuple):
metadata: MetaData
need_download: bool
need_reparse: bool
4 changes: 2 additions & 2 deletions conda_build/os_utils/ldd.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def ldd(path):
def get_linkages(
obj_files: Iterable[str],
prefix: str | os.PathLike | Path,
sysroot,
sysroot: str,
) -> dict[str, list[tuple[str, str]]]:
return _get_linkages(tuple(obj_files), Path(prefix), sysroot)

Expand All @@ -61,7 +61,7 @@ def get_linkages(
def _get_linkages(
obj_files: tuple[str],
prefix: Path,
sysroot,
sysroot: str,
) -> dict[str, list[tuple[str, str]]]:
linkages = {}
for file in obj_files:
Expand Down
6 changes: 3 additions & 3 deletions conda_build/os_utils/pyldd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ def _get_magic_bit(path: Path) -> bytes:
return None


def _trim_sysroot(sysroot):
def _trim_sysroot(sysroot: str) -> str:
if sysroot:
while sysroot.endswith("/") or sysroot.endswith("\\"):
sysroot = sysroot[:-1]
Expand All @@ -1066,7 +1066,7 @@ def _get_arch_if_native(arch):

# TODO :: Consider memoizing instead of repeatedly scanning
# TODO :: libc.so/libSystem.dylib when inspect_linkages(recurse=True)
def _inspect_linkages_this(filename, sysroot="", arch="native"):
def _inspect_linkages_this(filename, sysroot: str = "", arch="native"):
"""
:param filename:
Expand Down Expand Up @@ -1100,7 +1100,7 @@ def _inspect_linkages_this(filename, sysroot="", arch="native"):

# TODO :: Consider returning a tree structure or a dict when recurse is True?
def inspect_linkages(
filename, resolve_filenames=True, recurse=True, sysroot="", arch="native"
filename, resolve_filenames=True, recurse=True, sysroot: str = "", arch="native"
):
already_seen = set()
todo = {filename}
Expand Down
Loading

0 comments on commit e4434d9

Please sign in to comment.