Skip to content

Commit cb8e1cf

Browse files
rfourquetStefanKarpinski
authored andcommitted
allow passing a module to methods (#33403)
* allow passing a module to methods * add missing `=nothing` (ararslan) Co-Authored-By: Alex Arslan <[email protected]> * allow specifying multiple modules * add NEWS and compat annotations * use `nothing` instead of `()` as a default value
1 parent 48d1290 commit cb8e1cf

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Standard library changes
5353
* Sets are now displayed less compactly in the REPL, as a column of elements, like vectors
5454
and dictionaries ([#33300]).
5555

56+
* `methods` now accepts passing a module (or a list thereof) to filter methods defined in it ([#33403]).
57+
5658
#### Libdl
5759

5860
#### LinearAlgebra

base/reflection.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -862,19 +862,29 @@ function MethodList(mt::Core.MethodTable)
862862
end
863863

864864
"""
865-
methods(f, [types])
865+
methods(f, [types], [module])
866866
867-
Returns the method table for `f`.
867+
Return the method table for `f`.
868868
869-
If `types` is specified, returns an array of methods whose types match.
869+
If `types` is specified, return an array of methods whose types match.
870+
If `module` is specified, return an array of methods defined in this module.
871+
A list of modules can also be specified as an array or tuple.
872+
873+
!!! compat "Julia 1.4"
874+
At least Julia 1.4 is required for specifying a module.
870875
"""
871-
function methods(@nospecialize(f), @nospecialize(t))
876+
function methods(@nospecialize(f), @nospecialize(t),
877+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}},Nothing}=nothing))
878+
if mod isa Module
879+
mod = (mod,)
880+
end
872881
if isa(f, Core.Builtin)
873882
throw(ArgumentError("argument is not a generic function"))
874883
end
875884
t = to_tuple_type(t)
876885
world = typemax(UInt)
877-
return MethodList(Method[m[3] for m in _methods(f, t, -1, world)], typeof(f).name.mt)
886+
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module in mod],
887+
typeof(f).name.mt)
878888
end
879889

880890
methods(f::Core.Builtin) = MethodList(Method[], typeof(f).name.mt)
@@ -887,9 +897,11 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
887897
ms = ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), tt, -1, 1, world, min, max)::Array{Any,1}
888898
return MethodList(Method[m[3] for m in ms], typeof(f).name.mt)
889899
end
890-
function methods(@nospecialize(f))
900+
901+
function methods(@nospecialize(f),
902+
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}},Nothing}=nothing))
891903
# return all matches
892-
return methods(f, Tuple{Vararg{Any}})
904+
return methods(f, Tuple{Vararg{Any}}, mod)
893905
end
894906

895907
function visit(f, mt::Core.MethodTable)

test/reflection.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,30 @@ end
893893
@test nameof(:) === :Colon
894894
@test nameof(Core.Intrinsics.mul_int) === :mul_int
895895
@test nameof(Core.Intrinsics.arraylen) === :arraylen
896+
897+
module TestMod33403
898+
f(x) = 1
899+
f(x::Int) = 2
900+
901+
module Sub
902+
import ..TestMod33403: f
903+
f(x::Char) = 3
904+
end
905+
end
906+
907+
@testset "methods with module" begin
908+
using .TestMod33403: f
909+
@test length(methods(f)) == 3
910+
@test length(methods(f, (Int,))) == 1
911+
912+
@test length(methods(f, TestMod33403)) == 2
913+
@test length(methods(f, (TestMod33403,))) == 2
914+
@test length(methods(f, [TestMod33403])) == 2
915+
@test length(methods(f, (Int,), TestMod33403)) == 1
916+
@test length(methods(f, (Int,), (TestMod33403,))) == 1
917+
918+
@test length(methods(f, TestMod33403.Sub)) == 1
919+
@test length(methods(f, (TestMod33403.Sub,))) == 1
920+
@test length(methods(f, (Char,), TestMod33403.Sub)) == 1
921+
@test length(methods(f, (Int,), TestMod33403.Sub)) == 0
922+
end

0 commit comments

Comments
 (0)