Skip to content

Commit

Permalink
Merge pull request #20 from baszalmstra/refactor/remove_self_type
Browse files Browse the repository at this point in the history
remove self type
  • Loading branch information
tdejager authored Jul 18, 2024
2 parents 48b2e63 + fb8509b commit ae3aeab
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 60 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/type-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# name: Run updater that will check for conda-forge packages
on:
push:
branches: [ "main" ]
pull_request:

jobs:
run_tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: prefix-dev/setup-pixi@v0.5.1
with:
pixi-version: "latest"
environments: lint

- name: type check
run: |
pixi run type-check
261 changes: 228 additions & 33 deletions pixi.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"]
build_sdist = "pixi run python -m build --sdist"

[dependencies]
python = ">=3.8"
python = ">=3.10"
build = "*"
rattler-build = "*"
conda-build = "*"
"ruamel.yaml" = "*"
conda = ">=4.2"
pygithub = ">=2,<3"
tomli = "*"
typing-extensions = ">=4.12.2,<4.13"

[pypi-dependencies]
rattler-build-conda-compat = { path = ".", editable = true}
Expand All @@ -39,10 +40,13 @@ pre-commit = ">=3.7.1,<4"
pre-commit-hooks = ">=4.6.0,<5"
ruff = ">=0.4.8,<0.5"
typos = ">=1.23.1,<2"
mypy = ">=1.10.1,<2"
types-pyyaml = ">=6.0.12.20240311,<6.0.13"

[feature.lint.tasks]
pre-commit-install = "pre-commit-install"
pre-commit-run = "pre-commit run"
type-check = "mypy src"


[environments]
Expand Down
13 changes: 10 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ version = "0.1.2"
readme = "README.md"
authors = [{ name = "Nichita Morcotilo", email = "nichita@prefix.dev" }]
license = { file = "LICENSE.txt" }

requires-python = ">=3.8"
dependencies = [
"typing-extensions>=4.12,<5"
]
requires-python = ">=3.10"

[tool.ruff]
target-version = "py38"
target-version = "py310"
line-length = 100

