# Code introspection

**URL:** <https://discourse.julialang.org/t/code-introspection/76734>\
**Category:** General Usage\
**Created:** [February 19, 2022, 10:16am UTC](https://discourse.julialang.org/t/code-introspection/76734 "2022-02-19T10:16:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)\
**Post date:** [February 19, 2022, 10:16am UTC](https://discourse.julialang.org/t/code-introspection/76734/1 "2022-02-19T10:16:59Z")

</div>

Hi all,

I’m working on a [Julia terminal application](https://twitter.com/Federico_claudi/status/1494456702292348928?s=20&t=aDH53AQJsz2p0Uon_P8RuA) implementing some of python’s awesome [rich](https://github.com/Textualize/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](https://github.com/FedeClaudi/Term.jl) but struggling with find nice ways to introspect Julia code. I’ve looked at:

- [https://towardsdatascience.com/runtime-introspection-julias-most-powerful-best-kept-secret-bf845e282367](https://towardsdatascience.com/runtime-introspection-julias-most-powerful-best-kept-secret-bf845e282367)
- [Reflection and introspection · The Julia Language](https://docs.julialang.org/en/v1/devdocs/reflection/)
- [Documentation · The Julia Language](https://docs.julialang.org/en/v1/manual/documentation/#Core.@ __doc__ )
- [https://github.com/JuliaLang/julia/blob/bf534986350a991e4a1b29126de0342ffd76205e/base/docs/Docs.jl#L440-L455](https://github.com/JuliaLang/julia/blob/bf534986350a991e4a1b29126de0342ffd76205e/base/docs/Docs.jl#L440-L455)

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

```julia
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)`

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a890264a6dd6b86a30f6bc60ac057c7bc6a69b61.png)

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`](https://github.com/JuliaLang/julia/blob/bf534986350a991e4a1b29126de0342ffd76205e/base/docs/Docs.jl#L334), 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?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 21, 2022, 10:02am UTC](https://discourse.julialang.org/t/code-introspection/76734/2 "2022-02-21T10:02:18Z")

</div>

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

```julia
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](https://docs.julialang.org/en/v1/manual/stacktraces/)

---

<div class="post-metadata">

**Author:** ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)\
**Post date:** [February 21, 2022, 4:20pm UTC](https://discourse.julialang.org/t/code-introspection/76734/3 "2022-02-21T16:20:24Z")

</div>

That’s very useful! Thank you!

---

<div class="post-metadata">

**Author:** ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)\
**Post date:** [February 21, 2022, 7:01pm UTC](https://discourse.julialang.org/t/code-introspection/76734/4 "2022-02-21T19:01:44Z")

</div>

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](https://github.com/JuliaDebug/Debugger.jl#breakpoints) do something similar to what you want. You may be able to take some inspiration from that.

---

<div class="post-metadata">

**Author:** ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)\
**Post date:** [February 22, 2022, 9:29am UTC](https://discourse.julialang.org/t/code-introspection/76734/5 "2022-02-22T09:29:04Z")

</div>

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](https://github.com/FedeClaudi/Term.jl)  
@Federico\_claudi on twitter
