Skip to content

Commit

Permalink
Simplifies py_require() code
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz committed Jan 22, 2025
1 parent 35be263 commit ef2d1ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
41 changes: 8 additions & 33 deletions R/py_require.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,45 +74,22 @@ py_require <- function(packages = NULL,
return(py_reqs_get())
}

req_packages <- py_reqs_get("packages")
if (!is.null(packages)) {
req_packages <- switch(action,
add = unique(c(req_packages, packages)),
remove = setdiff(req_packages, packages),
set = packages
)
}

req_python <- py_reqs_get("python_version")
if (!is.null(python_version)) {
req_python <- switch(action,
add = unique(c(python_version, req_python)),
remove = setdiff(req_python, python_version),
set = python_version
)
}

top_env <- topenv(parent.frame())

pr <- .globals$python_requirements
pr$packages <- req_packages
pr$python_version <- req_python
pr <- py_reqs_get()
pr$packages <- py_reqs_action(action, packages, py_reqs_get("packages"))
pr$python_version <- py_reqs_action(action, python_version, py_reqs_get("python_version"))
pr$exclude_newer <- pr$exclude_newer %||% exclude_newer
pr$history <- c(pr$history, list(list(
requested_from = environmentName(top_env),
env_is_package = isNamespace(top_env),
requested_from = environmentName(topenv(parent.frame())),
env_is_package = isNamespace(topenv(parent.frame())),
packages = packages,
python_version = python_version,
exclude_newer = exclude_newer,
action = action
)))
.globals$python_requirements <- pr

if (uv_initialized) {
new_path <- uv_get_or_create_env(
packages = pr$packages,
python_version = pr$python_version,
exclude_newer = pr$exclude_newer
)
new_path <- uv_get_or_create_env()
new_config <- python_config(new_path)
if (new_config$libpython == .globals$py_config$libpython) {
py_activate_virtualenv(file.path(dirname(new_path), "activate_this.py"))
Expand All @@ -124,8 +101,6 @@ py_require <- function(packages = NULL,
}
}

.globals$python_requirements <- pr

invisible()
}

Expand Down Expand Up @@ -217,7 +192,7 @@ py_reqs_action <- function(action, x, y = NULL) {
return(y)
}
switch(action,
add = unique(c(x, y)),
add = unique(c(y, x)),
remove = setdiff(y, x),
set = x
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/py_require.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
> py_require(python_version = ">=3.10")
> py_require(python_version = "<3.10")
> uv_get_or_create_env()
error: No interpreter found for Python <3.10, >=3.10 in virtual environments or managed installations
error: No interpreter found for Python >=3.10, <3.10 in virtual environments or managed installations
-- Current requirements -------------------------------------------------
Python: <3.10, >=3.10
Python: >=3.10, <3.10
Packages: numpy
-------------------------------------------------------------------------
Error: Call `py_require()` to remove or replace conflicting requirements.
Expand Down

0 comments on commit ef2d1ef

Please sign in to comment.