-
-
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
applicable
and hasmethod
is returning wrong information for the function round
in Julia master
#51258
Comments
Likely an unintended consequence of #50812, @LilithHafner |
Indeed! If I pass all the arguments, it provides the correct answer: julia> applicable(round, "test", RoundNearest)
false
julia> applicable(round, 1.0, RoundNearest)
true |
The issue is that there now truly is a method |
Yes, this is the line you mentioned: Line 463 in 7b9fdf8
Of course I can easily change this in PrettyTables.jl but wouldn't it be a really breaking change? |
I don't know. What exactly are you relying on here? Adding new methods to functions in Base is, as far as I recall, not a breaking change. That'd be incredibly limiting. |
When we are formatting cells in a Table, I do not know what is the cell type. We have a feature called if applicable(round, v)
return round(v, digits = digits[k])
else
return v
end But notice that it is only inside a pre-defined formatter in PrettyTables.jl. Maybe users of the package already defined formatters using the same "recipe" in other cases, which will badly break with 1.11.
I agree that adding methods to Base should not be considered breaking, but this is changing the behavior of a already defined method. |
Using |
Then I think the documentation must state this clearly: help?> applicable
search: applicable
applicable(f, args...) -> Bool
Determine whether the given generic function has a method applicable to the given arguments.
See also hasmethod.
help?> hasmethod
search: hasmethod
hasmethod(f, t::Type{<:Tuple}[, kwnames]; world=get_world_counter()) -> Bool
Determine whether the given generic function has a method matching the given Tuple of argument types with the upper bound of world age given by world.
If a tuple of keyword argument names kwnames is provided, this also checks whether the method of f matching t has the given keyword argument names. If the
matching method accepts a variable number of keyword arguments, e.g. with kwargs..., any names given in kwnames are considered valid. Otherwise the
provided names must be a subset of the method's keyword arguments.
See also applicable. For anyone not familiar with the internals, it seems very clear that if |
This commit adds a warning about the functions `applicable` and `hasmethod`. A user reading the current documentation can think that if those functions return `true`, we can call the method without errors. However, it is not always the case and should be clearly stated. Closes JuliaLang#51258
I think the real problem here is the implicit assumption that an applicable method executes without an error. The documentation is not lying, there is indeed an applicable method, but it simply results in an error. To make this confusion a bit more explicit consider this function:
Both EDIT: Ah I can see now that you've discussed in in the PR. |
Sounds like |
The problem is that people might overload |
Probably not the best idea, but you could use this: julia> function has_round(x)
isempty(Base.return_types(round, Tuple{typeof(x)})) && return false
try
round(x)
true
catch
false
end
end
has_round (generic function with 1 method)
julia> using Random, BenchmarkTools, PrettyTables
julia> @btime has_round(x) setup=(x=rand())
402.915 ns (8 allocations: 656 bytes)
true
julia> @btime has_round(x) setup=(x=randstring())
421.271 ns (5 allocations: 464 bytes)
false
julia> @btime has_round(x) setup=(x=rand([Base.Forward, Base.DEFAULT_STABLE, Base, open]))
441.919 ns (5 allocations: 464 bytes)
false
julia> @btime applicable(round, x, RoundNearest) setup=(x=randstring())
482.262 ns (0 allocations: 0 bytes)
false
julia> io = IOBuffer();
julia> table = randn(5,5);
julia> @btime pretty_table(io, table)
41.084 μs (1024 allocations: 58.46 KiB) But be aware that |
|
Hi!
I am seeing build errors in PrettyTables.jl using Julia master. After some analysis, it turns out that the functions
applicable
andhasmethod
is indicating that the functionround
can be used with arguments of typeString
:Julia nightly
Julia 1.9
The text was updated successfully, but these errors were encountered: