Skip to content

Commit

Permalink
Merge pull request #93 from tidyverse/f-68-stop-lossy
Browse files Browse the repository at this point in the history
- `vec_cast()` and `as_hms()` throw error instead of a warning if input can't be parsed (#68).
  • Loading branch information
krlmlr authored Jan 12, 2021
2 parents df9486f + fa77005 commit a4f667f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
9 changes: 4 additions & 5 deletions R/cast.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ vec_cast.integer.hms <- function(x, to, ...) as.integer(vec_data(x))
vec_cast.hms.character <- function(x, to, ...) {
ret <- parse_hms(x)
lossy <- is.na(ret) & !is.na(x)
warn_lossy_cast(x, to, ..., lossy = lossy)
abort_lossy_cast(x, to, ..., lossy = lossy)
ret
}

Expand All @@ -97,13 +97,12 @@ vec_default_cast_old <- function(x, to, ...) {
}
}

warn_lossy_cast <- function(x, to, ..., lossy) {
abort_lossy_cast <- function(x, to, ..., lossy) {
problems <- which(lossy)
if (is_empty(problems)) return()

warn(
paste0("Lossy cast from <character> to <hms> at position(s) ", commas(problems)),
.subclass = "hms_lossy_cast"
abort(
paste0("Lossy cast from <character> to <hms> at position(s) ", commas(problems))
)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-coercion.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("coercion in", {
expect_error(as_hms(FALSE))

x <- c("12:34:56", "ab:cd:ef")
expect_condition(hms::as_hms(x), class = "hms_lossy_cast")
expect_error(hms::as_hms(x))
})

test_that("coercion out", {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ test_that("range updating warns if lossy cast", {

x <- hms(1:3)

# r-lib/testthat#783
expect_warning(x[2] <- "a")
expect_error(x[2] <- "a")
})

test_that("index subsetting keeps class", {
Expand Down

0 comments on commit a4f667f

Please # to comment.