Skip to content

Refactoring with checkmate and rlang - #548

Merged
billdenney merged 12 commits into
humanpred:mainfrom
PavanLomati:checkmate-rlang-refactor
Aug 17, 2026
Merged

Refactoring with checkmate and rlang#548
billdenney merged 12 commits into
humanpred:mainfrom
PavanLomati:checkmate-rlang-refactor

Conversation

@PavanLomati

Copy link
Copy Markdown
Contributor

No description provided.

@PavanLomati
PavanLomati requested a review from billdenney May 27, 2026 04:18
@billdenney

Copy link
Copy Markdown
Member

This looks like it will make all of the code more consistent. Thank you!. Please merge in the current origin/main branch and resolve the conflicts.

Also, for many of the checkmate calls, you've added .var.name = "name" or similar where name is the parameter name used. Please omit those as they are not necessary since they are the default that checkmate will provide.

…ng-refactor

# Conflicts:
#	R/001-add.interval.col.R
#	R/PKNCA.options.R
#	R/assertions.R
#	R/auc.R
#	R/aucint.R
#	R/class-PKNCAdata.R
#	R/class-PKNCAdose.R
#	R/class-general.R
#	R/class-summary_PKNCAresults.R
#	R/exclude.R
#	R/interpolate.conc.R
#	R/pk.calc.all.R
#	R/pk.calc.c0.R
#	R/superposition.R
#	R/tss.R
#	R/tss.monoexponential.R
#	R/tss.stepwise.linear.R
#	tests/testthat/test-PKNCA.options.R
Comment thread R/assertions.R Outdated
Comment thread R/assertions.R Outdated
Comment thread R/auc.R Outdated
Comment thread R/auc.R Outdated
Comment thread R/auc.R Outdated
Comment thread R/aucint.R Outdated
Comment thread tests/testthat/test-001-add.interval.col.R
@billdenney

Copy link
Copy Markdown
Member

@PavanLomati

Here are the comprehensive findings:

Additional review findings

