-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds names(qenv/qenv.error)
S3 methods
#218
base: main
Are you sure you want to change the base?
Conversation
Unit Tests Summary 1 files 10 suites 1s ⏱️ Results for commit faa843b. ♻️ This comment has been updated with latest results. |
Unit Test Performance Difference
Additional test case details
Results for commit dd2a739 ♻️ This comment has been updated with latest results. |
Code Coverage Summary
Diff against main
Results for commit: faa843b Minimum allowed coverage is ♻️ This comment has been updated with latest results |
R/qenv-names.R
Outdated
ls(get_env(x)) | ||
} | ||
|
||
#' @rdname names.qenv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to decide where to put names.qenv
. At this moment within
, eval_code
, get_code
are part of qenv
documentation and they are described in the sections. Problem with some methods (names
, within
) is that they have generic in base and it is not obvious that one needs to call names.qenv
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test cover new functions
NAMESPACE
Outdated
S3method("[[",qenv.error) | ||
S3method("[[<-",qenv.error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to preface that I'm more than happy to remove as this is **totally** nitpicking.
My reasoning adding this would be to make the API consistent
The 2 snippets below shows the **released/main** behavior and the assignments (<-
) seem inconsistent with qenv
.
It seems odd that with qenv.error
the script runs a few more commands.
qenv
object without errors
library(teal.code)
q <- qenv() |> within(a <- 2)
q$something <- 2
#> Error in `$<-`(`*tmp*`, something, value = 2): no method for assigning subsets of this S4 class
q[["another"]] <- 3
#> Error in `[[<-`(`*tmp*`, "another", value = 3): [[<- defined for objects of type "S4" only for subclasses of environment
q$something
#> Error in q$something: $ operator not defined for this S4 class
q[["another"]]
#> object 'another' not found
#> NULL
Created on 2024-10-29 with reprex v2.1.1
qenv
object when there's an error in within
code
library(teal.code)
q <- qenv() |> within(stop())
q$something <- 2
q[["another"]] <- 3
q$something
#> [1] 2
q[["another"]]
#> Error:
#> when evaluating qenv code:
#> stop()
Created on 2024-10-29 with reprex v2.1.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to fence all possible methods. There is large set of the methods inherited from environment
, it is important that officially supported functions are mentioned in the docs of qenv
. We need to mention inherited working methods but not add them. What I would add though to qenv.error
is $.qenv.error
, [[.qenv.error
, as.list.qenv.error
, ls.qenv.error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ls.qenv.error
cannot be added, but it will still give an error, so no harm there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, there is a difference between throwing error and returning error. ls(<qenv.error>)
will throw while names.qenv.error(<qenv.error>)
will return error. The first one needs to be tryCatched while second one not. We need to check if ls(data())
in teal
internals doesn't throw because of this (probably not as teal
is protected against any types of errors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some topics to discuss on this implementation
@@ -31,7 +31,7 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code | |||
id <- sample.int(.Machine$integer.max, size = 1) | |||
|
|||
object@id <- c(object@id, id) | |||
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv)) | |||
object@.xData <- rlang::env_clone(object@.xData, parent = parent.env(.GlobalEnv)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{rlang} functions don't play nicely with this approach, so we need to access the slot directly
🔴 Could this lack of support from {rlang} be a blocker for this new approach?
pkgload::load_all("teal.code")
#> ℹ Loading teal.code
q1 <- qenv()
rlang::is_environment(q1)
#> [1] FALSE
base::is.environment(q1)
#> [1] TRUE
rlang::env_clone(q1)
#> Error in `rlang::env_clone()`:
#> ! `env` must be an environment, not a <qenv> object.
rlang::is_environment
#> function(x) {
#> typeof(x) == "environment"
#> }
#> <bytecode: 0x65127a0d6b08>
#> <environment: namespace:rlang>
typeof(q1)
#> [1] "S4"
Created on 2024-10-29 with reprex v2.1.1
@@ -55,3 +55,29 @@ setMethod("[[", signature = c("qenv", "ANY"), function(x, i) { | |||
class = c("validation", "try-error", "simpleError") | |||
)) | |||
} | |||
|
|||
#' @export | |||
`$.qenv.error` <- function(x, name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Should we add this method and have it behave just like [[
?
We need to allow $
to retrieve existing properties of qenv.error
, as conditionMessage(x)
will try to access x$message
, x$call
and x$trace
.
return(NextMethod("$", x)) | ||
} | ||
|
||
class(x) <- setdiff(class(x), "qenv.error") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Safeguard to prevent infinite recursion, this can be "safely" removed if reviewer deems necessary
@@ -1,19 +1,67 @@ | |||
testthat::describe("qenv inherits from environment: ", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test cover new functions
ℹ️ Added tests that addressed last comment before the "change" (covering a wider range of functionalities)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some @env
remaining in R/
directory . Also there shouldn't be any mention about internal slots (regex:#'.+@(env|.xData|code|id|warnings|messages)
in the documentation @
- please replace them with something implicit.
(you can leave join docs unchanged - this is a bigger problem)
@@ -23,9 +45,7 @@ | |||
#' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add usage of names
, [[
and $
to the examples section somewhere before # retrieve code
. This could be a "section" # extracting objects from qenv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See 0a644988135a098665-af7054b837b.
I may have gone a bit overboard by including these methods in the usage section, WDYT?
Co-authored-by: Dawid Kałędkowski <[email protected]> Signed-off-by: André Veríssimo <[email protected]>
Co-authored-by: Dawid Kałędkowski <[email protected]> Signed-off-by: André Veríssimo <[email protected]>
Pull Request
$
method to access objs from env #164names()
function and deprecatesdatanames()
teal.data#347Changes description
qenv
S4 class inherits fromenvironment
data class@env
slot in favor ofqenv
@env
with@.xData
(slot created by parent class)