From 566bff0fe1a10d94a494026c59eb611b90b4dc04 Mon Sep 17 00:00:00 2001 From: Nitish Jha <151559388+Nj221102@users.noreply.github.com> Date: Tue, 26 Mar 2024 05:26:39 +0530 Subject: [PATCH] Enhance Error Message for using `:=` or `let` in Non-data.table-aware 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 * Update tests.Rraw * sprintf() won't coerce symbol->character, do it explicitly * cleanup of test, better error message test --------- Co-authored-by: nitish jha Co-authored-by: Michael Chirico --- R/data.table.R | 4 ++++ inst/tests/tests.Rraw | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/R/data.table.R b/R/data.table.R index f7b9b4192..24eff62d5 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -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) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 2e75c8c96..24b4f4c60 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -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)