Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property getter return values are not validated #1834

Open
mdickinson opened this issue Feb 4, 2025 · 0 comments
Open

Property getter return values are not validated #1834

mdickinson opened this issue Feb 4, 2025 · 0 comments

Comments

@mdickinson
Copy link
Member

mdickinson commented Feb 4, 2025

Given the following code, one might expect the getter _get_bar to cause a TraitError, since the returned value (a string) is incompatible with the type declared in the Property (Int()). However, no TraitError is issued (or at least, not visibly).

The docs suggest that the intent is to validate:

you can define a Property trait by simply passing another trait. For example:

source = Property( Code )
This line defines a trait whose value is validated by the Code trait, and whose getter and setter methods are defined elsewhere on the same class.

I doubt that we could change the behaviour now without breaking lots of code, but we could at least update the documentation to be accurate.

Example code:

from traits.api import HasStrictTraits, Int, Property


class A(HasStrictTraits):
    foo = Int()

    bar = Property(Int(), observe="foo")

    def _get_bar(self):
        return "definitely not an integer"


a = A(foo=43)
print(a.bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant