From ba8ff58e5a5e9bb6a38b76b1fda6371e594c8137 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 13 May 2025 16:50:45 -0400 Subject: [PATCH] fix `hasmethod` with kwargs to exclude positional arg names --- base/reflection.jl | 2 +- test/reflection.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/reflection.jl b/base/reflection.jl index 3b0d522bfd0c2..2e976add50190 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -1035,11 +1035,11 @@ function hasmethod(f, t, kwnames::Tuple{Vararg{Symbol}}; world::UInt=get_world_c match = ccall(:jl_gf_invoke_lookup, Any, (Any, Any, UInt), tt, nothing, world) match === nothing && return false kws = ccall(:jl_uncompress_argnames, Array{Symbol,1}, (Any,), (match::Method).slot_syms) + kws = kws[((match::Method).nargs + 1):end] # remove positional arguments isempty(kws) && return true # some kwfuncs simply forward everything directly for kw in kws endswith(String(kw), "...") && return true end - kwnames = collect(kwnames) return issubset(kwnames, kws) end diff --git a/test/reflection.jl b/test/reflection.jl index 25b61945aefac..6da64ae7d7031 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -936,6 +936,7 @@ f(x::Int; y=3) = x + y @test hasmethod(f, Tuple{Int}) @test hasmethod(f, Tuple{Int}, ()) @test hasmethod(f, Tuple{Int}, (:y,)) +@test !hasmethod(f, Tuple{Int}, (:x,)) @test !hasmethod(f, Tuple{Int}, (:jeff,)) @test !hasmethod(f, Tuple{Int}, (:y,), world=typemin(UInt)) g(; b, c, a) = a + b + c