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

Allow mutable values in Constant trait, add tests #929

Merged
merged 7 commits into from
Mar 17, 2020

Conversation

midhun-pm
Copy link
Contributor

Fixes #819

This PR removes a restriction on the Constant trait where the Constant trait could not be initialized with mutable types such as list and dict.

@mdickinson
Copy link
Member

@midhun-pm: We should have a documentation update as part of this PR, too. The minimum would be updating the docstring for the Constant trait to make it clear that this is permitted. But if there's a place in the user documentation where we can add this information, that would be even better.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

We should update the documentation as part of this PR.

@mdickinson
Copy link
Member

Actually, the current docstring for Constant is now incorrect, so we definitely need to fix that.

@midhun-pm
Copy link
Contributor Author

Updated the docstring but didn't find any place in the mentions anything about the value accepted by Constant trait.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

Looking good; agreed that there isn't anywhere obvious in the user docs that needs updating.

A few more suggested changes, and then I think this is good to merge.

class TestClass(HasTraits):
c_atr = Constant(5)

self.assertEqual(5, TestClass().c_atr)
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick, but let's switch the order of arguments on all these self.assertEqual calls: the usual order in Python-land is self.assertEqual(actual, expected) rather than self.assertEqual(expected, actual).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it was the other way around, because when I have :
self.assertEqual(4, TestClass().c_atr)
I get the following error message (in PyCharm).

FAILED (failures=1)


5 != 4

Expected :4
Actual   :5
<Click to see difference>

Copy link
Member

Choose a reason for hiding this comment

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

Hmm; interesting. It's not clear cut, and some languages make other decisions, but for Traits, let's be consistent with the existing code and with core Python style (and the examples in the unittest documentation) and use (actual, expected). It makes code harder to read if we use a mix of the two styles.

Copy link
Member

Choose a reason for hiding this comment

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

(If you want some fun historical reading, including Guido arguing with himself, see https://mail.python.org/pipermail/python-dev/2010-December/106875.html)

traits/trait_types.py Outdated Show resolved Hide resolved
traits/trait_types.py Show resolved Hide resolved
@mdickinson mdickinson assigned mdickinson and midhun-pm and unassigned mdickinson Mar 16, 2020
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM. I took the liberty of fixing my own nitpick about the assertEqual argument order.

Merging when CI approves.

@codecov-io
Copy link

codecov-io commented Mar 17, 2020

Codecov Report

Merging #929 into master will increase coverage by 0.21%.
The diff coverage is 68.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #929      +/-   ##
==========================================
+ Coverage   72.90%   73.12%   +0.21%     
==========================================
  Files          51       51              
  Lines        6456     6453       -3     
  Branches     1301     1300       -1     
==========================================
+ Hits         4707     4719      +12     
+ Misses       1357     1340      -17     
- Partials      392      394       +2     
Impacted Files Coverage Δ
traits/editor_factories.py 79.59% <0.00%> (-4.79%) ⬇️
traits/trait_base.py 62.96% <ø> (-0.46%) ⬇️
traits/trait_types.py 71.11% <ø> (-0.11%) ⬇️
traits/constants.py 100.00% <100.00%> (ø)
traits/trait_handlers.py 61.11% <100.00%> (+0.08%) ⬆️
traits/traits.py 76.92% <0.00%> (+4.04%) ⬆️
traits/etsconfig/etsconfig.py 63.46% <0.00%> (+6.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fbf08dc...0ba35e2. Read the comment docs.

@mdickinson mdickinson merged commit 1d76457 into master Mar 17, 2020
@mdickinson mdickinson deleted the enh/allow_mutable_values_in_constant_trait branch March 17, 2020 08:38
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.

Allow Constant trait to have mutable values or restrict more types
4 participants