[tool.ruff.format]
Expand All @@ -30,6 +32,7 @@ ignore = [
"T201", # https://docs.astral.sh/ruff/rules/print/
"A003", # https://docs.astral.sh/ruff/rules/builtin-attribute-shadowing/
"PTH", # We dont want to change the API to pathlib just yet
"ANN101" # Deprecated
]
exclude = [
"src/rattler_build_conda_compat/lint.py",
Expand All @@ -43,3 +46,7 @@ exclude = [
[tool.pyright]
venvPath = ".pixi/envs"
venv = "default"

[tool.mypy]
python_version = "3.10"
allow_redefinition = true
17 changes: 11 additions & 6 deletions src/rattler_build_conda_compat/conditional_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from __future__ import annotations

from typing import Any, Callable, Generator, Generic, TypeVar, Union
from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, cast

if TYPE_CHECKING:
from collections.abc import Callable, Generator

T = TypeVar("T")
K = TypeVar("K")


class IfStatement(Generic[T]):
Expand All @@ -11,11 +15,12 @@ class IfStatement(Generic[T]):
else_: T | list[T] | None


ConditionalList = Union[T, "IfStatement[T]", list[Union[T, "IfStatement[T]"]]]
ConditionalList = Union[T, IfStatement[T], list[T | IfStatement[T]]] # noqa: UP007


def visit_conditional_list( # noqa: C901
value: ConditionalList[T], evaluator: Callable[[Any], bool] | None = None
value: T | IfStatement[T] | list[T | IfStatement[T]],
evaluator: Callable[[Any], bool] | None = None,
) -> Generator[T, None, None]:
"""
A function that yields individual branches of a conditional list.
Expand All @@ -30,7 +35,7 @@ def visit_conditional_list( # noqa: C901
A generator that yields the individual branches.
"""

def yield_from_list(value: list[T] | T) -> Generator[T, None, None]:
def yield_from_list(value: list[K] | K) -> Generator[K, None, None]:
if isinstance(value, list):
yield from value
else:
Expand All @@ -57,8 +62,8 @@ def yield_from_list(value: list[T] | T) -> Generator[T, None, None]:
yield from yield_from_list(otherwise)
else:
# In this case its not an if statement
yield element
yield cast(T, element)
# If the element is not a dictionary, just yield it
else:
# (tim) I get a pyright error here, but I don't know how to fix it
yield element
yield cast(T, element)
2 changes: 2 additions & 0 deletions src/rattler_build_conda_compat/lint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

import re

from inspect import cleandoc
Expand Down
13 changes: 8 additions & 5 deletions src/rattler_build_conda_compat/loader.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
from __future__ import annotations

from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Iterator, Self
from typing import TYPE_CHECKING, Any

import yaml

from rattler_build_conda_compat.conditional_list import visit_conditional_list

if TYPE_CHECKING:
from collections.abc import Iterator
from os import PathLike


class RecipeLoader(yaml.BaseLoader):
_namespace: dict[str, Any] | None = None

@classmethod
@contextmanager
def with_namespace(cls: Self, namespace: dict[str, Any] | None) -> Iterator[None]:
def with_namespace(cls: type[RecipeLoader], namespace: dict[str, Any] | None) -> Iterator[None]:
try:
cls._namespace = namespace
yield
finally:
del cls._namespace

def construct_sequence( # noqa: C901
self: Self,
node: yaml.Node,
self,
node: yaml.ScalarNode | yaml.SequenceNode | yaml.MappingNode,
deep: bool = False, # noqa: FBT002, FBT001
) -> list[yaml.Node]:
) -> list[yaml.ScalarNode]:
"""deep is True when creating an object/mapping recursively,
in that case want the underlying elements available during construction
"""
Expand Down
29 changes: 20 additions & 9 deletions src/rattler_build_conda_compat/recipe_sources.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
from __future__ import annotations

import sys
import typing
from typing import Any, Iterator, Mapping, NotRequired, TypedDict
from typing import Any, TypedDict

from .conditional_list import ConditionalList, visit_conditional_list

if sys.version_info < (3, 11):
from typing_extensions import NotRequired
else:
from typing import NotRequired

if typing.TYPE_CHECKING:
from collections.abc import Iterator, Mapping

OptionalUrlList = str | list[str] | None


Expand All @@ -29,10 +40,10 @@ def get_all_url_sources(recipe: Mapping[Any, Any]) -> Iterator[str]:

# Try getting all url top-level sources
if sources is not None:
sources = visit_conditional_list(sources, None)
for source in sources:
if "url" in source:
yield source["url"]
source_list = visit_conditional_list(sources, None)
for source in source_list:
if url := source.get("url"):
yield url

outputs = recipe.get("outputs", None)
if outputs is None:
Expand All @@ -44,7 +55,7 @@ def get_all_url_sources(recipe: Mapping[Any, Any]) -> Iterator[str]:
sources = typing.cast(ConditionalList[Source], sources)
if sources is None:
continue
sources = visit_conditional_list(sources, None)
for source in sources:
if "url" in source:
yield source["url"]
source_list = visit_conditional_list(sources, None)
for source in source_list:
if url := source.get("url"):
yield url
9 changes: 6 additions & 3 deletions src/rattler_build_conda_compat/render.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# mypy: ignore-errors

from collections import OrderedDict
import json
import os
Expand Down Expand Up @@ -66,9 +68,10 @@ def render_recipes(self, variants) -> List[Dict]:
platform_and_arch = f"{self.config.platform}-{self.config.arch}"

try:
with tempfile.NamedTemporaryFile(mode="w+") as outfile, tempfile.NamedTemporaryFile(
mode="w"
) as variants_file:
with (
tempfile.NamedTemporaryFile(mode="w+") as outfile,
tempfile.NamedTemporaryFile(mode="w") as variants_file,
):
# dump variants in our variants that will be used to generate recipe
if variants:
yaml.dump(variants, variants_file, default_flow_style=False)
Expand Down

0 comments on commit ae3aeab

Please sign in to comment.