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

Piracy fix for kwcall #171

Merged
merged 2 commits into from
Sep 5, 2023
Merged

Conversation

lgoettgens
Copy link
Collaborator

The implementation of #131 does not work properly if the function in question has a kwarg (see the change in the test file here for an example).
This fixes that by ignoring the first two elements of the signature of a kwcall (first is typeof(kwcall) and second is Any, the NamedTuple of all kwargs to be applied), and then considering the third to be "the original function" and everything behind that as positional arguments.

@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Merging #171 (9ac8a95) into master (8dc359d) will increase coverage by 0.03%.
The diff coverage is 83.33%.

@@            Coverage Diff             @@
##           master     #171      +/-   ##
==========================================
+ Coverage   77.58%   77.61%   +0.03%     
==========================================
  Files          11       11              
  Lines         736      746      +10     
==========================================
+ Hits          571      579       +8     
- Misses        165      167       +2     
Flag Coverage Δ
unittests 77.61% <83.33%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/utils.jl 92.42% <71.42%> (-2.50%) ⬇️
src/piracy.jl 94.04% <100.00%> (+0.22%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

Unfortunately it fails in all older Julia versions (<=1.8)

@lgoettgens
Copy link
Collaborator Author

Unfortunately it fails in all older Julia versions (<=1.8)

Works now in julia 1.5. Let's see if CI succeeds for older version as well. It needed way much more of wiggling around with internals than I expected or wanted.

@@ -157,3 +157,22 @@ function format_diff(
$text_b
"""
end

function is_kwcall(signature::DataType)
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps it would make sense to use https://github.com/FluxML/MacroTools.jl or https://github.com/invenia/ExprTools.jl instead of rolling this on our own?

julia> ex = :(
                  function Base.f(x::T, y::T; z::Bool=false) where T
                      x + y
                  end
              );

julia> ExprTools.splitdef(ex)
Dict{Symbol, Any} with 6 entries:
  :args        => Any[:(x::T), :(y::T)]
  :body        => quote
  :kwargs      => Any[:($(Expr(:kw, :(z::Bool), false)))]
  :head        => :function
  :whereparams => Any[:T]
  :name        => :(Base.f)

julia> MacroTools.splitdef(ex)
Dict{Symbol, Any} with 5 entries:
  :name        => :(Base.f)
  :args        => Any[:(x::T), :(y::T)]
  :kwargs      => Any[:($(Expr(:kw, :(z::Bool), false)))]
  :body        => quote
  :whereparams => (:T,)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, but of course that's for expressions, not methods/signatures, so it might not be helpful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, these ones unfortunately do not offer what we need here. I couldn't find any good way to test this in other packages. Most just ignore old julia versions, where Core.kwcall doesn't exist, or use some regex with the function name of the kwsorter. The first is not an option here (unless we drop support for julia-pre-1.9, or have different semantics for the tests depending on the julia version). The second option relies on weirder internals, where we won't get good error messages in future changes (as regexes and strings just tell "no", not fail with some type error etc.). Thus, I think the current proposal here is the best we can currently do. I already added a try-catch with some additional debug information for people to open issues here.

@fingolfin fingolfin merged commit 673780f into JuliaTesting:master Sep 5, 2023
18 checks passed
@lgoettgens lgoettgens deleted the lg/kwarg-piracy branch September 5, 2023 16:07
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