-
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
Add 'factory' support for the Any trait type #1557
Changes from all commits
278241c
a02b273
e59830e
972ea72
dd2b4df
e72ea7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,9 +52,8 @@ class Foo: | |
|
||
class Superclass(HasTraits): | ||
x = Any() | ||
# y = Any() | ||
|
||
|
||
class Subclass(Superclass): | ||
x = Instance(Foo) | ||
x = Instance(Foo) # E: assignment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line didn't raise before this PR, and does now; I believe that this was a bug in the test. |
||
y = Int() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,13 @@ class Any(TraitType): | |
will no longer be copied. If you need a per-instance default, use a | ||
``_trait_name_default`` method to supply that default. | ||
|
||
factory : callable, optional | ||
A callable, that when called with *args* and | ||
*kw*, returns the default value for the trait. | ||
args : tuple, optional | ||
Positional arguments (if any) for generating the default value. | ||
kw : dictionary, optional | ||
Keyword arguments (if any) for generating the default value. | ||
**metadata | ||
Metadata for the trait. | ||
""" | ||
|
@@ -214,7 +221,15 @@ class Any(TraitType): | |
#: A description of the type of value this trait accepts: | ||
info_text = "any value" | ||
|
||
def __init__(self, default_value=NoDefaultSpecified, **metadata): | ||
def __init__( | ||
self, | ||
default_value=NoDefaultSpecified, | ||
*, | ||
factory=None, | ||
args=(), | ||
kw={}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is considered an antipattern (compared to using |
||
**metadata | ||
): | ||
if isinstance(default_value, list): | ||
warnings.warn( | ||
( | ||
|
@@ -240,6 +255,17 @@ def __init__(self, default_value=NoDefaultSpecified, **metadata): | |
) | ||
self.default_value_type = DefaultValue.dict_copy | ||
|
||
# Sanity check the parameters | ||
if default_value is not NoDefaultSpecified and factory is not None: | ||
raise TypeError( | ||
"ambiguous defaults: both a default value and a default " | ||
"value factory are specified" | ||
) | ||
|
||
if factory is not None: | ||
self.default_value_type = DefaultValue.callable_and_args | ||
default_value = (factory, args, kw) | ||
|
||
super().__init__(default_value, **metadata) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected the name of the initial parameter from
value
todefault_value
(though I expect few people will be passing that parameter by name).