These supplement the 6 inline comments already on the diff (non-unique classes, conditionMessage(), paste() in messages, stray #'). They're organized by class, highest-leverage first. All behavioral claims were verified against the base branch.

Severity token missing from some preexisting condition classes — breaks class-based capture

Three internal "report a bug" errors are raised with a pknca_internal_* class instead of pknca_error_*:

  • pknca_internal_pk_tss_ret_non_naR/tss.R
  • pknca_internal_dose_scalingR/superposition.R
  • pknca_internal_unknown_nca_unitsR/unit-support.R

Their siblings correctly use pknca_error_internal_* (pknca_error_internal_tlast in R/auc.R, pknca_error_internal_clast_na in R/aucint.R). A handler filtering on pknca_error_* will silently miss the three pknca_internal_* ones. Please standardize on pknca_error_internal_*.

Negative-path tests deleted instead of migrated — coverage regression

Please restore the tests to match the previous intent.

Across ~10 test files, the expect_error(..., regexp=) cases that exercised the exact validation being refactored were removed rather than updated to the new message/class:

  • test-001-add.interval.col.R — entire ~20-case input-validation block (name/FUN/pretty_name/datatype/desc/depends/formalsmap) deleted
  • test-PKNCA.options.R — ~18 scalar/factor/NA/"must be a number" checks across nine options, plus PKNCA.set.summary point/spread/description checks
  • test-superpostion.Radditional.times non-numeric/negative/>tau and steady.state.tol checks
  • test-set_and_assert_intervals.R — 3 whole test_that blocks
  • Same pattern in test-time.above.R, test-pk.calc.c0.R, test-class-PKNCAconc.R, test-class-PKNCAdata.R, test-class-PKNCAdose.R, test-exclude.R, test-assertions.R, test-time.to.steady.state.R

The new behavior is now less tested than the old. Please convert these to expect_error(..., class="pknca_error_...") instead of deleting them.

checkmate semantics quietly differ from the manual checks they replaced

Please add tests for where these tests are stricter:

CI can't catch these because the negative tests (finding 2) were deleted. All verified against base:

  • finite=TRUE added where the old code accepted Inf: pk.calc.aucabove now uses assert_number(conc_above, na.ok=TRUE, finite=TRUE); old stopifnot(is.numeric, length==1) allowed Inf. conc_above=Inf now errors.
  • all-NA acceptance: assert_character/assert_numeric accept an all-NA logical vector by default, where is.character()/is.numeric() rejected it. In R/exclude.R the old else if (!is.character(reason)) became an unconditional assert_character(reason), so reason=NA now passes the type gate. (add.interval.col intentionally relies on this for FUN=NA, but the relaxation is silent elsewhere.)
  • Newly stricter, not in the original: add.interval.col adds min.chars=1 to name (rejects "") and unique=TRUE on formalsmap names; R/impute.R assert_scalar(impute) rejects a length-1 list that length(impute)==1 accepted.

None look dangerous, but they're undocumented behavior changes with no test pinning them.

Please find remaining stop and warning calls

"Replaced all stop() and warning() calls" — but genuine sites remain at the PR head:

  • stop() in R/auc_integrate.R (the idx_tlast "must occur exactly once" check — not nocov), R/prepare_data.R:165, R/time.above.R:86
  • warning() in R/pk.calc.all.R:143 (re-raises a captured condition)

.Rbuildignore references a file not in the repo

The PR adds ^coverage_check\.R$ and strips the trailing newline, but coverage_check.R is not part of the PR or repo — a leftover local-scratch reference. Please drop the line and restore the trailing newline.

Whitespace / formatting issues

Pervasive enough to be a class:

  • ) } — closing brace fused onto an abort()/warn() close paren: ~11 sites (e.g. R/auc.R, R/class-PKNCAdata.R, R/superposition.R, R/tss.monoexponential.R, R/exclude.R, R/class-general.R, R/001-add.interval.col.R)
  • Zero/under-indentation: R/pk.calc.c0.R (assert_number + if at column 0), R/PKNCA.options.R (if (default && check)), R/class-summary_PKNCAresults.R (} else if)
  • Comments displaced onto code lines: R/PKNCA.options.R (assert_number(...) # Must be between 0 and 1), R/aucint.R (} else if (...) { # If using clast.pred…)
  • Inconsistent ){ (no space), stray trailing whitespace, and two new "No newline at end of file" (R/pk.calc.simple.R, .Rbuildignore)

A styler/lintr pass clears all of these.

Commented-out dead code left behind (8 sites)

Beyond the #stop(unit_col, …) already noted: #c("interval", …) (R/001-add.interval.col.R), #checkmate::assert_number(… max.missing) (R/PKNCA.options.R), #checkmate::assertNumeric(x$start …) (R/check.intervals.R), #stopifnot(inherits(… o_conc/o_dose)) ×2 and # stopifnot(…cols…) ×2 (R/prepare_data.R), and the trailing assert_number(tlast)#, finite = TRUE) (R/auc_integrate.R).

PavanLomati and others added 6 commits July 27, 2026 15:42
…ng-refactor

 Conflicts:
	NEWS.md
	R/001-add.interval.col.R
	R/aucint.R
	R/pk.calc.all.R
	R/pk.calc.urine.R
	tests/testthat/test-001-add.interval.col.R
NEWS.md:
- Restore the deleted changelog entry for the unsorted concentration-time
  fix, which a merge dropped while leaving its code and test in place.
- Split two bullets that a merge glued together, and put the "Bug Fixes"
  heading back on its own line so the section renders.
- Remove the pk_nca_result_to_df() entry: that regression was introduced
  and fixed within this branch, so no released version exhibited it.
- Document the pknca_* condition class renames under Breaking changes,
  including the two sparse classes merged into one.

Revert the new S3method(AIC,list) registration. AIC.list() keeps @nord,
so AIC() dispatch on lists is unchanged from main.

Restore the base formatting of aucint.R, pk.calc.c0.R, time.above.R, and
AIC.list.R, keeping only the condition-signaling changes. These four were
reindented wholesale, which inflated the diff without changing behavior:
aucint.R alone was 991 changed lines carrying 22 real ones. The branch
diff drops from 4760 changed lines to 3184; the test suite is unchanged
at 2745 passing.

Co-Authored-By: Claude Opus 5 <[email protected]>
Expanding one-line stop() calls into multi-line rlang::abort() calls left
`# nocov` on only some lines of each call, so the unmarked middle lines
still counted toward coverage. Per the contributing guide, regions of
three or more lines are bracketed with `# nocov start` / `# nocov end`;
statements of one or two lines keep a trailing marker on each line.

35 sites across 14 files. Every converted region contains exactly one
rlang::abort() or rlang::warn() call and nothing else.

Also bracket the closure in zero_len_summary(), where a pre-existing pair
of bare `#nocov` markers on the first and last line left the nine-line
body counted.

Co-Authored-By: Claude Opus 5 <[email protected]>
…isc_arg

The class names were built with sprintf() from the argument name, so the
six resulting classes could not be grepped for and only the three
pptestcd_cdisc ones were ever evaluated -- a typo in the pptest_cdisc
trio would have gone unnoticed. Three literal classes cover both
arguments and are exercised by the existing tests.

Replace the hand-rolled scalar and name checks with checkmate::test_string()
and checkmate::test_names(), keeping rlang::abort() so each failure retains
its class. Drop the redundant length(x) != 1 test, which
identical(names(x), "route") already implies.

Document the validation itself in NEWS: it is stricter than the previous
character-or-list check and can reject input that add.interval.col()
used to accept.

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread R/001-add.interval.col.R Outdated
Comment thread R/assertions.R Outdated
Comment thread R/assertions.R Outdated
Comment thread R/assertions.R Outdated
Comment thread R/auc.R Outdated
Comment thread R/auc.R
Comment thread R/auc_integrate.R Outdated
Comment thread R/auc_integrate.R
Comment thread R/auc_integrate.R Outdated
Condition signaling:
- Drop the named `message =` argument from every rlang::abort/warn/inform
  call (301 sites); it is distinctly the first argument.
- Condense 108 short calls onto one line so `# nocov` can be a trailing
  marker rather than a start/end bracket.
- Give every condition class a distinct value (17 sites across 10 reused
  names), so callers such as ANCA or ruminate can capture a specific
  failure rather than a shared one.
- Replace paste()/paste0() message assembly with sprintf templates (20
  sites) so the message a user reports can be grepped for in the source,
  and hoist the branching message in choose_interval_method() out of the
  abort call.

checkmate:
- assert_unit_col() uses assert_names(must.include=) and
  assert_character() in place of two hand-rolled classed aborts.
- add.interval.col() uses assert_list(names = "unique"); the empty-list
  conditional was dead because checkmate accepts a zero-length list.
- choose_interval_method() folds the conc/time length check into the
  time assertion via len = length(conc).
- pk_nca_result_to_df() reads warning_prep$message directly rather than
  through conditionMessage().

Formatting, limited to lines this PR introduced:
- Restore `) {`, drop trailing whitespace, and put closing braces on
  their own line (80 lines). Lines unchanged from the base are untouched.
- Remove the leftover `info` arguments from test-001-add.interval.col.R.

Tests follow the renamed classes: the Tobit half-life cases now pin
pknca_warning_halflife_too_few_points_tobit, and the assert_unit_col
cases pin checkmate's messages.

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread R/assertions.R Outdated
Comment thread R/auc_integrate.R Outdated
Comment thread R/auc_integrate.R Outdated
Comment thread R/aucint.R Outdated
Comment thread R/aucint.R Outdated
Comment thread R/check.intervals.R Outdated
Comment thread R/check.intervals.R Outdated
Comment thread R/class-general.R Outdated
Comment thread R/class-general.R Outdated
Comment thread R/class-general.R Outdated
Extends the earlier pass, which only recognized a call whose message was a
string literal.  A message that is a variable or a nested sprintf() is now
flattened too, which is what left calls such as
rlang::abort(unit_col, class = ...) spread over four lines.

A call inside a `# nocov start` / `# nocov end` bracket is allowed a longer
line, since collapsing it also removes the two marker lines and leaves a
trailing `# nocov`; the limit is 175 characters there and 120 elsewhere.
44 calls collapsed and nocov brackets drop from 42 to 12.

Whitespace is only collapsed outside string literals, so no message text
changes.  The 12 remaining brackets are not single calls (.onLoad, the
lifecycle shims, a closure), wrap two statements, or would exceed 180
characters on one line.

Co-Authored-By: Claude Opus 5 <[email protected]>
@billdenney
billdenney self-requested a review August 17, 2026 21:22
@billdenney
billdenney merged commit 6ae72b1 into humanpred:main Aug 17, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants