# How to write a unit test that checks whether a function is type stable

**URL:** <https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587>\
**Category:** General Usage\
**Tags:** question, testing, inference, type-stability, feature-request\
**Created:** [October 4, 2023, 8:39pm UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587 "2023-10-04T20:39:36Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [October 6, 2023, 10:19am UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587/21 "2023-10-06T10:19:51Z")

</div>

Amazing, something like `Core.Compiler.return_type` is what I actually needed.  
I was not familiar with this feature, @Mason thanks for mentioning it!  
Now I can do this:

```julia
@test !isa(return_type(type_unstable_function, Tuple{Int64, Int64}), Union)

```

Which should fail.  
And these should pass

```julia
@test !isa(return_type(type_unstable_function, Tuple{Float64, Float64}), Union)
@test !isa(return_type(type_unstable_function, Tuple{Float64, Int64}), Union)

```

and they do.

**EDIT**

This does not cover the case `return_type` returns `Any`.  
So some extension is needed for the above tests.  
I’ll manage and thanks again!

**EDIT 2**  
This would work for a function that can return a `Union` or an `Any`:

```julia
@test !any(x -> isa(return_type(type_unstable_function, Tuple{Int64, Int64}), x), (Union, Any))

```

and is extendible.

---

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [October 6, 2023, 10:26am UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587/22 "2023-10-06T10:26:36Z")

</div>

Also thanks for the warning.  
It is good to know.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 6, 2023, 11:02am UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587/23 "2023-10-06T11:02:47Z")

</div>

> [@CFBaptista](#):
>
> **EDIT**
> 
> This does not cover the case `return_type` returns `Any`.  
> So some extension is needed for the above tests.  
> I’ll manage and thanks again!
> 
> **EDIT 2**  
> This would work for a function that can return a `Union` or an `Any`:
> 
> ```julia
> @test !any(x -> isa(return_type(type_unstable_function, Tuple{Int64, Int64}), x), (Union, Any))
> 
> ```
> 
> and is extendible.

I would instead just write `isconcretetype(return_type(f, tt))`. Your check for `Union` and `Any` will miss any abstract type between your type and `Any`.

---

<div class="post-metadata">

**Author:** ![CFBaptista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cfbaptista/32/9264_2.png) [@CFBaptista](https://discourse.julialang.org/u/CFBaptista)\
**Post date:** [October 6, 2023, 11:07am UTC](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587/24 "2023-10-06T11:07:18Z")

</div>

You’re right, I would have to keep on adding more stuff into the tuple of what to check for.  
`isconcretetype` is excellent!

[Previous page](https://discourse.julialang.org/t/how-to-write-a-unit-test-that-checks-whether-a-function-is-type-stable/104587.md?page=1)
