Skip to content

Commit

Permalink
Enhance Error Message for using := or let in Non-data.table-aware…
Browse files Browse the repository at this point in the history
… Environment (#6019)

* adding error for when := is called in not data.table aware enviroment

* added tests

* whitespace

* Update R/data.table.R

Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>

* Update tests.Rraw

* sprintf() won't coerce symbol->character, do it explicitly

* cleanup of test, better error message test

---------

Co-authored-by: nitish jha <nitishjha@nitishs-MacBook-Air.local>
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
  • Loading branch information
3 people authored Mar 25, 2024
1 parent b4ae3ef commit 566bff0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ replace_dot_alias = function(e) {
# the drop=NULL is to sink drop argument when dispatching to [.data.frame; using '...' stops test 147
if (!cedta()) {
# Fix for #500 (to do)
if (substitute(j) %iscall% c(":=", "let")) {
# Throw a specific error message
stopf("[ was called on a data.table in an environment that is not data.table-aware (i.e. cedta()), but '%s' was used, implying the owner of this call really intended for data.table methods to be called. See vignette('datatable-importing') for details on properly importing data.table.", as.character(substitute(j)[[1L]]))
}
Nargs = nargs() - (!missing(drop))
ans = if (Nargs<3L) { `[.data.frame`(x,i) } # drop ignored anyway by DF[i]
else if (missing(drop)) `[.data.frame`(x,i,j)
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18419,3 +18419,9 @@ test(2251.10, dim(fread(text, fill=TRUE)), c(9L, 9L))
test(2251.11, dim(fread(text, fill=7)), c(9L, 9L))
test(2251.12, dim(fread(text, fill=9)), c(9L, 9L))
test(2251.13, dim(fread(text, fill=20)), c(9L, 20L)) # clean up currently only kicks in if sep!=' '

.datatable.aware = FALSE
dt = data.table(a = 1L)
test(2252.1, dt[, b:=2L], error = "\\[ was called on a data.table.*not data.table-aware.*':='")
test(2252.2, dt[, let(b=2L)], error = "\\[ was called on a data.table.*not data.table-aware.*'let'")
rm(.datatable.aware)

0 comments on commit 566bff0

Please sign in to comment.