Julia v1.7.3
I have encountered a problem with using Base.@kwdef
that only occurs in tests, not when code is executed normally. An example:
File src/Example.jl:
module Example
Base.@kwdef mutable struct Foo
bar :: Integer
end
Foo() = Foo(bar=1)
instance = Foo()
print("Instance value is $(instance.bar)")
end
Running this code with julia src/Example.jl
results in the expected output: Instance value is 1
Now let’s add a test covering the constructor that takes no argument:
In file test/runtests.jl
using Test
using Example
@testset "default_foo_constructor" begin
instance = Example.Foo()
@test typeof(instance) == Example.Foo
@test instance.bar == 1
end
Running the tests results in an error:
Testing Example
Status `userdir\AppData\Local\Temp\jl_ADLTZW\Project.toml`
[53506a2b] Example v0.1.0 `userdir\Projects\test-julia`
[8dfed614] Test `@stdlib/Test`
Status `userdir\AppData\Local\Temp\jl_ADLTZW\Manifest.toml`
[53506a2b] Example v0.1.0 `userdir\Projects\test-julia`
[2a0f44e3] Base64 `@stdlib/Base64`
[b77e0a4c] InteractiveUtils `@stdlib/InteractiveUtils`
[56ddb016] Logging `@stdlib/Logging`
[d6f4376e] Markdown `@stdlib/Markdown`
[9a3f8284] Random `@stdlib/Random`
[ea8e919c] SHA `@stdlib/SHA`
[9e88b42a] Serialization `@stdlib/Serialization`
[8dfed614] Test `@stdlib/Test`
Testing Running tests...
default_foo_constructor: Error During Test at projectdir\test\runtests.jl:4
Got exception outside of a @test
UndefKeywordError: keyword argument bar not assigned
Stacktrace:
[1] Example.Foo()
@ Example .\util.jl:478
[2] macro expansion
@ projectdir\test\runtests.jl:5 [inlined]
[3] macro expansion
@ userdir\AppData\Local\Programs\Julia-1.7.3\share\julia\stdlib\v1.7\Test\src\Test.jl:1283 [inlined]
[4] top-level scope
@ projectdir\test\runtests.jl:5
[5] include(fname::String)
@ Base.MainInclude .\client.jl:451
[6] top-level scope
@ none:6
[7] eval
@ .\boot.jl:373 [inlined]
[8] exec_options(opts::Base.JLOptions)
@ Base .\client.jl:268
[9] _start()
@ Base .\client.jl:495
Test Summary: | Error Total
default_foo_constructor | 1 1
ERROR: LoadError: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.
in expression starting at projectdir\test\runtests.jl:4
ERROR: Package Example errored during testing
This example is not exactly how we use Base.@kwdef
in our project, but I was able to condense the problem down to this example. I’m just a bit confused why this error occurs. Perhaps I’m misunderstanding how constructors in general and Base.@kwdef
in particular work, as I do find the concepts involved a bit confusing. We could also fairly easily not use the macro in the first place. It seems a useful tool for developers, but perhaps it would best not to mix keyword and positional constructors.