Warn about uses of `Instance(SomeTraitType)`
- Dominant language
- Python
- Stars
- 462
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
A fairly common Traits mistake is passing a TraitType to `Instance`:
```python
class A(HasTraits):
shoe_size = Instance(Int)
button_callback = Instance(Function)
```
Needless to say, this doesn't work the way the user intended, and the error message isn't all that helpful unless you're looking _really_ closely:
```python
>>> from traits.api import Function, HasTraits, Instance, Int
>>> class A(HasTraits):
... shoe_size = Instance(Int)
... button_callback = Instance(Function)
...
>>> a = A()
>>> a.shoe_size
>>> a.shoe_size = 35
Traceback (most recent call last):
File "", line 1, in
File "/Users/mdickinson/.venvs/traits/lib/python3.8/site-packages/traits/base_trait_handler.py", line 74, in error
raise TraitError(
traits.trait_errors.TraitError: The 'shoe_size' trait of an A instance must be an Int or None, but a value of 35 was specified.
>>> a.button_callback = lambda x: x
Traceback (most recent call last):
File "", line 1, in
File "/Users/mdickinson/.venvs/traits/lib/python3.8/site-packages/traits/base_trait_handler.py", line 74, in error
raise TraitError(
traits.trait_errors.TraitError: The 'button_callback' trait of an A instance must be a Function or None, but a value of at 0x10fa8c430> was specified.
```
I can't think of any case where this would actually be what you want. We might consider warning about uses of this pattern, or, if we can't think of any valid uses, even making it an error.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.