ipython / ipython/traitlets

Traitlets validates default even if overrided

Ouverte
#548 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Python
Étoiles
653
Forks
217
Merge moyen
2 j 21 h
PR mergées (30 j)
2

Description

To make a required trait, I thought I could use:

```
from traitlets import HasTraits, Unicode

class Foo(HasTraits):
bar = Unicode(default_value=None, allow_none=False, required=True)
```

As expected, this raises a trait error, if Foo is instantiated without arguments:

```
f = Foo()
```

But: this also raises, when bar is provided:

```
f = Foo(bar='test')
```

This error is raised:
```
TraitError Traceback (most recent call last)
in
----> 1 Foo(bar='test')

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in __new__(cls, *args, **kwargs)
956 else:
957 inst = new_meth(cls, *args, **kwargs)
--> 958 inst.setup_instance(*args, **kwargs)
959 return inst
960

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
984 self._trait_notifiers = {}
985 self._trait_validators = {}
--> 986 super(HasTraits, self).setup_instance(*args, **kwargs)
987
988 def __init__(self, *args, **kwargs):

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in setup_instance(self, *args, **kwargs)
975 else:
976 if isinstance(value, BaseDescriptor):
--> 977 value.instance_init(self)
978
979

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in instance_init(self, obj)
520 if (self._dynamic_default_callable(obj) is None) \
521 and (self.default_value is not Undefined):
--> 522 v = self._validate(obj, self.default_value)
523 if self.name is not None:
524 obj._trait_values[self.name] = v

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in _validate(self, obj, value)
589 return value
590 if hasattr(self, 'validate'):
--> 591 value = self.validate(obj, value)
592 if obj._cross_validation_lock is False:
593 value = self._cross_validate(obj, value)

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in validate(self, obj, value)
2052 msg = "Could not decode {!r} for unicode trait '{}' of {} instance."
2053 raise TraitError(msg.format(value, self.name, class_of(obj)))
-> 2054 self.error(obj, value)
2055
2056

/usr/lib/python3.8/site-packages/traitlets/traitlets.py in error(self, obj, value)
623 e = "The '%s' trait must be %s, but a value of %r was specified." \
624 % (self.name, self.info(), repr_type(value))
--> 625 raise TraitError(e)
626
627 def get_metadata(self, key, default=None):

TraitError: The 'bar' trait of a Foo instance must be a unicode string, but a value of None was specified.
```

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.