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

Add a TypedReadOnly trait #488

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add a TypedReadOnly trait #488

wants to merge 3 commits into from

Conversation

itziakos
Copy link
Member

@itziakos itziakos commented Jun 20, 2019

This PR adds a Typed version of the ReadOnly trait.

@itziakos itziakos requested review from mdickinson and corranwebster and removed request for corranwebster and mdickinson June 20, 2019 16:50
@itziakos itziakos changed the title Add a TypedReadOnly trait [wip] Add a TypedReadOnly trait Jun 20, 2019
@itziakos
Copy link
Member Author

sorry @corranwebster and @mdickinson I thought that I had the PR ready but it looks I am still missing something

@itziakos itziakos changed the title [wip] Add a TypedReadOnly trait Add a TypedReadOnly trait Jun 20, 2019
@itziakos itziakos requested a review from mdickinson June 20, 2019 19:48
@itziakos
Copy link
Member Author

@mdickinson and @corranwebster this pr is ready for review.

@codecov-io
Copy link

codecov-io commented Jun 22, 2019

Codecov Report

Merging #488 into master will increase coverage by 0.33%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #488      +/-   ##
==========================================
+ Coverage   65.15%   65.48%   +0.33%     
==========================================
  Files          44       44              
  Lines        7040     7061      +21     
  Branches     1413     1416       +3     
==========================================
+ Hits         4587     4624      +37     
+ Misses       2030     2014      -16     
  Partials      423      423
Impacted Files Coverage Δ
traits/api.py 86.2% <ø> (ø) ⬆️
traits/trait_types.py 63.98% <100%> (+0.58%) ⬆️
traits/has_traits.py 66.32% <0%> (+0.07%) ⬆️
traits/trait_handlers.py 63.4% <0%> (+0.33%) ⬆️
traits/etsconfig/etsconfig.py 63.58% <0%> (+6.17%) ⬆️

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 c602b70...94cd6bb. Read the comment docs.

@mdickinson mdickinson self-assigned this Jun 24, 2019
@mdickinson
Copy link
Member

@itziakos Thank you! I'll look at this within the next couple of days.

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.

@itziakos Thank you! I'll look at this within the next couple of days.

For a fairly geological interpretation of the word "days", obviously ... Sorry.

A couple of comments, below.

This trait doesn't behave quite the way I expect: I'd expect to get the Undefined object if I access before setting, but instead I get the default value. Perhaps there's no reasonable way to get the expected behaviour here.

>>> class A(HasTraits):
...     foo = TypedReadOnly(Int)
...     bar = ReadOnly()
... 
>>> a = A()
>>> a.foo  # expect undefined, or an error
0
>>> a.foo = 3
>>> a.foo
3
>>> a.foo = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_types.py", line 1314, in set
    raise TraitError(message.format(name, object.__class__.__name__))
traits.trait_errors.TraitError: Cannot set 'foo' of 'A' more than once.
>>> a.bar 
<undefined>
>>> a.bar = 3
>>> a.bar
3
>>> a.bar = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
traits.trait_errors.TraitError: Cannot modify the read only 'bar' attribute of a 'A' object.

#
# Thanks for using Enthought open source!
#
# Author: Ioannis Tziakos
Copy link
Member

Choose a reason for hiding this comment

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

Let's lose the "Author" and "Date" fields; we can track that information through version control.


def __init__(self, trait=Any, **metadata):
if isinstance(trait, type):
self.inner_traits = (trait(),)
Copy link
Member

Choose a reason for hiding this comment

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

Should inner_traits be a method (taking no additional arguments) rather than an attribute? I don't get the behaviour I expect on accessing inner_traits on the corresponding CTrait object:

>>> class A(HasTraits):
...     foo = TypedReadOnly(Int)
...     bar = List(Int)
... 
>>> a = A()
>>> a.traits()["foo"].inner_traits  # gives None
>>> a.traits()["bar"].inner_traits
(<traits.traits.CTrait object at 0x10f4d72c0>,)

@mdickinson mdickinson removed their assignment Nov 12, 2019
@corranwebster
Copy link
Contributor

I'm OK with the behaviour of returning a default before a value is set - I think that might be a more pragmatically useful behaviour - but perhaps the name of the trait is wrong: should it be SetOnce or something like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants