Skip to content

trait_medatada exception#37

Closed
SylvainCorlay wants to merge 1 commit intoipython:masterfrom
SylvainCorlay:allow_raise
Closed

trait_medatada exception#37
SylvainCorlay wants to merge 1 commit intoipython:masterfrom
SylvainCorlay:allow_raise

Conversation

@SylvainCorlay
Copy link
Member

If allow_raise is set to False, the method trait_metadata will return the default value even if traitname is not a valid trait name.

The goal is to optimize things like

if name in Foo.traits(bar=True):

which creates a dictionary and then checks if name is in it. Now one can simply do

if Foo.trait_metadata(name, 'bar', allow_raise=False):

@takluyver
Copy link
Member

if isinstance(getattr(type(Foo), name, None), TraitType):?

@SylvainCorlay
Copy link
Member Author

What do you mean (I also need to check for bar=True)?

@takluyver
Copy link
Member

What you want to test is 'does this object have a trait called name?', right? The snippet I posted should do that, though I haven't tested it. It seems preferable to me to do that test explicitly rather than asking for metadata to test if a trait exists.

If this is a common enough case, we could have a has_trait method that does this, but I think that's probably not needed. In most cases, the type of an object defines what traits it has, just like it defines what methods it has.

@SylvainCorlay
Copy link
Member Author

We currently have a filter Foo.traits(bar=True) that creates the Dict of all the trait attributes that have a metadata 'bar' equal to True.

What I am testing is if the trait_metadata 'bar' for 'name' is True. If 'name' is not a trait at all, I want False, not an exception.

It should be equivalent to name in Foo.traits(bar=True)

@takluyver
Copy link
Member

OK. So that's implicitly if has_trait(foo, 'name') and foo.trait_metadata('name', 'bar'):, right?

I'm wary of making dynamic things too easy, because in the major use cases for traitlets, they are relatively static things which are defined by the class.

Also, if we do this, I think the parameter name should be clearer. I'll have a think about what name I'd want.

@SylvainCorlay
Copy link
Member Author

Ok, I think that it is fine. Did no think of the has_trait method.

@SylvainCorlay SylvainCorlay deleted the allow_raise branch June 16, 2015 18:18
@takluyver
Copy link
Member

That doesn't actually exist, I was doing pseudocode ;-). But it's an easy addition if you think it's necessary.

@SylvainCorlay
Copy link
Member Author

Well, that might be the reason :) opening a PR.

@minrk minrk modified the milestone: no action Nov 9, 2015
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.

3 participants