traits icon indicating copy to clipboard operation
traits copied to clipboard

Dynamic Range does not call _default method

Open aaronayres35 opened this issue 2 years ago • 1 comments

Running

from traits.api import HasTraits, Int, Range


class Foo(HasTraits):

    bar = Int(10)

    baz = Range(low=0, high=10)

    def _baz_default(self):
        print('_baz_default')
        return 5


if __name__ == '__main__':
    foo = Foo()
    print(foo.baz)

yields

>>> _baz_default
>>> 5

but changing the Range trait definition to baz = Range(low=0, high="bar")

the output is

>>> 0

aaronayres35 avatar Nov 18 '21 19:11 aaronayres35

looks like this is somewhat more of the same of #395 in the sense that if one of low/high/value is dynamic we assume all should be. ref: https://github.com/enthought/traits/issues/395#issuecomment-364214234

When I do:

from traits.api import HasTraits, Int, Range


class Foo(HasTraits):

    bar = Int(10)

    other = Int()

    baz = Range(low=0, high="bar", value="other")

    def _other_default(self):
        print('_other_default')
        return 5


if __name__ == '__main__':
    foo = Foo()
    print(foo.baz)

I get what I want.

Relevant code it here: https://github.com/enthought/traits/blob/e13dc8d7f0648f0ebd87e269420be1fa7386dadf/traits/trait_types.py#L1794-L1799 which overrides the "standard" behavior defined here: https://github.com/enthought/traits/blob/e13dc8d7f0648f0ebd87e269420be1fa7386dadf/traits/has_traits.py#L721

all or nothing vtype: https://github.com/enthought/traits/blob/e13dc8d7f0648f0ebd87e269420be1fa7386dadf/traits/trait_types.py#L1740-L1744 rather than considering low/high/value independently

aaronayres35 avatar Nov 18 '21 19:11 aaronayres35