Code introspection

Hi all,

I’m working on a Julia terminal application implementing some of python’s awesome rich terminal library.

Two of rich’s best features, in my opinion, are inspect which prints out details and methods about a python object and tracebacks which styles and organize error messages making them much easier to read.

I’m trying to implement these in Term.jl but struggling with find nice ways to introspect Julia code. I’ve looked at:

but still struggle to get a few bits of information.
For instance given:

abstract type AA end

abstract type AbstractTest <: AA end

"""
    Test

A test `struct` for inspection
"""
mutable struct Test <: AbstractTest
    x::Int
    y::String
end


Test(x) = Test(x[1], x[2])

I’m writing a function inspect that spits out formatted information about Test: inspect(Test)

using things like methods, supertypes, fieldnames
to get the docstring I’ve tried string(@doc my_type) within my inspect function, but that doesn’t work. If outside my inspect function I call @doc Test I can retrieve the docstring correctly, but when passing Test as an argument to inspect then @doc fails.
I’ve seen that Docs.jl has methods like metadata, but I can’t find any good docs. Anyone has any idea on how to do this better?


Related, is there any docs on how the tracebacks are created in Julia and if there are methods to access them? In python you can have code that replaces the default tracebacks with ones you like, is there a way to do something similar in Julia?

6 Likes

To get the doc string, you can use Docs.doc. E.g.

julia> foo(x) = Docs.doc(x);

julia> foo(Complex)
  Complex{T<:Real} <: Number

  Complex number type with real and imaginary part of type T.

  ComplexF16, ComplexF32 and ComplexF64 are aliases for Complex{Float16}, Complex{Float32} and Complex{Float64} respectively.

I don’t know much about manipulating stacktraces, but I assume this is the place to start: Stack Traces · The Julia Language

That’s very useful! Thank you!

Just wanted to say, this looks useful already, thank you for working in this. More informative tracebacks would be amazing too, I’ve been looking for this for Julia for a while. Good luck!

Edit: you may have already had a look, but I think breakpoints in Debugger.jl do something similar to what you want. You may be able to take some inspiration from that.

Yeah that looks like it should give me an idea of how to do it, thank you!

I’m happy you’re finding it useful, keep an eye on the project development on GitHub and twitter, a first usable version will deploy soon including inspect and trace back functionality + loads of terminal goodies.

https://github.com/FedeClaudi/Term.jl
@Federico_claudi on twitter