-
Notifications
You must be signed in to change notification settings - Fork 85
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
Proposal: "Optional(Foo)" as a synonym for "Union(None, Foo)" #1298
Comments
+1 for easier ways to spell We'd need to figure out what the recommended practice should be for things that already allow I'm not so keen on the option to extend |
And I'd say "probably yes" here - it's a nice way to indicate to the code reader that yes, it really is intentional that this particular trait will sometimes be |
👋 Hey, I was surfing for Good First Issue labels to kill an evening and started working on this. A couple questions: Is that alright if I open a PR here? class MyClass(HasTraits):
a = Optional(Int)
b = Optional(Int(5))
c = Optional(Int, default_value=5) (And this can potentially get weirder with things like A literal implementation of " obj = MyClass()
obj.a # None
obj.b # None
obj.c # 5 because only in the third case are you setting the default value of What would be the desired behaviour here? And if differing from the above, is there any way to inspect a trait and determine if the default value is set by the user rather than undefined? I.e. differentiate (ETA: Also to note, I think simply flipping the definition to |
Hi Kevin! 👋
Absolutely, yes!
I think so, yes. On defaults, I think I'd go with the straightforward interpretation, so indeed
Agreed - I think it makes sense for the "default default" for |
It is common to have
Union
traits of the formUnion(None, <something>)
. As a convenience to developers, and following a similar convention in Python'styping
module, we should consider havingOptional(<something>)
as an alternative way of writingUnion(None, <something>)
.An alternative would be to make
allow_none
metadata universally accepted acrossTraitType
instances.The text was updated successfully, but these errors were encountered: