Skip to content
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

Add update sha1.default() to work with any class #226

Merged
merged 13 commits into from
Jan 3, 2025

Conversation

billdenney
Copy link
Contributor

Fix #224

ChangeLog Outdated Show resolved Hide resolved
R/sha1.R Show resolved Hide resolved
ChangeLog Outdated Show resolved Hide resolved
@eddelbuettel
Copy link
Owner

Reverse-dependency checks came and went. (The machine I run these on is not often used for digest and specific per-package dependencies do not autoinstall so a fair number failed for 'dependency xyz missing' so I ran another baseline check with digest from the main branch. Came out the the same so "no change for worse".) While that is good I am still not a fan of the removal of the (arguably redundant) S3 accessors. How would you feel about reverting that part of the change?

@billdenney
Copy link
Contributor Author

I reverted those removals.

@eddelbuettel
Copy link
Owner

Appreciate the reversal. I keep pondering this, and the actual hard error in there with stop() was part of the original support, and I think it makes sense --- it indicates that the S3 support is complete, just as it does in #224. So a more minimal PR may be my current preference.

I updated my branch, borrowed you additional tests and am running it in reverse dependency checks now.

@billdenney
Copy link
Contributor Author

billdenney commented Jan 3, 2025

Is there a scenario where using sha1_attr_digest() for classes not specifically handled would be a problem? I ask because I discovered that <- and environment were missing when I tried to implement an sha1() method in the rxode2 library.

Since <- and environment are base R classes, it seemed better to put the methods here rather than into the rxode2 library. Enabling the sha1.default() function to handle any class by default via sha1_attr_digest() would prevent needing to update digest when new classes are necessary that don't fit within another library (as with <- and environment here).

Another example that would be fixed with a default method that provides a result is logLik (found just after I opened #224):

digest::sha1(logLik(lmx <- lm(x ~ 1, data = data.frame(x = 1:5))))
#> Error: sha1() has no method for the 'logLik' class

Created on 2025-01-03 with reprex v2.1.1

My concern is that this becomes whack-a-mole when going through all the required and recommended libraries that are unlikely to provide their own sha1() S3 methods.

@eddelbuettel
Copy link
Owner

I think I prefer to follow the explicit S3 dispatch where we already use it that is in sha1. As demonstrated earlier, if sha1(foo) bites for whatever foo you have digest(foo, algo="sha1") should always be available

> library(digest)
> digest(logLik(lmx <- lm(x ~ 1, data = data.frame(x = 1:5))), algo="sha1")
[1] "e2f78853387414a8b41d7793383ab062aa27241e"
> 

meaning that if you need it you could (if push came to shove) define a local sha1 override for it.

The package is 20+ years old, the S3-based sha1 support is nine years old. I think on balance I rather stay with the design that worked than to (more radically than we need to) alter it now. The aim is to have something reliable for all/most use cases.

@eddelbuettel
Copy link
Owner

eddelbuettel commented Jan 3, 2025

That said, whackamole is a risk, and logLik is a good example. There are a very large number of S3 classes out there, and we cannot possibly cover all. Then again digest() always works on "anything".

Maybe fusing our two competing PR and turning the stop() into a warning() (or message()) and proceeding is a good middle ground.

@eddelbuettel
Copy link
Owner

message() it is:

> library(digest)
> sha1(logLik(lmx <- lm(x ~ 1, data = data.frame(x = 1:5))))
sha1() has no method for the 'logLik' class, so using fallback.
[1] "7d7ba7a23dbe3ccc8342811bf6dce184501dc12a"
> suppressMessages(sha1(logLik(lmx <- lm(x ~ 1, data = data.frame(x = 1:5)))))
[1] "7d7ba7a23dbe3ccc8342811bf6dce184501dc12a"
> 

Committed in 7af4aa7. Thoughts?

…fix-224; Update man to include `sha1.<-()` method

# Conflicts:
#	R/sha1.R
#	inst/tinytest/test_sha1.R
@billdenney
Copy link
Contributor Author

I like that compromise. I've merged the branches.

expect_message(
sha1(logLik(lmx <- lm(x ~ 1, data = data.frame(x = 1:5)))),
pattern = "sha1\\(\\) has no method for the 'logLik' class, so using fallback\\."
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate hate hate hate this editor default of using four lines for what can be / should be two. Sadly I am in the minority on this.

No need to change though.

@@ -41,7 +41,7 @@ Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd
person("Michael", "Chirico", role="ctb", comment = c(ORCID = "0000-0003-0787-087X")),
person("Kevin", "Ushey", role="ctb", comment = c(ORCID = "0000-0003-2880-7407")),
person("Carl", "Pearson", role="ctb", comment = c(ORCID = "0000-0003-0701-7860")))
Version: 0.6.37.1
Version: 0.6.37.1.1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to try to keep these things out of PRs but I can clean this up post-merge.

No need to change.

Copy link
Owner

@eddelbuettel eddelbuettel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Some procrastination and noodling over alternatives made this better.

@eddelbuettel eddelbuettel merged commit 53d7d0b into eddelbuettel:master Jan 3, 2025
2 checks passed
@billdenney billdenney deleted the fix-224 branch January 3, 2025 16:59
eddelbuettel added a commit that referenced this pull request Jan 3, 2025
* Add update `sha1.default()` to work with any class and remove what are now duplicate classes

* Update docs

* Clarify new instructions for using `sha1()`

* Clarify description of sha1() changes

* Expanding sha1 support as discussed in #224

* Add more sha1 tests (borrowed from @billdenney)

* Add ChangeLog

* Revert removal of sha1() S3 methods

* Add fallback default methods (following discussion in #224)

* test sha1.default()

---------

Co-authored-by: Dirk Eddelbuettel <[email protected]>
@eddelbuettel eddelbuettel changed the title Add update sha1.default() to work with any class and remove what are now duplicate classes Add update sha1.default() to work with any class Jan 3, 2025
@eddelbuettel
Copy link
Owner

Should have altered the PR title (but then did so in an --amend commit). All good now.

Thanks for your attention to detail and patience on this.

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.

Feature request: sha1() methods for <- and environment objects
2 participants