-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
allow passing a module to methods #33403
Conversation
Bump! I need this all the time (I come back to this PR today because I wrote in the OP the incantation to a work-around). |
If there's no review of this in the next couple of days, I'm just going to merge it. |
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.
Yep, Jeff and I wanted this too recently. Seems like we can always change it later if we want it to behave differently (e.g. looking into submodules, etc.)
Cool, I will add the tests and news. Actually, are you fine that instead of only |
Perhaps it should be default |
base/reflection.jl
Outdated
if isa(f, Core.Builtin) | ||
throw(ArgumentError("argument is not a generic function")) | ||
end | ||
t = to_tuple_type(t) | ||
world = typemax(UInt) | ||
return MethodList(Method[m[3] for m in _methods(f, t, -1, world)], typeof(f).name.mt) | ||
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module == mod], |
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.
Could arguably be parentmodule(m[3].module)
.
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.
Why? If I want methods defined in a submodule S
of P
, then methods(f, S)
will give nothing and methods(f, P)
will give all methods of f
whether they are in S
or P
...
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.
Yeah, I think I meant m[3].module == mod || parentmodule(m[3].module) == mod
.
Basically, if you are asking for the methods in Pkg
you probably want the methods in Pkg.API
as well? Anyway, doesn't have to be done now.
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.
Yes a "recursive" option would be useful, maybe as a keyword.
Yes I had something like that in mind. I will add a commit. |
1ece4b7
to
d2c97d6
Compare
Is there a bug with CI not being triggered? No indication of CI status here, and I see the same happening in other PRs too... |
The status doesn't show up until the build starts, so if there's a backlog of builds, they may not show up for a bit. Looks like many of the checks have run now, but there are still more to go. |
What about allowing passing a collection of modules and then having a function for iterating a module and all of its children? Then this would be done as |
Yes that seems like a good idea, more orthogonal. |
Bump: would be great to have that in version 1.4! |
Co-Authored-By: Alex Arslan <ararslan@comcast.net>
d2c97d6
to
69d9c04
Compare
base/reflection.jl
Outdated
""" | ||
function methods(@nospecialize(f), @nospecialize(t)) | ||
function methods(@nospecialize(f), @nospecialize(t), | ||
@nospecialize(mod::Union{Module,AbstractArray{Module},Tuple{Vararg{Module}}}=())) |
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.
I realize that by passing ()
explicitly, like in methods(f, (Int,), ())
, the user would expect an empty result, whereas here we consider it means no filtering on the module... should we change the default to nothing
? This use-case is very contrived, but it might happen programmatically... Hm, I think I will use nothing
, it's more correct.
Bump :) |
* allow passing a module to methods * add missing `=nothing` (ararslan) Co-Authored-By: Alex Arslan <ararslan@comcast.net> * allow specifying multiple modules * add NEWS and compat annotations * use `nothing` instead of `()` as a default value
I often wish to see methods implemented by a specific module. Of course I can do
collect(Iterators.filter(x -> x.module == MyModule, methods(eltype)))
(collect
is quite necessary as otherwise all the methods are printed, because the iterator itself is printed).But it's cumbersome and I'm not even sure
method.module
is documented.This PR implements the API
methods(f, [types], [module::Module])
, a possible added convenience could be to also allow passing a list of modules.