pkl
pkl copied to clipboard
For class properties with type constraints, instances that violate those constraints should be included in the error report
Playground: https://pkl-playground.vercel.app/?share=strike-what-planet
(Please let me know if this stuff should be a discussion instead)
When instantiating a class e.g. thing = new Thing {}
, if at the time of evaluation a type constraint is violated, the line where the instantiation occured should be included in the error output.
The following:
class TypeConstrained {
name: String = "default"
aList: Listing<String>(length > 0)
}
validExample = new TypeConstrained {
aList {
"hello"
"world"
}
}
invalidExample = new TypeConstrained {}
Yields
pkl failed with error Error: Command failed: /var/task/node_modules/@pkl-community/pkl-linux-x64/bin/pkl eval - --no-project --format json --allowed-modules pkl:,repl: --allowed-modules package: --cache-dir /tmp/pkl
–– Pkl Error ––
Type constraint `length > 0` violated.
Value: new Listing {}
3 | aList: Listing<String>(length > 0)
^^^^^^^^^^
at text#TypeConstrained.aList (repl:text)
3 | aList: Listing<String>(length > 0)
^^^^^
at text#TypeConstrained.aList (repl:text)
106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.1/stdlib/base.pkl#L106)
and stderr:
–– Pkl Error ––
Type constraint `length > 0` violated.
Value: new Listing {}
3 | aList: Listing<String>(length > 0)
^^^^^^^^^^
at text#TypeConstrained.aList (repl:text)
3 | aList: Listing<String>(length > 0)
^^^^^
at text#TypeConstrained.aList (repl:text)
106 | text = renderer.renderDocument(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.1/stdlib/base.pkl#L106)
The error here correctly identifies which type constraint is violated, but not where that type constraint was violated. The value hint may be helpful, but at the top, there should probably be a file reference to the line invalidExample = new TypeConstrained {}
For this example specifically, I am not aware of a better way to ensure a Listing is actually defined for this key rather than just defaulting
This is definitely something that needs to be improved. Right now, we don't preserve enough information in runtime object to present errors like this, and we'd need to balance this with performance concerns when considering this feature.