Skip to content

Commit

Permalink
Starts adding new tests, defaults get_or_create_venv() args to use wh…
Browse files Browse the repository at this point in the history
…at's accumulated in py_require() and adds a conditional for package error print out
  • Loading branch information
edgararuiz committed Jan 13, 2025
1 parent ef4e2fc commit bea217d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 35 deletions.
13 changes: 7 additions & 6 deletions R/py_require.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ uv_binary <- function() {
# TODO: we should pass --cache-dir=file.path(rappdirs::user_cache_dir("r-reticulate"), "uv-cache")
# if we are using a reticulate-managed uv installation.

get_or_create_venv <- function(packages = "numpy",
python_version = NULL,
exclude_newer = NULL) {
get_or_create_venv <- function(packages = get_python_reqs("packages"),
python_version = get_python_reqs("python_version"),
exclude_newer = get_python_reqs("exclude_newer")) {
if (length(packages))
packages <- as.vector(rbind("--with", maybe_shQuote(packages)))

Expand Down Expand Up @@ -218,9 +218,10 @@ get_or_create_venv <- function(packages = "numpy",
"Python requirements could not be satisfied.",
if (!is.null(python_version))
paste0("Python version: ", python_version[2]),
# TODO: wrap+indent+un_shQuote python packages
paste0(c("Python dependencies: ", matrix(packages, nrow = 2)[2, ]),
collapse = " "),
if (!is.null(packages))
# TODO: wrap+indent+un_shQuote python packages
paste0(c("Python dependencies: ", matrix(packages, nrow = 2)[2, ]),
collapse = " "),
if (!is.null(exclude_newer))
paste0("Exclude newer: ", exclude_newer[2]),
"Call `py_require()` to remove or replace conflicting requirements."
Expand Down
86 changes: 82 additions & 4 deletions tests/testthat/_snaps/py_require.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Error requesting newer package version against an older snapshot
# Error requesting conflicting package versions

Code
r_session(attach_namespace = TRUE, {
get_or_create_venv(c("numpy<2", "numpy>=2"))
py_require("numpy<2")
py_require("numpy>=2")
get_or_create_venv()
})
Output
> get_or_create_venv(c("numpy<2", "numpy>=2"))
> py_require("numpy<2")
> py_require("numpy>=2")
> get_or_create_venv()
× No solution found when resolving `--with` dependencies:
╰─▶ Because you require numpy<2 and numpy>=2, we can conclude that your
requirements are unsatisfiable.
Error in get_or_create_venv(c("numpy<2", "numpy>=2")) :
Error in get_or_create_venv() :
Python requirements could not be satisfied.
Python dependencies: 'numpy<2' 'numpy>=2'
Call `py_require()` to remove or replace conflicting requirements.
Expand All @@ -18,3 +22,77 @@
success: false
exit_code: 1

# Error requesting newer package version against an older snapshot

Code
r_session(attach_namespace = TRUE, {
py_require("tensorflow==2.18.*")
py_require(exclude_newer = "2024-10-20")
get_or_create_venv()
})
Output
> py_require("tensorflow==2.18.*")
> py_require(exclude_newer = "2024-10-20")
> get_or_create_venv()
× No solution found when resolving `--with` dependencies:
╰─▶ Because only tensorflow<2.18.dev0 is available and you require
tensorflow>=2.18.dev0, we can conclude that your requirements are
unsatisfiable.
hint: `tensorflow` was requested with a pre-release marker (e.g.,
tensorflow>=2.18.dev0), but pre-releases weren't enabled (try:
`--prerelease=allow`)
Error in get_or_create_venv() :
Python requirements could not be satisfied.
Python dependencies: 'tensorflow==2.18.*'
Exclude newer: 2024-10-20
Call `py_require()` to remove or replace conflicting requirements.
Execution halted
------- session end -------
success: false
exit_code: 1

# Error requesting a package that does not exists

Code
r_session(attach_namespace = TRUE, {
py_require(c("pandas", "numpy", "notexists"))
get_or_create_venv()
})
Output
> py_require(c("pandas", "numpy", "notexists"))
> get_or_create_venv()
× No solution found when resolving `--with` dependencies:
╰─▶ Because notexists was not found in the package registry and you require
notexists, we can conclude that your requirements are unsatisfiable.
Error in get_or_create_venv() :
Python requirements could not be satisfied.
Python dependencies: pandas numpy notexists
Call `py_require()` to remove or replace conflicting requirements.
Execution halted
------- session end -------
success: false
exit_code: 1

# Error requesting conflicting Python versions

Code
r_session(attach_namespace = TRUE, {
py_require(python_version = ">=3.10")
py_require(python_version = "<3.10")
get_or_create_venv()
})
Output
> py_require(python_version = ">=3.10")
> py_require(python_version = "<3.10")
> get_or_create_venv()
error: No interpreter found for Python <3.10, >=3.10 in virtual environments or managed installations
Error in get_or_create_venv() :
Python requirements could not be satisfied.
Python version: '<3.10,>=3.10'
Call `py_require()` to remove or replace conflicting requirements.
Execution halted
------- session end -------
success: false
exit_code: 1

55 changes: 30 additions & 25 deletions tests/testthat/test-py_require.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
test_that("Error requesting conflicting package versions", {
local_edition(3)
expect_snapshot(r_session(attach_namespace = TRUE, {
py_require("numpy<2")
py_require("numpy>=2")
get_or_create_venv()
}))
})

test_that("Error requesting newer package version against an older snapshot", {
local_edition(3)
expect_snapshot(r_session(attach_namespace = TRUE, {
py_require("tensorflow==2.18.*")
py_require(exclude_newer = "2024-10-20")
get_or_create_venv()
}))
})

test_that("Error requesting a package that does not exists", {
local_edition(3)
expect_snapshot(r_session(attach_namespace = TRUE, {
py_require(c("pandas", "numpy", "notexists"))
get_or_create_venv()
}))
})


test_that("Error requesting newer package version against an older snapshot", {
test_that("Error requesting conflicting Python versions", {
local_edition(3)
expect_snapshot(r_session(attach_namespace = TRUE, {
get_or_create_venv(c("numpy<2", "numpy>=2"))
py_require(python_version = ">=3.10")
py_require(python_version = "<3.10")
get_or_create_venv()
}))
})
# test_py_require_reset()
# py_require("tensorflow==2.18.*", exclude_newer = "2024-10-20")
# expect_snapshot({
# get_or_create_venv("tensorflow==2.18.*", exclude_newer = "2024-10-20")
# get_or_create_venv("tensorflow==2.18.*")#, exclude_newer = "2024-10-20")
# }, error = TRUE)
#
# test_that("Error requesting conflicting package versions", {
# local_edition(3)
# test_py_require_reset()
# py_require("pandas==2.2.3")
# py_require("pandas==2.2.2")
# expect_snapshot(get_or_create_venv(), error = TRUE)
# })
#
# test_that("Error requesting conflicting Python versions", {
# local_edition(3)
# test_py_require_reset()
# py_require(python_version = ">=3.10")
# py_require(python_version = "3.11")
# expect_snapshot(get_or_create_venv(), error = TRUE)
# })



0 comments on commit bea217d

Please sign in to comment.