-
Notifications
You must be signed in to change notification settings - Fork 121
Prevent infinite recursion of unit by adding a method unit(::Type{Any}) #807
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
Prevent infinite recursion of unit by adding a method unit(::Type{Any}) #807
Conversation
…it(::Type{Union{Missing,T}}) This is to resolve errors with uncluding units into FiniteDifferences.jl. See JuliaDiff/FiniteDifferences.jl#244 for details. Tests for Units.jl need to be updated, if this is to be accepted. The totalness of unit introduced via unit(::Ay) might be a bit controversial, but I don't think that should be a problem. In general, things don't have units. Only Numbers do. Closes JuliaPhysics#806.
Looks like there are not too many tests failing because if this change, but there are apparently things like dates that should be throwing because of some reason that I still need to decipher. Any comments on this would be appreciated. |
Ah, right, the methods were intentionally missing for date-type units where a duration cannot be fixed sensibly. I'll need to modify this a bit to keep those semantics intact. |
…e types with ambiguous units For example, Years are not always the same length, so they do not have unambiguous units. Many abstract Date types also do not have units.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #807 +/- ##
=========================================
Coverage ? 89.46%
=========================================
Files ? 21
Lines ? 1699
Branches ? 0
=========================================
Hits ? 1520
Misses ? 179
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restricting T
in the method unit(::Type{Union{Missing, T}})
is the correct thing to do to fix the stack overflow. Adding unit(::Any)
and unit(::Function)
is not.
As suggested by @sostock, do not only restrict the type of T to Number, but include FixedPeriod as well, to accommodate Dates-soecific code. Co-authored-by: Sebastian Stock <[email protected]>
…:Type{Union{Missing, T}}) and remove unwanted unit methods Now the tests run again. Still need to revert the tests that were converted from throwing MethodError to outputting NoUnits.
8712763
to
c230a6a
Compare
…ng against NoUnits This was to accommodate the removed unwanted methods unit(::Any) and unit(::Function).
I changed the title of the pull request to reflect its scope. I will look into why FiniteDifferences.jl does what it does more deeply. At the very least I am currently trying to take the unit of a function being differentiated in JuliaDiff/FiniteDifferences.jl#244. A quick look indicates that |
And |
After thinking some more about this, I’m not sure this is necessary. The stack overflow only happens when someone calls Before this PR, a user could define |
The different kind of error might be a good thing, though. It could function as an explicit way of telling a user that they misimplemented something, which is what I did and spent some time tracking down what and why, especially since the purpose of each method is not documented. The method function unit(x::Any)
NotImplementedError("You tried calling unit on an object $x, that has none defined. Not everything has (or should have) units.")
end User experience, and all that. StackOverflowErrors are never very nice. |
Then the type restriction could be lifted to allow defining |
This allows us to remove the type restriction on T for unit(::Type{Union{Missing,T}}), making it possible for users to define unit on their own types, while at the same time giving a more descriptive error message than StackOverflowError.
…e{Union{Missing,T}}) Not needed after re-removal of type constraint. Co-authored-by: Sebastian Stock <[email protected]>
…urposes Also changed the exception type thrown by unit(::Type{Any}) to ArgumentError, and changed the error description to be more helpful.
…moval of unit(::Any)
3d2e362
to
108bc4e
Compare
Had to force push to amend a commit message. But maybe this is close to completion now. |
Co-authored-by: Sebastian Stock <[email protected]>
The docstring is not needed, as this is not a feature that users need to know about in the documentation.
…ssage Line break escaping not supported in multi-line strings in older Julia versions. Co-authored-by: Sebastian Stock <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just two small comments to make the code style consistent with the rest of the package. Then this is ready to merge.
No empty lines between comments and code, and no spaces after opening parentheses of function invocations. Co-authored-by: Sebastian Stock <[email protected]>
Remove space between closing parentheses of nested function invocations Co-authored-by: Sebastian Stock <[email protected]>
Thanks! |
This is to resolve errors with including units into FiniteDifferences.jl. See JuliaDiff/FiniteDifferences.jl#244 for details.
Tests for Units.jl need to be updated (the
@test_throws
ones), if this is to be accepted. The totalness ofunit
introduced viaunit(::Any)
might be a bit controversial, but I don't think that should be a problem. In general, things don't have units. Only (some) Numbers do.Closes #806.