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

If the default value for Unicode is traitlets.Undefined, then why is it type string when tested? How can you enforce CLI definition of an attribute if it self-massages to ""? #732

Open
metaperl opened this issue May 27, 2022 · 2 comments

Comments

@metaperl
Copy link
Contributor

The code below shows that even though x is never given a value, it is an empty string.

Why do the docs for Unicode state default_value=traitlets.Undefined yet the type is string when tested?

from traitlets.config import Application
from traitlets import List, Dict, Integer, Unicode

class App(Application):

    aliases = {"x": "App.x", "y": "App.y"}
    x = Unicode().tag(config=True)
    y = Unicode().tag(config=True)

    def start(self):
        print(f"typex = {type(self.x)} val={self.x}")
        print(f"y={self.y}")


if __name__ == "__main__":
    App.launch_instance()

Output

typex = <class 'str'> val=
y=
@metaperl
Copy link
Contributor Author

Even if allow-none=True, an undefined value is not defaulted to None. I changed one of the attributes to have allow_none=True and got this:

from traitlets.config import Application
from traitlets import List, Dict, Integer, Unicode, validate, TraitError


class App(Application):

    aliases = {"x": "App.x", "y": "App.y"}
    x = Unicode().tag(config=True)
    y = Integer(allow_none=True).tag(config=True)

    def start(self):
        print(f"typex = {type(self.x)} val={self.x}")
        print(f"typey = {type(self.y)} val={self.y}")

        self.x = "hi"
c:\programming\traitlets-lab\required-cli-argument>python required_cli.py 
python required_cli.py 
typex = <class 'str'> val=
typey = <class 'int'> val=0

if name == "main":
App.launch_instance()

@vidartf
Copy link
Contributor

vidartf commented Jun 8, 2022

While Undefined is the default value for the default_value argument to the __init__ function, it is never actually used as a value for a trait. It is a placeholder to indicate that there was no override of the default value. When no such override is given, this value is used:

default_value = ""

I agree that it would be good if the docs surfaced the actual default value in a better way!

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

No branches or pull requests

2 participants