Remove the TraitListObject, TraitDictObject and TraitSetObject cases from default value inference - #1902
Remove the TraitListObject, TraitDictObject and TraitSetObject cases from default value inference#1902Kayvan-Zahiri wants to merge 2 commits into
Conversation
|
@Kayvan-Zahiri Thanks for the PR and the analysis. Some project context: I think there's another potential way to solve this issue: instead of adding extra conditions, we remove the three branches here. Those branches are the cause of the 5.2.0 -> 6.0 regression noted in the issue; they're not really useful, and they're the cause of this issue as well as potentially other ones. An example of a related bug that isn't touching the legacy from traits.api import HasTraits, List, TraitType
class Source(HasTraits):
values = List([1, 2])
class MyType(TraitType):
pass
class A(HasTraits):
value = MyType(Source().values)
A().valueThe above currently raises |
As suggested in review: instead of special-casing Trait(), remove the three branches in _infer_default_value_type that picked the trait_*_object default value types. Those types need a List, Dict or Set handler, so any other trait given one of these objects as its default failed on read. The objects now get the same default value types as plain lists, dicts and sets. Reverts the traits.py change from the previous commit and adds a test for a plain TraitType.
|
Thanks, that's a cleaner fix. I've pushed it: the three branches are gone, the |
Fixes #1591.
Following @mdickinson's review, this removes the three branches in
_infer_default_value_typethat mappedTraitListObject,TraitDictObjectandTraitSetObjectto thetrait_*_objectdefault value types. Those types only work with a List, Dict or Set handler, andList,DictandSetset their default value type explicitly, so the branches only ever applied to other traits, where reading the attribute failed. These objects now get what plain containers get from the same inference:list_copy,dict_copy, andconstantfor sets. The earlierTrait()-specific change is reverted.Behavior note: lists and dicts are now copied per instance. Sets are still shared, since there is no set-copy default value type.
Tests: the issue's
add_traitexample, list, dict and set defaults throughTrait(), and a plainTraitTypesubclass (the example from the review) all error onmainand pass here. Full suite: 1633 tests OK, flake8 clean.Written with AI assistance. The runs above are real.