For class properties with type constraints, instances that violate those constraints should be included in the error report
- Dominant language
- Java
- Stars
- 11.5k
- Forks
- 402
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 20
Description
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:
```pkl
class TypeConstrained {
name: String = "default"
aList: Listing(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(length > 0)
^^^^^^^^^^
at text#TypeConstrained.aList (repl:text)
3 | aList: Listing(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(length > 0)
^^^^^^^^^^
at text#TypeConstrained.aList (repl:text)
3 | aList: Listing(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
Contributor guide
Research direction
Start with the Pkl Playground example and compare the current error output for `invalidExample = new TypeConstrained {}` with the reported output. Trace how the type constraint and instantiation locations are represented, including the `pkl.base#Module.output.text` entry in `stdlib/base.pkl`; done means the error identifies the instantiation line as well as the violated constraint.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100