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

Make TraitListObject use the CTrait to validate items, not the handler #1625

Merged
merged 4 commits into from
Apr 20, 2022

Conversation

mdickinson
Copy link
Member

This PR fixes TraitListObject, TraitSetObject and TraitDictObject to perform item / key / value validation using the validate method of the appropriate CTrait instance instead of the validate method of the handler. Using the handler's validation is not the right thing to do, because the CTrait validation may or may not delegate to the handler's validate method : in particular, in the case where there's a fast validator, CTrait validation uses that fast validator, and so should the list / set / dict validation.

This fixes one of the issues identified in #1619. (In particular, it fixes the particular example that @mdsmarte gives in that issue.)

Checklist

  • Tests
  • Update API reference (docs/source/traits_api_reference) N/A
  • Update User manual (docs/source/traits_user_manual) N/A
  • Update type annotation hints in traits-stubs N/A

@mdickinson
Copy link
Member Author

@mdsmarte Do you have bandwidth for a review?

@@ -434,6 +446,46 @@ def test_trait_dict_object_pickle(self):
tdo_unpickled.value_validator(1)
tdo_unpickled.value_validator(True)

def test_disconnected_dict(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a drive-by cleanup unrelated to the PR: the recent PR #1624 added this test to the wrong test class. so I took the liberty of moving it to the right place in this PR.

@mdsmarte
Copy link
Contributor

@mdsmarte Do you have bandwidth for a review?

If you don't mind a short wait, I can take a look mid next week.

@mdickinson mdickinson changed the title Fix trait list object validation Make TraitListObject use the CTrait to validate items, not the handler Apr 4, 2022
Copy link
Contributor

@mdsmarte mdsmarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior reported in #1619 is now consistent, with both cases now raising an error:

from traits.api import *


class Test(HasTraits):

    foo = Tuple(Int(), Int())
    bar = List(Tuple(Int(), Int()))


Test(bar=[[1, 2], [3, 4]])  # No error on main, error on this branch
Test(foo=[1, 2])  # Error on both main and this branch

However, for the nested case, this is a backward incompatible change... I would hope not a lot of code relies on this, but should we issue a deprecation warning instead of an error?

@mdickinson
Copy link
Member Author

I would hope not a lot of code relies on this, but should we issue a deprecation warning instead of an error?

I'm not really seeing any reasonable way to do that that doesn't also involve a lot of false positives - there's no way for the general machinery to determine when the switch from handler validation to trait validation might make a difference, for example. I think the best we can do is (a) not make this change in a bugfix release (even though it's really a bugfix), and (b) document it clearly in the release notes. The next release of Traits should be a major release anyway, so it's not unreasonable to be making some potentially backwards-incompatible changes (not that we're going to use that as an excuse for gratuitous breakage, of course).

@mdickinson
Copy link
Member Author

I've added an entry for this in what will become the changelog for the 7.0 release.

Copy link
Contributor

@mdsmarte mdsmarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mdickinson mdickinson merged commit 141e65f into main Apr 20, 2022
@mdickinson mdickinson deleted the fix-trait-list-object-validation branch April 20, 2022 07:31
mdickinson added a commit that referenced this pull request Aug 9, 2022
#1625)

* Use the CTrait's validation instead of the handler validation in TraitListObject and friends

* Add regression tests

* Add changelog note highlighting the possibility of backwards incompatibility with Tuple

(cherry picked from commit 141e65f)
@mdickinson
Copy link
Member Author

Update: I'm planning a Traits 6.4.0 release; this change will go into that release, rather than straight into the 7.0 release as originally planned. I'll make sure that the release notes contain a prominent note about the potential backward incompatibility.

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

Successfully merging this pull request may close these issues.

2 participants