diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-05-12 12:25:10 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-05-12 12:25:10 +0300 |
commit | 242ed6b08b9edde490fb7a16a6063e0bad8ccc88 (patch) | |
tree | 905ee9509e06949fb489ee0532ce5db7d2ea9c29 /contrib/python/traitlets | |
parent | 64d1f1262fd0f33992ff184fef10d806d64f74da (diff) | |
download | ydb-242ed6b08b9edde490fb7a16a6063e0bad8ccc88.tar.gz |
intermediate changes
ref:0fbe94d885cb5449f7ff351c063c77f8b4f47715
Diffstat (limited to 'contrib/python/traitlets')
34 files changed, 2971 insertions, 2273 deletions
diff --git a/contrib/python/traitlets/py3/.dist-info/METADATA b/contrib/python/traitlets/py3/.dist-info/METADATA index 7387396b72..eb35757fe1 100644 --- a/contrib/python/traitlets/py3/.dist-info/METADATA +++ b/contrib/python/traitlets/py3/.dist-info/METADATA @@ -1,30 +1,21 @@ Metadata-Version: 2.1 Name: traitlets -Version: 5.1.1 +Version: 5.2.0 Summary: Traitlets Python configuration system -Home-page: https://github.com/ipython/traitlets -Author: IPython Development Team -Author-email: ipython-dev@python.org -License: BSD -Project-URL: Documentation, https://traitlets.readthedocs.io/ -Project-URL: Funding, https://numfocus.org/ -Project-URL: Source, https://github.com/ipython/traitlets -Project-URL: Tracker, https://github.com/ipython/traitlets/issues Keywords: Interactive,Interpreter,Shell,Web -Platform: Linux -Platform: Mac OS X -Platform: Windows +Author-email: IPython Development Team <ipython-dev@python.org> +Requires-Python: >=3.7 +Description-Content-Type: text/markdown Classifier: Intended Audience :: Developers Classifier: Intended Audience :: System Administrators Classifier: Intended Audience :: Science/Research Classifier: License :: OSI Approved :: BSD License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 -Requires-Python: >=3.7 -Description-Content-Type: text/markdown -License-File: COPYING.md +Requires-Dist: pytest ; extra == "test" +Requires-Dist: pre-commit ; extra == "test" +Project-URL: Homepage, https://github.com/ipython/traitlets Provides-Extra: test -Requires-Dist: pytest ; extra == 'test' # Traitlets @@ -32,17 +23,17 @@ Requires-Dist: pytest ; extra == 'test' [![Test downstream projects](https://github.com/ipython/traitlets/actions/workflows/downstream.yml/badge.svg)](https://github.com/ipython/traitlets/actions/workflows/downstream.yml) [![Documentation Status](https://readthedocs.org/projects/traitlets/badge/?version=latest)](https://traitlets.readthedocs.io/en/latest/?badge=latest) -| | | -|---------------|----------------------------------------| -| **home** | https://github.com/ipython/traitlets | -| **pypi-repo** | https://pypi.org/project/traitlets/ | -| **docs** | https://traitlets.readthedocs.io/ | -| **license** | Modified BSD License | +| | | +| ------------- | ------------------------------------ | +| **home** | https://github.com/ipython/traitlets | +| **pypi-repo** | https://pypi.org/project/traitlets/ | +| **docs** | https://traitlets.readthedocs.io/ | +| **license** | Modified BSD License | Traitlets is a pure Python library enabling: - the enforcement of strong typing for attributes of Python objects - (typed attributes are called *"traits"*); + (typed attributes are called _"traits"_); - dynamically calculated default values; - automatic validation and coercion of trait attributes when attempting a change; @@ -53,7 +44,7 @@ Traitlets is a pure Python library enabling: Its implementation relies on the [descriptor](https://docs.python.org/howto/descriptor.html) pattern, and it is a lightweight pure-python alternative of the -[*traits* library](https://docs.enthought.com/traits/). +[_traits_ library](https://docs.enthought.com/traits/). Traitlets powers the configuration system of IPython and Jupyter and the declarative API of IPython interactive widgets. @@ -83,6 +74,35 @@ pip install "traitlets[test]" py.test traitlets ``` +## Code Styling + +`traitlets` has adopted automatic code formatting so you shouldn't +need to worry too much about your code style. +As long as your code is valid, +the pre-commit hook should take care of how it should look. + +To install `pre-commit` locally, run the following:: + + pip install pre-commit + pre-commit install + +You can invoke the pre-commit hook by hand at any time with:: + + pre-commit run + +which should run any autoformatting on your code +and tell you about any errors it couldn't fix automatically. +You may also install [black integration](https://github.com/psf/black#editor-integration) +into your text editor to format code automatically. + +If you have already committed files before setting up the pre-commit +hook with `pre-commit install`, you can fix everything up using +`pre-commit run --all-files`. You need to make the fixing commit +yourself after that. + +Some of the hooks only run on CI by default, but you can invoke them by +running with the `--hook-stage manual` argument. + ## Usage Any class with trait attributes must inherit from `HasTraits`. @@ -119,7 +139,7 @@ To do something when a trait attribute is changed, decorate a method with The method will be called with a single argument, a dictionary which contains an owner, new value, old value, name of the changed trait, and the event type. -In this example, the `_num_changed` method is decorated with ``@observe(`num`)``: +In this example, the `_num_changed` method is decorated with `` @observe(`num`) ``: ```Python from traitlets import HasTraits, Integer, observe @@ -192,4 +212,3 @@ $ pip install build $ python -m build . ``` - diff --git a/contrib/python/traitlets/py3/COPYING.md b/contrib/python/traitlets/py3/COPYING.md index 39ca730a63..861df38586 100644 --- a/contrib/python/traitlets/py3/COPYING.md +++ b/contrib/python/traitlets/py3/COPYING.md @@ -27,7 +27,7 @@ software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER @@ -49,8 +49,8 @@ IPython uses a shared copyright model. Each contributor maintains copyright over their contributions to IPython. But, it is important to note that these contributions are typically only changes to the repositories. Thus, the IPython source code, in its entirety is not the copyright of any single person or -institution. Instead, it is the collective copyright of the entire IPython -Development Team. If individual contributors want to maintain a record of what +institution. Instead, it is the collective copyright of the entire IPython +Development Team. If individual contributors want to maintain a record of what changes/contributions they have specific copyright on, they should indicate their copyright in the commit message of the change, when they commit the change to one of the IPython repositories. diff --git a/contrib/python/traitlets/py3/README.md b/contrib/python/traitlets/py3/README.md index b0c1b2e67c..98fa570aa1 100644 --- a/contrib/python/traitlets/py3/README.md +++ b/contrib/python/traitlets/py3/README.md @@ -4,17 +4,17 @@ [![Test downstream projects](https://github.com/ipython/traitlets/actions/workflows/downstream.yml/badge.svg)](https://github.com/ipython/traitlets/actions/workflows/downstream.yml) [![Documentation Status](https://readthedocs.org/projects/traitlets/badge/?version=latest)](https://traitlets.readthedocs.io/en/latest/?badge=latest) -| | | -|---------------|----------------------------------------| -| **home** | https://github.com/ipython/traitlets | -| **pypi-repo** | https://pypi.org/project/traitlets/ | -| **docs** | https://traitlets.readthedocs.io/ | -| **license** | Modified BSD License | +| | | +| ------------- | ------------------------------------ | +| **home** | https://github.com/ipython/traitlets | +| **pypi-repo** | https://pypi.org/project/traitlets/ | +| **docs** | https://traitlets.readthedocs.io/ | +| **license** | Modified BSD License | Traitlets is a pure Python library enabling: - the enforcement of strong typing for attributes of Python objects - (typed attributes are called *"traits"*); + (typed attributes are called _"traits"_); - dynamically calculated default values; - automatic validation and coercion of trait attributes when attempting a change; @@ -25,7 +25,7 @@ Traitlets is a pure Python library enabling: Its implementation relies on the [descriptor](https://docs.python.org/howto/descriptor.html) pattern, and it is a lightweight pure-python alternative of the -[*traits* library](https://docs.enthought.com/traits/). +[_traits_ library](https://docs.enthought.com/traits/). Traitlets powers the configuration system of IPython and Jupyter and the declarative API of IPython interactive widgets. @@ -55,6 +55,35 @@ pip install "traitlets[test]" py.test traitlets ``` +## Code Styling + +`traitlets` has adopted automatic code formatting so you shouldn't +need to worry too much about your code style. +As long as your code is valid, +the pre-commit hook should take care of how it should look. + +To install `pre-commit` locally, run the following:: + + pip install pre-commit + pre-commit install + +You can invoke the pre-commit hook by hand at any time with:: + + pre-commit run + +which should run any autoformatting on your code +and tell you about any errors it couldn't fix automatically. +You may also install [black integration](https://github.com/psf/black#editor-integration) +into your text editor to format code automatically. + +If you have already committed files before setting up the pre-commit +hook with `pre-commit install`, you can fix everything up using +`pre-commit run --all-files`. You need to make the fixing commit +yourself after that. + +Some of the hooks only run on CI by default, but you can invoke them by +running with the `--hook-stage manual` argument. + ## Usage Any class with trait attributes must inherit from `HasTraits`. @@ -91,7 +120,7 @@ To do something when a trait attribute is changed, decorate a method with The method will be called with a single argument, a dictionary which contains an owner, new value, old value, name of the changed trait, and the event type. -In this example, the `_num_changed` method is decorated with ``@observe(`num`)``: +In this example, the `_num_changed` method is decorated with `` @observe(`num`) ``: ```Python from traitlets import HasTraits, Integer, observe diff --git a/contrib/python/traitlets/py3/patches/01-fix-tests.patch b/contrib/python/traitlets/py3/patches/01-fix-tests.patch index a4b6de274d..3b23932a03 100644 --- a/contrib/python/traitlets/py3/patches/01-fix-tests.patch +++ b/contrib/python/traitlets/py3/patches/01-fix-tests.patch @@ -1,83 +1,44 @@ --- contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py (index) +++ contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py (working tree) -@@ -22,7 +22,7 @@ from traitlets.traitlets import ( - - from traitlets.config.loader import Config - +@@ -33,1 +33,1 @@ from traitlets.traitlets import ( -from ...tests._warnings import expected_warnings +from traitlets.tests._warnings import expected_warnings - - class MyConfigurable(Configurable): - a = Integer(1, help="The integer a.").tag(config=True) --- contrib/python/traitlets/py3/traitlets/config/tests/test_application.py (index) +++ contrib/python/traitlets/py3/traitlets/config/tests/test_application.py (working tree) -@@ -629,6 +629,8 @@ class TestApplication(TestCase): - self.assertEqual(app.running, False) +@@ -635,2 +635,3 @@ class TestApplication(TestCase): - -+ +@mark.skip def test_cli_multi_scalar(caplog): - class App(Application): - aliases = {"opt": "App.opt"} -@@ -648,7 +650,7 @@ def test_cli_multi_scalar(caplog): - - class Root(Application): - subcommands = { -- 'sub1': ('traitlets.config.tests.test_application.Sub1', 'import string'), -+ 'sub1': ('__tests__.config.tests.test_application.Sub1', 'import string'), - } - - +@@ -655,1 +656,1 @@ def test_cli_multi_scalar(caplog): +- "sub1": ("traitlets.config.tests.test_application.Sub1", "import string"), ++ "sub1": ("__tests__.config.tests.test_application.Sub1", "import string"), --- contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py (index) +++ contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py (working tree) -@@ -13,7 +13,7 @@ from unittest import TestCase - - import pytest - +@@ -63,1 +63,1 @@ from unittest import TestCase -from ._warnings import expected_warnings +from traitlets.tests._warnings import expected_warnings - from traitlets import ( - HasTraits, - MetaHasTraits, --- contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py (index) +++ contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py (working tree) -@@ -1,4 +1,4 @@ +@@ -1,1 +1,1 @@ -from ..bunch import Bunch +from traitlets.utils.bunch import Bunch - - def test_bunch(): - b = Bunch(x=5, y=10) --- contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py (index) +++ contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py (working tree) -@@ -2,9 +2,9 @@ from unittest import TestCase - - from inspect import Signature, Parameter, signature - +@@ -4,2 +4,2 @@ from unittest import TestCase -from ...traitlets import HasTraits, Int, Unicode +from traitlets.traitlets import HasTraits, Int, Unicode - -from ..decorators import signature_has_traits +from traitlets.utils.decorators import signature_has_traits - - - class TestExpandSignature(TestCase): --- contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py (index) +++ contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py (working tree) -@@ -8,7 +8,7 @@ - import os - from unittest import TestCase - +@@ -11,1 +11,1 @@ -from ..importstring import import_item +from traitlets.utils.importstring import import_item - - - class TestImportItem(TestCase): --- contrib/python/traitlets/py3/traitlets/tests/utils.py (index) +++ contrib/python/traitlets/py3/traitlets/tests/utils.py (working tree) @@ -1,10 +1,13 @@ - from subprocess import Popen, PIPE import sys + from subprocess import PIPE, Popen +import os @@ -85,8 +46,8 @@ """Get stdout, stderr, and exit code from running a command""" - p = Popen(cmd, stdout=PIPE, stderr=PIPE) + env = os.environ.copy() -+ env['Y_PYTHON_ENTRY_POINT'] = ':main' ++ env["Y_PYTHON_ENTRY_POINT"] = ":main" + p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env) out, err = p.communicate() - out = out.decode('utf8', 'replace') - err = err.decode('utf8', 'replace') + out = out.decode("utf8", "replace") + err = err.decode("utf8", "replace") diff --git a/contrib/python/traitlets/py3/traitlets/__init__.py b/contrib/python/traitlets/py3/traitlets/__init__.py index ad5ba73c86..a3ea9f0d54 100644 --- a/contrib/python/traitlets/py3/traitlets/__init__.py +++ b/contrib/python/traitlets/py3/traitlets/__init__.py @@ -1,16 +1,17 @@ +"""Traitlets Python configuration system""" from warnings import warn from . import traitlets +from ._version import __version__, version_info from .traitlets import * -from .utils.importstring import import_item -from .utils.decorators import signature_has_traits from .utils.bunch import Bunch -from ._version import version_info, __version__ +from .utils.decorators import signature_has_traits +from .utils.importstring import import_item -class Sentinel(traitlets.Sentinel): +class Sentinel(traitlets.Sentinel): # type:ignore[name-defined] def __init__(self, *args, **kwargs): - super(Sentinel, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) warn( """ Sentinel is not a public part of the traitlets API. diff --git a/contrib/python/traitlets/py3/traitlets/_version.py b/contrib/python/traitlets/py3/traitlets/_version.py index 5f05912b66..c416b77b84 100644 --- a/contrib/python/traitlets/py3/traitlets/_version.py +++ b/contrib/python/traitlets/py3/traitlets/_version.py @@ -1,13 +1,10 @@ -version_info = (5, 1, 1) +version_info = (5, 2, 0) # unlike `.dev`, alpha, beta and rc _must not_ have dots, # or the wheel and tgz won't look to pip like the same version. __version__ = ( - ".".join(map(str, version_info)) - .replace(".b", "b") - .replace(".a", "a") - .replace(".rc", "rc") + ".".join(map(str, version_info)).replace(".b", "b").replace(".a", "a").replace(".rc", "rc") ) assert ".b" not in __version__ assert ".a" not in __version__ diff --git a/contrib/python/traitlets/py3/traitlets/config/__init__.py b/contrib/python/traitlets/py3/traitlets/config/__init__.py index 0ae7d63171..c5feccc73b 100644 --- a/contrib/python/traitlets/py3/traitlets/config/__init__.py +++ b/contrib/python/traitlets/py3/traitlets/config/__init__.py @@ -1,5 +1,3 @@ -# encoding: utf-8 - # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. diff --git a/contrib/python/traitlets/py3/traitlets/config/application.py b/contrib/python/traitlets/py3/traitlets/config/application.py index 99a6ef7ee0..02dc72eb56 100644 --- a/contrib/python/traitlets/py3/traitlets/config/application.py +++ b/contrib/python/traitlets/py3/traitlets/config/application.py @@ -4,8 +4,6 @@ # Distributed under the terms of the Modified BSD License. -from collections import defaultdict, OrderedDict -from copy import deepcopy import functools import json import logging @@ -13,25 +11,43 @@ import os import pprint import re import sys -import warnings +import typing as t +from collections import OrderedDict, defaultdict +from contextlib import suppress +from copy import deepcopy +from logging.config import dictConfig +from textwrap import dedent from traitlets.config.configurable import Configurable, SingletonConfigurable from traitlets.config.loader import ( - KVArgParseConfigLoader, PyFileConfigLoader, Config, ArgumentError, ConfigFileNotFound, JSONFileConfigLoader + ArgumentError, + Config, + ConfigFileNotFound, + JSONFileConfigLoader, + KVArgParseConfigLoader, + PyFileConfigLoader, ) from traitlets.traitlets import ( - Bool, Unicode, List, Enum, Dict, Instance, TraitError, observe, observe_compat, default, + Bool, + Dict, + Enum, + Instance, + List, + TraitError, + Unicode, + default, + observe, + observe_compat, ) - -from ..utils.importstring import import_item -from ..utils import cast_unicode +from traitlets.utils.nested_update import nested_update from traitlets.utils.text import indent, wrap_paragraphs -from textwrap import dedent +from ..utils import cast_unicode +from ..utils.importstring import import_item -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Descriptions for the various sections -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # merge flags&aliases into options option_description = """ The options below are convenience aliases to configurable class-options, @@ -46,7 +62,7 @@ The command-line option below sets the respective configurable class-parameter: This line is evaluated in Python, so simple expressions are allowed. For instance, to set `C.a=[0,1,2]`, you may type this: --C.a='range(3)' -""".strip() # trim newlines of front and back +""".strip() # trim newlines of front and back # sys.argv can be missing, for example when python is embedded. See the docs # for details: http://docs.python.org/2/c-api/intro.html#embedding-python @@ -59,19 +75,21 @@ subcommand 'cmd', do: `{app} cmd -h`. """ # get running program name -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Application class -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- - -_envvar = os.environ.get('TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR','') -if _envvar.lower() in {'1','true'}: +_envvar = os.environ.get("TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR", "") +if _envvar.lower() in {"1", "true"}: TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR = True -elif _envvar.lower() in {'0','false',''} : +elif _envvar.lower() in {"0", "false", ""}: TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR = False else: - raise ValueError("Unsupported value for environment variable: 'TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}."% _envvar ) + raise ValueError( + "Unsupported value for environment variable: 'TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR' is set to '%s' which is none of {'0', '1', 'false', 'true', ''}." + % _envvar + ) def catch_config_error(method): @@ -82,6 +100,7 @@ def catch_config_error(method): For use on init methods, to prevent invoking excepthook on invalid input. """ + @functools.wraps(method) def inner(app, *args, **kwargs): try: @@ -93,6 +112,7 @@ def catch_config_error(method): return inner + class ApplicationError(Exception): pass @@ -106,6 +126,7 @@ class LevelFormatter(logging.Formatter): Useful for adding 'WARNING' to warning messages, without adding 'INFO' to info, etc. """ + highlevel_limit = logging.WARN highlevel_format = " %(levelname)s |" @@ -114,7 +135,7 @@ class LevelFormatter(logging.Formatter): record.highlevel = self.highlevel_format % record.__dict__ else: record.highlevel = "" - return super(LevelFormatter, self).format(record) + return super().format(record) class Application(SingletonConfigurable): @@ -122,11 +143,11 @@ class Application(SingletonConfigurable): # The name of the application, will usually match the name of the command # line application - name = Unicode('application') + name = Unicode("application") # The description of the application that is printed at the beginning # of the help. - description = Unicode('This is an application.') + description = Unicode("This is an application.") # default section descriptions option_description = Unicode(option_description) keyvalue_description = Unicode(keyvalue_description) @@ -140,7 +161,7 @@ class Application(SingletonConfigurable): # A sequence of Configurable subclasses whose config=True attributes will # be exposed at the command line. - classes = [] + classes: t.List[t.Type[t.Any]] = [] def _classes_inc_parents(self, classes=None): """Iterate through configurable classes, including configurable parents @@ -163,7 +184,7 @@ class Application(SingletonConfigurable): yield parent # The version string of this application. - version = Unicode('0.0') + version = Unicode("0.0") # the argv used to initialize the application argv = List() @@ -172,99 +193,189 @@ class Application(SingletonConfigurable): raise_config_file_errors = Bool(TRAITLETS_APPLICATION_RAISE_CONFIG_FILE_ERROR) # The log level for the application - log_level = Enum((0,10,20,30,40,50,'DEBUG','INFO','WARN','ERROR','CRITICAL'), - default_value=logging.WARN, - help="Set the log level by value or name.").tag(config=True) - - @observe('log_level') - @observe_compat - def _log_level_changed(self, change): - """Adjust the log level when log_level is set.""" - new = change.new - if isinstance(new, str): - new = getattr(logging, new) - self.log_level = new - self.log.setLevel(new) + log_level = Enum( + (0, 10, 20, 30, 40, 50, "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"), + default_value=logging.WARN, + help="Set the log level by value or name.", + ).tag(config=True) _log_formatter_cls = LevelFormatter - log_datefmt = Unicode("%Y-%m-%d %H:%M:%S", - help="The date format used by logging formatters for %(asctime)s" + log_datefmt = Unicode( + "%Y-%m-%d %H:%M:%S", help="The date format used by logging formatters for %(asctime)s" ).tag(config=True) - log_format = Unicode("[%(name)s]%(highlevel)s %(message)s", + log_format = Unicode( + "[%(name)s]%(highlevel)s %(message)s", help="The Logging format template", ).tag(config=True) - @observe('log_datefmt', 'log_format') - @observe_compat - def _log_format_changed(self, change): - """Change the log formatter when log_format is set.""" - _log_handler = self._get_log_handler() - if not _log_handler: - warnings.warn( - f"No Handler found on {self.log}, setting log_format will have no effect", - RuntimeWarning, - ) - return - _log_formatter = self._log_formatter_cls(fmt=self.log_format, datefmt=self.log_datefmt) - _log_handler.setFormatter(_log_formatter) - - @default('log') - def _log_default(self): - """Start logging for this application. + def get_default_logging_config(self): + """Return the base logging configuration. The default is to log to stderr using a StreamHandler, if no default - handler already exists. The log level starts at logging.WARN, but this - can be adjusted by setting the ``log_level`` attribute. + handler already exists. + + The log handler level starts at logging.WARN, but this can be adjusted + by setting the ``log_level`` attribute. + + The ``logging_config`` trait is merged into this allowing for finer + control of logging. + """ + config: t.Dict[str, t.Any] = { + "version": 1, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "formatter": "console", + "level": logging.getLevelName(self.log_level), + "stream": "ext://sys.stderr", + }, + }, + "formatters": { + "console": { + "class": ( + f"{self._log_formatter_cls.__module__}" + f".{self._log_formatter_cls.__name__}" + ), + "format": self.log_format, + "datefmt": self.log_datefmt, + }, + }, + "loggers": { + self.__class__.__name__: { + "level": "DEBUG", + "handlers": ["console"], + } + }, + "disable_existing_loggers": False, + } + + if sys.executable and sys.executable.endswith("pythonw.exe"): + # disable logging + # (this should really go to a file, but file-logging is only + # hooked up in parallel applications) + del config["handlers"]["loggers"] + + return config + + @observe("log_datefmt", "log_format", "log_level", "logging_config") + def _observe_logging_change(self, change): + # convert log level strings to ints + log_level = self.log_level + if isinstance(log_level, str): + self.log_level = getattr(logging, log_level) + self._configure_logging() + + @observe("log", type="default") + def _observe_logging_default(self, change): + self._configure_logging() + + def _configure_logging(self): + config = self.get_default_logging_config() + nested_update(config, self.logging_config or {}) + dictConfig(config) + + @default("log") + def _log_default(self): + """Start logging for this application.""" log = logging.getLogger(self.__class__.__name__) - log.setLevel(self.log_level) log.propagate = False - _log = log # copied from Logger.hasHandlers() (new in Python 3.2) + _log = log # copied from Logger.hasHandlers() (new in Python 3.2) while _log: if _log.handlers: return log if not _log.propagate: break else: - _log = _log.parent - if sys.executable and sys.executable.endswith('pythonw.exe'): - # this should really go to a file, but file-logging is only - # hooked up in parallel applications - _log_handler = logging.StreamHandler(open(os.devnull, 'w')) - else: - _log_handler = logging.StreamHandler() - _log_formatter = self._log_formatter_cls(fmt=self.log_format, datefmt=self.log_datefmt) - _log_handler.setFormatter(_log_formatter) - log.addHandler(_log_handler) + _log = _log.parent # type:ignore[assignment] return log + logging_config = Dict( + help=""" + Configure additional log handlers. + + The default stderr logs handler is configured by the + log_level, log_datefmt and log_format settings. + + This configuration can be used to configure additional handlers + (e.g. to output the log to a file) or for finer control over the + default handlers. + + If provided this should be a logging configuration dictionary, for + more information see: + https://docs.python.org/3/library/logging.config.html#logging-config-dictschema + + This dictionary is merged with the base logging configuration which + defines the following: + + * A logging formatter intended for interactive use called + ``console``. + * A logging handler that writes to stderr called + ``console`` which uses the formatter ``console``. + * A logger with the name of this application set to ``DEBUG`` + level. + + This example adds a new handler that writes to a file: + + .. code-block:: python + + c.Application.logging_configuration = { + 'handlers': { + 'file': { + 'class': 'logging.FileHandler', + 'level': 'DEBUG', + 'filename': '<path/to/file>', + } + }, + 'loggers': { + '<application-name>': { + 'level': 'DEBUG', + # NOTE: if you don't list the default "console" + # handler here then it will be disabled + 'handlers': ['console', 'file'], + }, + } + } + + """, + ).tag(config=True) + #: the alias map for configurables #: Keys might strings or tuples for additional options; single-letter alias accessed like `-v`. #: Values might be like "Class.trait" strings of two-tuples: (Class.trait, help-text). - aliases = {'log-level' : 'Application.log_level'} + aliases: t.Dict[str, str] = {"log-level": "Application.log_level"} # flags for loading Configurables or store_const style flags # flags are loaded from this dict by '--key' flags # this must be a dict of two-tuples, the first element being the Config/dict # and the second being the help string for the flag - flags = { - 'debug': ({ - 'Application': { - 'log_level': logging.DEBUG, + flags: t.Dict[str, t.Any] = { + "debug": ( + { + "Application": { + "log_level": logging.DEBUG, + }, }, - }, "Set log-level to debug, for the most verbose logging."), - 'show-config': ({ - 'Application': { - 'show_config': True, + "Set log-level to debug, for the most verbose logging.", + ), + "show-config": ( + { + "Application": { + "show_config": True, + }, }, - }, "Show the application's configuration (human-readable format)"), - 'show-config-json': ({ - 'Application': { - 'show_config_json': True, + "Show the application's configuration (human-readable format)", + ), + "show-config-json": ( + { + "Application": { + "show_config_json": True, + }, }, - }, "Show the application's configuration (json format)"), + "Show the application's configuration (json format)", + ), } # subcommands for launching other applications @@ -274,17 +385,20 @@ class Application(SingletonConfigurable): # and the second being the help string for the subcommand subcommands = Dict() # parse_command_line will initialize a subapp, if requested - subapp = Instance('traitlets.config.application.Application', allow_none=True) + subapp = Instance("traitlets.config.application.Application", allow_none=True) # extra command-line arguments that don't set config values extra_args = List(Unicode()) - cli_config = Instance(Config, (), {}, + cli_config = Instance( + Config, + (), + {}, help="""The subset of our configuration that came from the command-line We re-load this configuration after loading config files, to ensure that it maintains highest priority. - """ + """, ) _loaded_config_files = List() @@ -297,15 +411,15 @@ class Application(SingletonConfigurable): help="Instead of starting the Application, dump configuration to stdout (as JSON)" ).tag(config=True) - @observe('show_config_json') + @observe("show_config_json") def _show_config_json_changed(self, change): self.show_config = change.new - @observe('show_config') + @observe("show_config") def _show_config_changed(self, change): if change.new: self._save_start = self.start - self.start = self.start_show_config + self.start = self.start_show_config # type:ignore[assignment] def __init__(self, **kwargs): SingletonConfigurable.__init__(self, **kwargs) @@ -319,11 +433,11 @@ class Application(SingletonConfigurable): else: self.classes.insert(0, self.__class__) - @observe('config') + @observe("config") @observe_compat def _config_changed(self, change): - super(Application, self)._config_changed(change) - self.log.debug('Config changed: %r', change.new) + super()._config_changed(change) + self.log.debug("Config changed: %r", change.new) @catch_config_error def initialize(self, argv=None): @@ -333,7 +447,6 @@ class Application(SingletonConfigurable): """ self.parse_command_line(argv) - def start(self): """Start the app mainloop. @@ -349,20 +462,19 @@ class Application(SingletonConfigurable): for cls in self.__class__.mro(): if cls.__name__ in config: cls_config = config[cls.__name__] - cls_config.pop('show_config', None) - cls_config.pop('show_config_json', None) + cls_config.pop("show_config", None) + cls_config.pop("show_config_json", None) if self.show_config_json: - json.dump(config, sys.stdout, - indent=1, sort_keys=True, default=repr) + json.dump(config, sys.stdout, indent=1, sort_keys=True, default=repr) # add trailing newline - sys.stdout.write('\n') + sys.stdout.write("\n") return if self._loaded_config_files: print("Loaded config files:") for f in self._loaded_config_files: - print(' ' + f) + print(" " + f) print() for classname in sorted(config): @@ -370,18 +482,20 @@ class Application(SingletonConfigurable): if not class_config: continue print(classname) - pformat_kwargs = dict(indent=4, compact=True) + pformat_kwargs: t.Dict[str, t.Any] = dict(indent=4, compact=True) for traitname in sorted(class_config): value = class_config[traitname] - print(' .{} = {}'.format( - traitname, - pprint.pformat(value, **pformat_kwargs), - )) + print( + " .{} = {}".format( + traitname, + pprint.pformat(value, **pformat_kwargs), + ) + ) def print_alias_help(self): """Print the alias parts of the help.""" - print('\n'.join(self.emit_alias_help())) + print("\n".join(self.emit_alias_help())) def emit_alias_help(self): """Yield the lines for alias part of the help.""" @@ -400,32 +514,29 @@ class Application(SingletonConfigurable): longname, fhelp = longname else: fhelp = None - classname, traitname = longname.split('.')[-2:] - longname = classname + '.' + traitname + classname, traitname = longname.split(".")[-2:] + longname = classname + "." + traitname cls = classdict[classname] trait = cls.class_traits(config=True)[traitname] fhelp = cls.class_get_trait_help(trait, helptext=fhelp).splitlines() if not isinstance(alias, tuple): - alias = (alias, ) - alias = sorted(alias, key=len) - alias = ', '.join(('--%s' if len(m) > 1 else '-%s') % m - for m in alias) + alias = (alias,) # type:ignore[assignment] + alias = sorted(alias, key=len) # type:ignore[assignment] + alias = ", ".join(("--%s" if len(m) > 1 else "-%s") % m for m in alias) # reformat first line - fhelp[0] = fhelp[0].replace('--' + longname, alias) - for l in fhelp: - yield l + fhelp[0] = fhelp[0].replace("--" + longname, alias) + yield from fhelp yield indent("Equivalent to: [--%s]" % longname) except Exception as ex: - self.log.error('Failed collecting help-message for alias %r, due to: %s', - alias, ex) + self.log.error("Failed collecting help-message for alias %r, due to: %s", alias, ex) raise def print_flag_help(self): """Print the flag part of the help.""" - print('\n'.join(self.emit_flag_help())) + print("\n".join(self.emit_flag_help())) def emit_flag_help(self): """Yield the lines for the flag part of the help.""" @@ -435,47 +546,44 @@ class Application(SingletonConfigurable): for flags, (cfg, fhelp) in self.flags.items(): try: if not isinstance(flags, tuple): - flags = (flags, ) - flags = sorted(flags, key=len) - flags = ', '.join(('--%s' if len(m) > 1 else '-%s') % m - for m in flags) + flags = (flags,) # type:ignore[assignment] + flags = sorted(flags, key=len) # type:ignore[assignment] + flags = ", ".join(("--%s" if len(m) > 1 else "-%s") % m for m in flags) yield flags yield indent(dedent(fhelp.strip())) - cfg_list = ' '.join('--%s.%s=%s' %(clname, prop, val) - for clname, props_dict - in cfg.items() - for prop, val in props_dict.items()) + cfg_list = " ".join( + f"--{clname}.{prop}={val}" + for clname, props_dict in cfg.items() + for prop, val in props_dict.items() + ) cfg_txt = "Equivalent to: [%s]" % cfg_list yield indent(dedent(cfg_txt)) except Exception as ex: - self.log.error('Failed collecting help-message for flag %r, due to: %s', - flags, ex) + self.log.error("Failed collecting help-message for flag %r, due to: %s", flags, ex) raise def print_options(self): """Print the options part of the help.""" - print('\n'.join(self.emit_options_help())) + print("\n".join(self.emit_options_help())) def emit_options_help(self): """Yield the lines for the options part of the help.""" if not self.flags and not self.aliases: return - header = 'Options' + header = "Options" yield header - yield '=' * len(header) + yield "=" * len(header) for p in wrap_paragraphs(self.option_description): yield p - yield '' + yield "" - for l in self.emit_flag_help(): - yield l - for l in self.emit_alias_help(): - yield l - yield '' + yield from self.emit_flag_help() + yield from self.emit_alias_help() + yield "" def print_subcommands(self): """Print the subcommand part of the help.""" - print('\n'.join(self.emit_subcommands_help())) + print("\n".join(self.emit_subcommands_help())) def emit_subcommands_help(self): """Yield the lines for the subcommand part of the help.""" @@ -484,16 +592,15 @@ class Application(SingletonConfigurable): header = "Subcommands" yield header - yield '=' * len(header) - for p in wrap_paragraphs(self.subcommand_description.format( - app=self.name)): + yield "=" * len(header) + for p in wrap_paragraphs(self.subcommand_description.format(app=self.name)): yield p - yield '' - for subc, (cls, help) in self.subcommands.items(): + yield "" + for subc, (_, help) in self.subcommands.items(): yield subc if help: yield indent(dedent(help.strip())) - yield '' + yield "" def emit_help_epilogue(self, classes): """Yield the very bottom lines of the help message. @@ -502,26 +609,23 @@ class Application(SingletonConfigurable): """ if not classes: yield "To see all available configurables, use `--help-all`." - yield '' + yield "" def print_help(self, classes=False): """Print the help for each Configurable class in self.classes. If classes=False (the default), only flags and aliases are printed. """ - print('\n'.join(self.emit_help(classes=classes))) + print("\n".join(self.emit_help(classes=classes))) def emit_help(self, classes=False): """Yield the help-lines for each Configurable class in self.classes. If classes=False (the default), only flags and aliases are printed. """ - for l in self.emit_description(): - yield l - for l in self.emit_subcommands_help(): - yield l - for l in self.emit_options_help(): - yield l + yield from self.emit_description() + yield from self.emit_subcommands_help() + yield from self.emit_options_help() if classes: help_classes = self._classes_with_config_traits() @@ -530,38 +634,35 @@ class Application(SingletonConfigurable): yield "=============" for p in wrap_paragraphs(self.keyvalue_description): yield p - yield '' + yield "" for cls in help_classes: yield cls.class_get_help() - yield '' - for l in self.emit_examples(): - yield l + yield "" + yield from self.emit_examples() - for l in self.emit_help_epilogue(classes): - yield l + yield from self.emit_help_epilogue(classes) def document_config_options(self): """Generate rST format documentation for the config options this application Returns a multiline string. """ - return '\n'.join(c.class_config_rst_doc() - for c in self._classes_inc_parents()) + return "\n".join(c.class_config_rst_doc() for c in self._classes_inc_parents()) def print_description(self): """Print the application description.""" - print('\n'.join(self.emit_description())) + print("\n".join(self.emit_description())) def emit_description(self): """Yield lines with the application description.""" - for p in wrap_paragraphs(self.description or self.__doc__): + for p in wrap_paragraphs(self.description or self.__doc__ or ""): yield p - yield '' + yield "" def print_examples(self): - """Print usage and examples (see `emit_examples()`). """ - print('\n'.join(self.emit_examples())) + """Print usage and examples (see `emit_examples()`).""" + print("\n".join(self.emit_examples())) def emit_examples(self): """Yield lines with the usage and examples. @@ -572,9 +673,9 @@ class Application(SingletonConfigurable): if self.examples: yield "Examples" yield "--------" - yield '' + yield "" yield indent(dedent(self.examples.strip())) - yield '' + yield "" def print_version(self): """Print the version string.""" @@ -588,7 +689,7 @@ class Application(SingletonConfigurable): if isinstance(subapp, str): subapp = import_item(subapp) - ## Cannot issubclass() on a non-type (SOhttp://stackoverflow.com/questions/8692430) + # Cannot issubclass() on a non-type (SOhttp://stackoverflow.com/questions/8692430) if isinstance(subapp, type) and issubclass(subapp, Application): # Clear existing instances before... self.__class__.clear_instance() @@ -596,7 +697,7 @@ class Application(SingletonConfigurable): self.subapp = subapp.instance(parent=self) elif callable(subapp): # or ask factory to create it... - self.subapp = subapp(self) + self.subapp = subapp(self) # type:ignore[call-arg] else: raise AssertionError("Invalid mappings for subcommand '%s'!" % subc) @@ -626,30 +727,30 @@ class Application(SingletonConfigurable): mro_tree[parent.__name__].append(clsname) # flatten aliases, which have the form: # { 'alias' : 'Class.trait' } - aliases = {} + aliases: t.Dict[str, str] = {} for alias, longname in self.aliases.items(): if isinstance(longname, tuple): longname, _ = longname - cls, trait = longname.split('.', 1) - children = mro_tree[cls] + cls, trait = longname.split(".", 1) # type:ignore[assignment] + children = mro_tree[cls] # type:ignore[index] if len(children) == 1: # exactly one descendent, promote alias - cls = children[0] + cls = children[0] # type:ignore[assignment] if not isinstance(aliases, tuple): - alias = (alias, ) + alias = (alias,) # type:ignore[assignment] for al in alias: - aliases[al] = '.'.join([cls,trait]) + aliases[al] = ".".join([cls, trait]) # type:ignore[list-item] # flatten flags, which are of the form: # { 'key' : ({'Cls' : {'trait' : value}}, 'help')} flags = {} for key, (flagdict, help) in self.flags.items(): - newflag = {} + newflag: t.Dict[t.Any, t.Any] = {} for cls, subdict in flagdict.items(): - children = mro_tree[cls] + children = mro_tree[cls] # type:ignore[index] # exactly one descendent, promote flag section if len(children) == 1: - cls = children[0] + cls = children[0] # type:ignore[assignment] if cls in newflag: newflag[cls].update(subdict) @@ -657,30 +758,29 @@ class Application(SingletonConfigurable): newflag[cls] = subdict if not isinstance(key, tuple): - key = (key, ) + key = (key,) # type:ignore[assignment] for k in key: flags[k] = (newflag, help) return flags, aliases def _create_loader(self, argv, aliases, flags, classes): - return KVArgParseConfigLoader(argv, aliases, flags, classes=classes, - log=self.log) + return KVArgParseConfigLoader(argv, aliases, flags, classes=classes, log=self.log) @catch_config_error def parse_command_line(self, argv=None): """Parse the command line arguments.""" assert not isinstance(argv, str) argv = sys.argv[1:] if argv is None else argv - self.argv = [cast_unicode(arg) for arg in argv ] + self.argv = [cast_unicode(arg) for arg in argv] - if argv and argv[0] == 'help': + if argv and argv[0] == "help": # turn `ipython help notebook` into `ipython notebook -h` - argv = argv[1:] + ['-h'] + argv = argv[1:] + ["-h"] if self.subcommands and len(argv) > 0: # we have subcommands, and one may have been specified subc, subargv = argv[0], argv[1:] - if re.match(r'^\w(\-?\w)*$', subc) and subc in self.subcommands: + if re.match(r"^\w(\-?\w)*$", subc) and subc in self.subcommands: # it's a subcommand, and *not* a flag or class parameter return self.initialize_subcommand(subc, subargv) @@ -689,15 +789,15 @@ class Application(SingletonConfigurable): # version), we want to only search the arguments up to the first # occurrence of '--', which we're calling interpreted_argv. try: - interpreted_argv = argv[:argv.index('--')] + interpreted_argv = argv[: argv.index("--")] except ValueError: interpreted_argv = argv - if any(x in interpreted_argv for x in ('-h', '--help-all', '--help')): - self.print_help('--help-all' in interpreted_argv) + if any(x in interpreted_argv for x in ("-h", "--help-all", "--help")): + self.print_help("--help-all" in interpreted_argv) self.exit(0) - if '--version' in interpreted_argv or '-V' in interpreted_argv: + if "--version" in interpreted_argv or "-V" in interpreted_argv: self.print_version() self.exit(0) @@ -726,12 +826,12 @@ class Application(SingletonConfigurable): path = [path] for path in path[::-1]: # path list is in descending priority order, so load files backwards: - pyloader = cls.python_config_loader_class(basefilename+'.py', path=path, log=log) + pyloader = cls.python_config_loader_class(basefilename + ".py", path=path, log=log) if log: log.debug("Looking for %s in %s", basefilename, path or os.getcwd()) - jsonloader = cls.json_config_loader_class(basefilename+'.json', path=path, log=log) - loaded = [] - filenames = [] + jsonloader = cls.json_config_loader_class(basefilename + ".json", path=path, log=log) + loaded: t.List[t.Any] = [] + filenames: t.List[str] = [] for loader in [pyloader, jsonloader]: config = None try: @@ -746,8 +846,7 @@ class Application(SingletonConfigurable): if raise_config_file_errors: raise if log: - log.error("Exception while loading config file %s", - filename, exc_info=True) + log.error("Exception while loading config file %s", filename, exc_info=True) else: if log: log.debug("Loaded config file: %s", loader.full_filename) @@ -755,10 +854,14 @@ class Application(SingletonConfigurable): for filename, earlier_config in zip(filenames, loaded): collisions = earlier_config.collisions(config) if collisions and log: - log.warning("Collisions detected in {0} and {1} config files." + log.warning( + "Collisions detected in {0} and {1} config files." " {1} has higher priority: {2}".format( - filename, loader.full_filename, json.dumps(collisions, indent=2), - )) + filename, + loader.full_filename, + json.dumps(collisions, indent=2), + ) + ) yield (config, loader.full_filename) loaded.append(config) filenames.append(loader.full_filename) @@ -773,11 +876,16 @@ class Application(SingletonConfigurable): """Load config files by filename and path.""" filename, ext = os.path.splitext(filename) new_config = Config() - for (config, filename) in self._load_config_files(filename, path=path, log=self.log, + for (config, filename) in self._load_config_files( + filename, + path=path, + log=self.log, raise_config_file_errors=self.raise_config_file_errors, ): new_config.merge(config) - if filename not in self._loaded_config_files: # only add to list of loaded files if not previously loaded + if ( + filename not in self._loaded_config_files + ): # only add to list of loaded files if not previously loaded self._loaded_config_files.append(filename) # add self.cli_config to preserve CLI config priority new_config.merge(self.cli_config) @@ -800,21 +908,23 @@ class Application(SingletonConfigurable): if classes is None: classes = self.classes - cls_to_config = OrderedDict( (cls, bool(cls.class_own_traits(config=True))) - for cls - in self._classes_inc_parents(classes)) + cls_to_config = OrderedDict( + (cls, bool(cls.class_own_traits(config=True))) + for cls in self._classes_inc_parents(classes) + ) def is_any_parent_included(cls): return any(b in cls_to_config and cls_to_config[b] for b in cls.__bases__) - ## Mark "empty" classes for inclusion if their parents own-traits, + # Mark "empty" classes for inclusion if their parents own-traits, # and loop until no more classes gets marked. # while True: to_incl_orig = cls_to_config.copy() - cls_to_config = OrderedDict( (cls, inc_yes or is_any_parent_included(cls)) - for cls, inc_yes - in cls_to_config.items()) + cls_to_config = OrderedDict( + (cls, inc_yes or is_any_parent_included(cls)) + for cls, inc_yes in cls_to_config.items() + ) if cls_to_config == to_incl_orig: break for cl, inc_yes in cls_to_config.items(): @@ -824,17 +934,26 @@ class Application(SingletonConfigurable): def generate_config_file(self, classes=None): """generate default config file from Configurables""" lines = ["# Configuration file for %s." % self.name] - lines.append('') + lines.append("") classes = self.classes if classes is None else classes config_classes = list(self._classes_with_config_traits(classes)) for cls in config_classes: lines.append(cls.class_config_section(config_classes)) - return '\n'.join(lines) + return "\n".join(lines) + + def close_handlers(self): + for handler in self.log.handlers: + with suppress(Exception): + handler.close() def exit(self, exit_status=0): self.log.debug("Exiting application: %s" % self.name) + self.close_handlers() sys.exit(exit_status) + def __del__(self): + self.close_handlers() + @classmethod def launch_instance(cls, argv=None, **kwargs): """Launch a global instance of this Application @@ -845,14 +964,16 @@ class Application(SingletonConfigurable): app.initialize(argv) app.start() -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # utility functions, for convenience -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- default_aliases = Application.aliases default_flags = Application.flags -def boolean_flag(name, configurable, set_help='', unset_help=''): + +def boolean_flag(name, configurable, set_help="", unset_help=""): """Helper for building basic --trait, --no-trait flags. Parameters @@ -873,14 +994,14 @@ def boolean_flag(name, configurable, set_help='', unset_help=''): the trait, respectively. """ # default helpstrings - set_help = set_help or "set %s=True"%configurable - unset_help = unset_help or "set %s=False"%configurable + set_help = set_help or "set %s=True" % configurable + unset_help = unset_help or "set %s=False" % configurable - cls,trait = configurable.split('.') + cls, trait = configurable.split(".") - setter = {cls : {trait : True}} - unsetter = {cls : {trait : False}} - return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)} + setter = {cls: {trait: True}} + unsetter = {cls: {trait: False}} + return {name: (setter, set_help), "no-" + name: (unsetter, unset_help)} def get_config(): @@ -894,5 +1015,5 @@ def get_config(): return Config() -if __name__ == '__main__': +if __name__ == "__main__": Application.launch_instance() diff --git a/contrib/python/traitlets/py3/traitlets/config/configurable.py b/contrib/python/traitlets/py3/traitlets/config/configurable.py index 3b2044a01b..5edb489201 100644 --- a/contrib/python/traitlets/py3/traitlets/config/configurable.py +++ b/contrib/python/traitlets/py3/traitlets/config/configurable.py @@ -4,31 +4,29 @@ # Distributed under the terms of the Modified BSD License. -from copy import deepcopy import logging import warnings +from copy import deepcopy +from textwrap import dedent -from .loader import Config, LazyConfigValue, DeferredConfig, _is_section_key from traitlets.traitlets import ( Any, - HasTraits, - Instance, Container, Dict, + HasTraits, + Instance, + default, observe, observe_compat, - default, validate, ) from traitlets.utils.text import indent, wrap_paragraphs -from textwrap import dedent - +from .loader import Config, DeferredConfig, LazyConfigValue, _is_section_key - -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Helper classes for Configurables -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class ConfigurableError(Exception): @@ -38,14 +36,16 @@ class ConfigurableError(Exception): class MultipleInstanceError(ConfigurableError): pass -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Configurable implementation -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- + class Configurable(HasTraits): config = Instance(Config, (), {}) - parent = Instance('traitlets.config.configurable.Configurable', allow_none=True) + parent = Instance("traitlets.config.configurable.Configurable", allow_none=True) def __init__(self, **kwargs): """Create a configurable given a config config. @@ -72,20 +72,21 @@ class Configurable(HasTraits): This ensures that instances will be configured properly. """ - parent = kwargs.pop('parent', None) + parent = kwargs.pop("parent", None) if parent is not None: # config is implied from parent - if kwargs.get('config', None) is None: - kwargs['config'] = parent.config + if kwargs.get("config", None) is None: + kwargs["config"] = parent.config self.parent = parent - config = kwargs.pop('config', None) + config = kwargs.pop("config", None) # load kwarg traits, other than config - super(Configurable, self).__init__(**kwargs) + super().__init__(**kwargs) # record traits set by config config_override_names = set() + def notice_config_override(change): """Record traits set by both config and kwargs. @@ -93,6 +94,7 @@ class Configurable(HasTraits): """ if change.name in kwargs: config_override_names.add(change.name) + self.observe(notice_config_override) # load config @@ -113,16 +115,17 @@ class Configurable(HasTraits): for name in config_override_names: setattr(self, name, kwargs[name]) - - #------------------------------------------------------------------------- + # ------------------------------------------------------------------------- # Static trait notifiations - #------------------------------------------------------------------------- + # ------------------------------------------------------------------------- @classmethod def section_names(cls): """return section names as a list""" - return [c.__name__ for c in reversed(cls.__mro__) if - issubclass(c, Configurable) and issubclass(cls, c) + return [ + c.__name__ + for c in reversed(cls.__mro__) + if issubclass(c, Configurable) and issubclass(cls, c) ] def _find_my_config(self, cfg): @@ -178,21 +181,25 @@ class Configurable(HasTraits): setattr(self, name, deepcopy(config_value)) elif not _is_section_key(name) and not isinstance(config_value, Config): from difflib import get_close_matches + if isinstance(self, LoggingConfigurable): warn = self.log.warning else: - warn = lambda msg: warnings.warn(msg, stacklevel=9) + warn = lambda msg: warnings.warn(msg, stacklevel=9) # noqa[E371] matches = get_close_matches(name, traits) msg = "Config option `{option}` not recognized by `{klass}`.".format( - option=name, klass=self.__class__.__name__) + option=name, klass=self.__class__.__name__ + ) if len(matches) == 1: - msg += " Did you mean `{matches}`?".format(matches=matches[0]) + msg += f" Did you mean `{matches[0]}`?" elif len(matches) >= 1: - msg +=" Did you mean one of: `{matches}`?".format(matches=', '.join(sorted(matches))) + msg += " Did you mean one of: `{matches}`?".format( + matches=", ".join(sorted(matches)) + ) warn(msg) - @observe('config') + @observe("config") @observe_compat def _config_changed(self, change): """Update all the class traits having ``config=True`` in metadata. @@ -235,13 +242,13 @@ class Configurable(HasTraits): """ assert inst is None or isinstance(inst, cls) final_help = [] - base_classes = ', '.join(p.__name__ for p in cls.__bases__) - final_help.append('%s(%s) options' % (cls.__name__, base_classes)) - final_help.append(len(final_help[0])*'-') - for k, v in sorted(cls.class_traits(config=True).items()): + base_classes = ", ".join(p.__name__ for p in cls.__bases__) + final_help.append(f"{cls.__name__}({base_classes}) options") + final_help.append(len(final_help[0]) * "-") + for _, v in sorted(cls.class_traits(config=True).items()): help = cls.class_get_trait_help(v, inst) final_help.append(help) - return '\n'.join(final_help) + return "\n".join(final_help) @classmethod def class_get_trait_help(cls, trait, inst=None, helptext=None): @@ -255,45 +262,45 @@ class Configurable(HasTraits): """ assert inst is None or isinstance(inst, cls) lines = [] - header = "--%s.%s" % (cls.__name__, trait.name) + header = f"--{cls.__name__}.{trait.name}" if isinstance(trait, (Container, Dict)): - multiplicity = trait.metadata.get('multiplicity', 'append') + multiplicity = trait.metadata.get("multiplicity", "append") if isinstance(trait, Dict): - sample_value = '<key-1>=<value-1>' + sample_value = "<key-1>=<value-1>" else: - sample_value = '<%s-item-1>' % trait.__class__.__name__.lower() - if multiplicity == 'append': - header = "%s=%s..." % (header, sample_value) + sample_value = "<%s-item-1>" % trait.__class__.__name__.lower() + if multiplicity == "append": + header = f"{header}={sample_value}..." else: - header = "%s %s..." % (header, sample_value) + header = f"{header} {sample_value}..." else: - header = '%s=<%s>' % (header, trait.__class__.__name__) - #header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__) + header = f"{header}=<{trait.__class__.__name__}>" + # header = "--%s.%s=<%s>" % (cls.__name__, trait.name, trait.__class__.__name__) lines.append(header) if helptext is None: helptext = trait.help - if helptext != '': - helptext = '\n'.join(wrap_paragraphs(helptext, 76)) + if helptext != "": + helptext = "\n".join(wrap_paragraphs(helptext, 76)) lines.append(indent(helptext)) - if 'Enum' in trait.__class__.__name__: + if "Enum" in trait.__class__.__name__: # include Enum choices - lines.append(indent('Choices: %s' % trait.info())) + lines.append(indent("Choices: %s" % trait.info())) if inst is not None: - lines.append(indent("Current: %r" % (getattr(inst, trait.name),))) + lines.append(indent(f"Current: {getattr(inst, trait.name)!r}")) else: try: dvr = trait.default_value_repr() except Exception: - dvr = None # ignore defaults we can't construct + dvr = None # ignore defaults we can't construct if dvr is not None: if len(dvr) > 64: dvr = dvr[:61] + "..." lines.append(indent("Default: %s" % dvr)) - return '\n'.join(lines) + return "\n".join(lines) @classmethod def class_print_help(cls, inst=None): @@ -320,9 +327,11 @@ class Configurable(HasTraits): """ defining_cls = cls for parent in cls.mro(): - if issubclass(parent, Configurable) and \ - parent in classes and \ - parent.class_own_traits(config=True).get(trait.name, None) is trait: + if ( + issubclass(parent, Configurable) + and parent in classes + and parent.class_own_traits(config=True).get(trait.name, None) is trait + ): defining_cls = parent return defining_cls @@ -336,31 +345,29 @@ class Configurable(HasTraits): The list of other classes in the config file. Used to reduce redundant information. """ + def c(s): """return a commented, wrapped block.""" - s = '\n\n'.join(wrap_paragraphs(s, 78)) + s = "\n\n".join(wrap_paragraphs(s, 78)) - return '## ' + s.replace('\n', '\n# ') + return "## " + s.replace("\n", "\n# ") # section header - breaker = '#' + '-' * 78 - parent_classes = ', '.join( - p.__name__ for p in cls.__bases__ - if issubclass(p, Configurable) - ) + breaker = "#" + "-" * 78 + parent_classes = ", ".join(p.__name__ for p in cls.__bases__ if issubclass(p, Configurable)) - s = "# %s(%s) configuration" % (cls.__name__, parent_classes) + s = f"# {cls.__name__}({parent_classes}) configuration" lines = [breaker, s, breaker] # get the description trait - desc = cls.class_traits().get('description') + desc = cls.class_traits().get("description") if desc: desc = desc.default_value if not desc: # no description from trait, use __doc__ - desc = getattr(cls, '__doc__', '') + desc = getattr(cls, "__doc__", "") if desc: lines.append(c(desc)) - lines.append('') + lines.append("") for name, trait in sorted(cls.class_traits(config=True).items()): default_repr = trait.default_value_repr() @@ -373,20 +380,20 @@ class Configurable(HasTraits): # cls owns the trait, show full help if trait.help: lines.append(c(trait.help)) - if 'Enum' in type(trait).__name__: + if "Enum" in type(trait).__name__: # include Enum choices - lines.append('# Choices: %s' % trait.info()) - lines.append('# Default: %s' % default_repr) + lines.append("# Choices: %s" % trait.info()) + lines.append("# Default: %s" % default_repr) else: # Trait appears multiple times and isn't defined here. # Truncate help to first line + "See also Original.trait" if trait.help: - lines.append(c(trait.help.split('\n', 1)[0])) - lines.append('# See also: %s.%s' % (defining_class.__name__, name)) + lines.append(c(trait.help.split("\n", 1)[0])) + lines.append(f"# See also: {defining_class.__name__}.{name}") - lines.append('# c.%s.%s = %s' % (cls.__name__, name, default_repr)) - lines.append('') - return '\n'.join(lines) + lines.append(f"# c.{cls.__name__}.{name} = {default_repr}") + lines.append("") + return "\n".join(lines) @classmethod def class_config_rst_doc(cls): @@ -396,40 +403,39 @@ class Configurable(HasTraits): """ lines = [] classname = cls.__name__ - for k, trait in sorted(cls.class_traits(config=True).items()): + for _, trait in sorted(cls.class_traits(config=True).items()): ttype = trait.__class__.__name__ - termline = classname + '.' + trait.name + termline = classname + "." + trait.name # Choices or type - if 'Enum' in ttype: + if "Enum" in ttype: # include Enum choices - termline += ' : ' + trait.info_rst() + termline += " : " + trait.info_rst() else: - termline += ' : ' + ttype + termline += " : " + ttype lines.append(termline) # Default value try: dvr = trait.default_value_repr() except Exception: - dvr = None # ignore defaults we can't construct + dvr = None # ignore defaults we can't construct if dvr is not None: if len(dvr) > 64: - dvr = dvr[:61]+'...' + dvr = dvr[:61] + "..." # Double up backslashes, so they get to the rendered docs dvr = dvr.replace("\\n", "\\\\n") lines.append(indent("Default: ``%s``" % dvr)) lines.append("") - help = trait.help or 'No description' + help = trait.help or "No description" lines.append(indent(dedent(help))) # Blank line - lines.append('') - - return '\n'.join(lines) + lines.append("") + return "\n".join(lines) class LoggingConfigurable(Configurable): @@ -456,12 +462,16 @@ class LoggingConfigurable(Configurable): if isinstance(self.parent, LoggingConfigurable): return self.parent.log from traitlets import log + return log.get_logger() def _get_log_handler(self): """Return the default Handler Returns None if none can be found + + Deprecated, this now returns the first log handler which may or may + not be the default one. """ logger = self.log if isinstance(logger, logging.LoggerAdapter): @@ -490,15 +500,16 @@ class SingletonConfigurable(LoggingConfigurable): """ for subclass in cls.mro(): - if issubclass(cls, subclass) and \ - issubclass(subclass, SingletonConfigurable) and \ - subclass != SingletonConfigurable: + if ( + issubclass(cls, subclass) + and issubclass(subclass, SingletonConfigurable) + and subclass != SingletonConfigurable + ): yield subclass @classmethod def clear_instance(cls): - """unset _instance for this class and singleton parents. - """ + """unset _instance for this class and singleton parents.""" if not cls.initialized(): return for subclass in cls._walk_mro(): @@ -555,6 +566,3 @@ class SingletonConfigurable(LoggingConfigurable): def initialized(cls): """Has an instance been created?""" return hasattr(cls, "_instance") and cls._instance is not None - - - diff --git a/contrib/python/traitlets/py3/traitlets/config/loader.py b/contrib/python/traitlets/py3/traitlets/config/loader.py index 5360f889ab..14a6863589 100644 --- a/contrib/python/traitlets/py3/traitlets/config/loader.py +++ b/contrib/python/traitlets/py3/traitlets/config/loader.py @@ -5,38 +5,41 @@ import argparse import copy +import json import os import re import sys -import json +import typing as t import warnings -from ..utils import cast_unicode, filefind +from traitlets.traitlets import Any, Container, Dict, HasTraits, List, Undefined -from traitlets.traitlets import ( - HasTraits, Container, List, Dict, Any, Undefined, -) +from ..utils import cast_unicode, filefind -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Exceptions -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class ConfigError(Exception): pass + class ConfigLoaderError(ConfigError): pass + class ConfigFileNotFound(ConfigError): pass + class ArgumentError(ConfigLoaderError): pass -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Argparse fix -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Unfortunately argparse by default prints help messages to stderr instead of # stdout. This makes it annoying to capture long help screens at the command @@ -62,17 +65,20 @@ class ArgumentParser(argparse.ArgumentParser): def print_help(self, file=None): if file is None: file = sys.stdout - return super(ArgumentParser, self).print_help(file) + return super().print_help(file) print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__ -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Config class for holding config information -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- + def execfile(fname, glob): - with open(fname, 'rb') as f: - exec(compile(f.read(), fname, 'exec'), glob, glob) + with open(fname, "rb") as f: + exec(compile(f.read(), fname, "exec"), glob, glob) + class LazyConfigValue(HasTraits): """Proxy object for exposing methods on configurable containers @@ -106,7 +112,6 @@ class LazyConfigValue(HasTraits): """like list.extend, but for the front""" self._prepend[:0] = other - def merge_into(self, other): """ Merge with another earlier LazyConfigValue or an earlier container. @@ -193,31 +198,31 @@ class LazyConfigValue(HasTraits): """ d = {} if self._update: - d['update'] = self._update + d["update"] = self._update if self._extend: - d['extend'] = self._extend + d["extend"] = self._extend if self._prepend: - d['prepend'] = self._prepend + d["prepend"] = self._prepend elif self._inserts: - d['inserts'] = self._inserts + d["inserts"] = self._inserts return d def __repr__(self): if self._value is not None: - return "<%s value=%r>" % (self.__class__.__name__, self._value) + return f"<{self.__class__.__name__} value={self._value!r}>" else: - return "<%s %r>" % (self.__class__.__name__, self.to_dict()) + return f"<{self.__class__.__name__} {self.to_dict()!r}>" def _is_section_key(key): """Is a Config key a section name (does it start with a capital)?""" - if key and key[0].upper()==key[0] and not key.startswith('_'): + if key and key[0].upper() == key[0] and not key.startswith("_"): return True else: return False -class Config(dict): +class Config(dict): # type:ignore[type-arg] """An attribute-based dict that can do smart merges. Accessing a field on a config object for the first time populates the key @@ -243,9 +248,7 @@ class Config(dict): """ for key in self: obj = self[key] - if _is_section_key(key) \ - and isinstance(obj, dict) \ - and not isinstance(obj, Config): + if _is_section_key(key) and isinstance(obj, dict) and not isinstance(obj, Config): setattr(self, key, Config(obj)) def _merge(self, other): @@ -258,7 +261,7 @@ class Config(dict): for k, v in other.items(): if k not in self: to_update[k] = v - else: # I have this key + else: # I have this key if isinstance(v, Config) and isinstance(self[k], Config): # Recursively merge common sub Configs self[k].merge(v) @@ -270,7 +273,7 @@ class Config(dict): self.update(to_update) - def collisions(self, other): + def collisions(self, other: "Config") -> t.Dict[str, t.Any]: """Check for collisions between two config objects. Returns a dict of the form {"Class": {"trait": "collision message"}}`, @@ -278,7 +281,7 @@ class Config(dict): An empty dict indicates no collisions. """ - collisions = {} + collisions: t.Dict[str, t.Any] = {} for section in self: if section not in other: continue @@ -287,18 +290,18 @@ class Config(dict): for key in mine: if key in theirs and mine[key] != theirs[key]: collisions.setdefault(section, {}) - collisions[section][key] = "%r ignored, using %r" % (mine[key], theirs[key]) + collisions[section][key] = f"{mine[key]!r} ignored, using {theirs[key]!r}" return collisions def __contains__(self, key): # allow nested contains of the form `"Section.key" in config` - if '.' in key: - first, remainder = key.split('.', 1) + if "." in key: + first, remainder = key.split(".", 1) if first not in self: return False return remainder in self[first] - return super(Config, self).__contains__(key) + return super().__contains__(key) # .has_key is deprecated for dictionaries. has_key = __contains__ @@ -332,7 +335,7 @@ class Config(dict): c = Config() dict.__setitem__(self, key, c) return c - elif not key.startswith('_'): + elif not key.startswith("_"): # undefined, create lazy value, used for container methods v = LazyConfigValue() dict.__setitem__(self, key, v) @@ -343,20 +346,22 @@ class Config(dict): def __setitem__(self, key, value): if _is_section_key(key): if not isinstance(value, Config): - raise ValueError('values whose keys begin with an uppercase ' - 'char must be Config instances: %r, %r' % (key, value)) + raise ValueError( + "values whose keys begin with an uppercase " + "char must be Config instances: %r, %r" % (key, value) + ) dict.__setitem__(self, key, value) def __getattr__(self, key): - if key.startswith('__'): - return dict.__getattr__(self, key) + if key.startswith("__"): + return dict.__getattr__(self, key) # type:ignore[attr-defined] try: return self.__getitem__(key) except KeyError as e: raise AttributeError(e) def __setattr__(self, key, value): - if key.startswith('__'): + if key.startswith("__"): return dict.__setattr__(self, key, value) try: self.__setitem__(key, value) @@ -364,7 +369,7 @@ class Config(dict): raise AttributeError(e) def __delattr__(self, key): - if key.startswith('__'): + if key.startswith("__"): return dict.__delattr__(self, key) try: dict.__delitem__(self, key) @@ -374,6 +379,7 @@ class Config(dict): class DeferredConfig: """Class for deferred-evaluation of config from CLI""" + pass def get_value(self, trait): @@ -401,6 +407,7 @@ class DeferredConfigString(str, DeferredConfig): .. versionadded:: 5.0 """ + def get_value(self, trait): """Get the value stored in this string""" s = str(self) @@ -413,10 +420,10 @@ class DeferredConfigString(str, DeferredConfig): return s def __repr__(self): - return '%s(%s)' % (self.__class__.__name__, self._super_repr()) + return f"{self.__class__.__name__}({self._super_repr()})" -class DeferredConfigList(list, DeferredConfig): +class DeferredConfigList(list, DeferredConfig): # type:ignore[type-arg] """Config value for loading config from a list of strings Interpretation is deferred until it is loaded into the trait. @@ -431,6 +438,7 @@ class DeferredConfigList(list, DeferredConfig): .. versionadded:: 5.0 """ + def get_value(self, trait): """Get the value stored in this string""" if hasattr(trait, "from_string_list"): @@ -439,7 +447,9 @@ class DeferredConfigList(list, DeferredConfig): else: # only allow one item if len(self) > 1: - raise ValueError(f"{trait.name} only accepts one value, got {len(self)}: {list(self)}") + raise ValueError( + f"{trait.name} only accepts one value, got {len(self)}: {list(self)}" + ) src = self[0] cast = trait.from_string @@ -452,15 +462,15 @@ class DeferredConfigList(list, DeferredConfig): return src def __repr__(self): - return '%s(%s)' % (self.__class__.__name__, self._super_repr()) + return f"{self.__class__.__name__}({self._super_repr()})" -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Config loading classes -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- -class ConfigLoader(object): +class ConfigLoader: """A object for loading configurations from just about anywhere. The resulting configuration is packaged as a :class:`Config`. @@ -477,6 +487,7 @@ class ConfigLoader(object): def _log_default(self): from traitlets.log import get_logger + return get_logger() def __init__(self, log=None): @@ -496,7 +507,7 @@ class ConfigLoader(object): self.clear() if log is None: self.log = self._log_default() - self.log.debug('Using default logger') + self.log.debug("Using default logger") else: self.log = log @@ -532,15 +543,16 @@ class FileConfigLoader(ConfigLoader): The path to search for the config file on, or a sequence of paths to try in order. """ - super(FileConfigLoader, self).__init__(**kw) + super().__init__(**kw) self.filename = filename self.path = path - self.full_filename = '' + self.full_filename = "" def _find_file(self): """Try to find the file by searching the paths.""" self.full_filename = filefind(self.filename, self.path) + class JSONFileConfigLoader(FileConfigLoader): """A JSON file loader for config @@ -558,7 +570,7 @@ class JSONFileConfigLoader(FileConfigLoader): self.clear() try: self._find_file() - except IOError as e: + except OSError as e: raise ConfigFileNotFound(str(e)) dct = self._read_file_as_dict() self.config = self._convert_to_config(dct) @@ -569,15 +581,15 @@ class JSONFileConfigLoader(FileConfigLoader): return json.load(f) def _convert_to_config(self, dictionary): - if 'version' in dictionary: - version = dictionary.pop('version') + if "version" in dictionary: + version = dictionary.pop("version") else: version = 1 if version == 1: return Config(dictionary) else: - raise ValueError('Unknown version of JSON config file: {version}'.format(version=version)) + raise ValueError(f"Unknown version of JSON config file: {version}") def __enter__(self): self.load_config() @@ -592,11 +604,10 @@ class JSONFileConfigLoader(FileConfigLoader): """ self.config.version = 1 json_config = json.dumps(self.config, indent=2) - with open(self.full_filename, 'w') as f: + with open(self.full_filename, "w") as f: f.write(json_config) - class PyFileConfigLoader(FileConfigLoader): """A config loader for pure python files. @@ -609,7 +620,7 @@ class PyFileConfigLoader(FileConfigLoader): self.clear() try: self._find_file() - except IOError as e: + except OSError as e: raise ConfigFileNotFound(str(e)) self._read_file_as_dict() return self.config @@ -631,6 +642,7 @@ class PyFileConfigLoader(FileConfigLoader): def _read_file_as_dict(self): """Load the config file into self.config, with recursive loading.""" + def get_config(): """Unnecessary now, but a deprecation warning is more trouble than it's worth.""" return self.config @@ -642,8 +654,8 @@ class PyFileConfigLoader(FileConfigLoader): __file__=self.full_filename, ) conf_filename = self.full_filename - with open(conf_filename, 'rb') as f: - exec(compile(f.read(), conf_filename, 'exec'), namespace, namespace) + with open(conf_filename, "rb") as f: + exec(compile(f.read(), conf_filename, "exec"), namespace, namespace) class CommandLineConfigLoader(ConfigLoader): @@ -690,13 +702,14 @@ class CommandLineConfigLoader(ConfigLoader): else: raise TypeError("Invalid flag: %r" % cfg) + # match --Class.trait keys for argparse # matches: # --Class.trait # --x # -x -class_trait_opt_pattern = re.compile(r'^\-?\-[A-Za-z][\w]*(\.[\w]+)*$') +class_trait_opt_pattern = re.compile(r"^\-?\-[A-Za-z][\w]*(\.[\w]+)*$") _DOT_REPLACEMENT = "__DOT__" _DASH_REPLACEMENT = "__DASH__" @@ -707,6 +720,7 @@ class _KVAction(argparse.Action): Always """ + def __call__(self, parser, namespace, values, option_string=None): if isinstance(values, str): values = [values] @@ -720,11 +734,12 @@ class _KVAction(argparse.Action): setattr(namespace, self.dest, items) -class _DefaultOptionDict(dict): +class _DefaultOptionDict(dict): # type:ignore[type-arg] """Like the default options dict but acts as if all --Class.trait options are predefined """ + def _add_kv_action(self, key): self[key] = _KVAction( option_strings=[key], @@ -734,7 +749,7 @@ class _DefaultOptionDict(dict): ) def __contains__(self, key): - if '=' in key: + if "=" in key: return False if super().__contains__(key): return True @@ -759,12 +774,12 @@ class _DefaultOptionDict(dict): class _KVArgParser(argparse.ArgumentParser): """subclass of ArgumentParser where any --Class.trait option is implicitly defined""" + def parse_known_args(self, args=None, namespace=None): # must be done immediately prior to parsing because if we do it in init, # registration of explicit actions via parser.add_option will fail during setup for container in (self, self._optionals): - container._option_string_actions = _DefaultOptionDict( - container._option_string_actions) + container._option_string_actions = _DefaultOptionDict(container._option_string_actions) return super().parse_known_args(args, namespace) @@ -773,8 +788,16 @@ class ArgParseConfigLoader(CommandLineConfigLoader): parser_class = ArgumentParser - def __init__(self, argv=None, aliases=None, flags=None, log=None, classes=(), - *parser_args, **parser_kw): + def __init__( + self, + argv: t.Optional[t.List[str]] = None, + aliases: t.Optional[t.Dict[str, str]] = None, + flags: t.Optional[t.Dict[str, str]] = None, + log: t.Any = None, + classes: t.Optional[t.List[t.Type[t.Any]]] = None, + *parser_args: t.Any, + **parser_kw: t.Any, + ) -> None: """Create a config loader for use with argparse. Parameters @@ -803,6 +826,7 @@ class ArgParseConfigLoader(CommandLineConfigLoader): config : Config The resulting Config object. """ + classes = classes or [] super(CommandLineConfigLoader, self).__init__(log=log) self.clear() if argv is None: @@ -853,13 +877,15 @@ class ArgParseConfigLoader(CommandLineConfigLoader): return self.config def get_extra_args(self): - if hasattr(self, 'extra_args'): + if hasattr(self, "extra_args"): return self.extra_args else: return [] def _create_parser(self): - self.parser = self.parser_class(*self.parser_args, **self.parser_kw) + self.parser = self.parser_class( + *self.parser_args, **self.parser_kw # type:ignore[arg-type] + ) self._add_arguments(self.aliases, self.flags, self.classes) def _add_arguments(self, aliases, flags, classes): @@ -869,14 +895,14 @@ class ArgParseConfigLoader(CommandLineConfigLoader): """self.parser->self.parsed_data""" uargs = [cast_unicode(a) for a in args] - unpacked_aliases = {} + unpacked_aliases: t.Dict[str, str] = {} if self.aliases: unpacked_aliases = {} for alias, alias_target in self.aliases.items(): if alias in self.flags: continue if not isinstance(alias, tuple): - short_alias, alias = alias, None + short_alias, alias = alias, None # type:ignore[assignment] else: short_alias, alias = alias for al in (short_alias, alias): @@ -893,12 +919,12 @@ class ArgParseConfigLoader(CommandLineConfigLoader): if arg == k: return v if arg.startswith(k + "="): - return v + "=" + arg[len(k) + 1:] + return v + "=" + arg[len(k) + 1 :] return arg - if '--' in uargs: - idx = uargs.index('--') - extra_args = uargs[idx+1:] + if "--" in uargs: + idx = uargs.index("--") + extra_args = uargs[idx + 1 :] to_parse = uargs[:idx] else: extra_args = [] @@ -920,17 +946,18 @@ class ArgParseConfigLoader(CommandLineConfigLoader): class _FlagAction(argparse.Action): """ArgParse action to handle a flag""" + def __init__(self, *args, **kwargs): - self.flag = kwargs.pop('flag') - self.alias = kwargs.pop('alias', None) - kwargs['const'] = Undefined + self.flag = kwargs.pop("flag") + self.alias = kwargs.pop("alias", None) + kwargs["const"] = Undefined if not self.alias: - kwargs['nargs'] = 0 - super(_FlagAction, self).__init__(*args, **kwargs) + kwargs["nargs"] = 0 + super().__init__(*args, **kwargs) def __call__(self, parser, namespace, values, option_string=None): if self.nargs == 0 or values is Undefined: - if not hasattr(namespace, '_flags'): + if not hasattr(namespace, "_flags"): namespace._flags = [] namespace._flags.append(self.flag) else: @@ -943,15 +970,16 @@ class KVArgParseConfigLoader(ArgParseConfigLoader): as well as arbitrary --Class.trait value """ - parser_class = _KVArgParser + parser_class = _KVArgParser # type:ignore[assignment] def _add_arguments(self, aliases, flags, classes): - alias_flags = {} + alias_flags: t.Dict[str, t.Any] = {} + argparse_kwds: t.Dict[str, t.Any] paa = self.parser.add_argument self.parser.set_defaults(_flags=[]) paa("extra_args", nargs="*") - ## An index of all container traits collected:: + # An index of all container traits collected:: # # { <traitname>: (<trait>, <argparse-kwds>) } # @@ -960,14 +988,14 @@ class KVArgParseConfigLoader(ArgParseConfigLoader): self.argparse_traits = argparse_traits = {} for cls in classes: for traitname, trait in cls.class_traits(config=True).items(): - argname = '%s.%s' % (cls.__name__, traitname) - argparse_kwds = {'type': str} + argname = f"{cls.__name__}.{traitname}" + argparse_kwds = {"type": str} if isinstance(trait, (Container, Dict)): - multiplicity = trait.metadata.get('multiplicity', 'append') - if multiplicity == 'append': - argparse_kwds['action'] = multiplicity + multiplicity = trait.metadata.get("multiplicity", "append") + if multiplicity == "append": + argparse_kwds["action"] = multiplicity else: - argparse_kwds['nargs'] = multiplicity + argparse_kwds["nargs"] = multiplicity argparse_traits[argname] = (trait, argparse_kwds) for keys, (value, _) in flags.items(): @@ -977,7 +1005,7 @@ class KVArgParseConfigLoader(ArgParseConfigLoader): if key in aliases: alias_flags[aliases[key]] = value continue - keys = ('-' + key, '--' + key) if len(key) == 1 else ('--' + key,) + keys = ("-" + key, "--" + key) if len(key) == 1 else ("--" + key,) paa(*keys, action=_FlagAction, flag=value) for keys, traitname in aliases.items(): @@ -986,28 +1014,28 @@ class KVArgParseConfigLoader(ArgParseConfigLoader): for key in keys: argparse_kwds = { - 'type': str, - 'dest': traitname.replace(".", _DOT_REPLACEMENT), - 'metavar': traitname, + "type": str, + "dest": traitname.replace(".", _DOT_REPLACEMENT), + "metavar": traitname, } if traitname in argparse_traits: argparse_kwds.update(argparse_traits[traitname][1]) - if 'action' in argparse_kwds and traitname in alias_flags: + if "action" in argparse_kwds and traitname in alias_flags: # flag sets 'action', so can't have flag & alias with custom action # on the same name raise ArgumentError( "The alias `%s` for the 'append' sequence " - "config-trait `%s` cannot be also a flag!'" - % (key, traitname)) + "config-trait `%s` cannot be also a flag!'" % (key, traitname) + ) if traitname in alias_flags: # alias and flag. # when called with 0 args: flag # when called with >= 1: alias - argparse_kwds.setdefault('nargs', '?') - argparse_kwds['action'] = _FlagAction - argparse_kwds['flag'] = alias_flags[traitname] - argparse_kwds['alias'] = traitname - keys = ('-' + key, '--' + key) if len(key) == 1 else ('--'+ key,) + argparse_kwds.setdefault("nargs", "?") + argparse_kwds["action"] = _FlagAction + argparse_kwds["flag"] = alias_flags[traitname] + argparse_kwds["alias"] = traitname + keys = ("-" + key, "--" + key) if len(key) == 1 else ("--" + key,) paa(*keys, **argparse_kwds) def _convert_to_config(self): @@ -1018,12 +1046,12 @@ class KVArgParseConfigLoader(ArgParseConfigLoader): if lhs == "extra_args": self.extra_args = ["-" if a == _DASH_REPLACEMENT else a for a in rhs] + extra_args continue - elif lhs == '_flags': + elif lhs == "_flags": # _flags will be handled later continue lhs = lhs.replace(_DOT_REPLACEMENT, ".") - if '.' not in lhs: + if "." not in lhs: # probably a mistyped alias, but not technically illegal self.log.warning("Unrecognized alias: '%s', it will have no effect.", lhs) trait = None @@ -1056,6 +1084,7 @@ class KeyValueConfigLoader(KVArgParseConfigLoader): Use KVArgParseConfigLoader """ + def __init__(self, *args, **kwargs): warnings.warn( "KeyValueConfigLoader is deprecated since Traitlets 5.0." @@ -1083,7 +1112,7 @@ def load_pyconfig_files(config_files, path): next_config = loader.load_config() except ConfigFileNotFound: pass - except: + except Exception: raise else: config.merge(next_config) diff --git a/contrib/python/traitlets/py3/traitlets/config/manager.py b/contrib/python/traitlets/py3/traitlets/config/manager.py index 164053261e..728cd2f22c 100644 --- a/contrib/python/traitlets/py3/traitlets/config/manager.py +++ b/contrib/python/traitlets/py3/traitlets/config/manager.py @@ -3,7 +3,6 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. import errno -import io import json import os @@ -38,7 +37,7 @@ class BaseJSONConfigManager(LoggingConfigurable): Deals with persisting/storing config in a json file """ - config_dir = Unicode('.') + config_dir = Unicode(".") def ensure_config_dir_exists(self): try: @@ -48,7 +47,7 @@ class BaseJSONConfigManager(LoggingConfigurable): raise def file_name(self, section_name): - return os.path.join(self.config_dir, section_name+'.json') + return os.path.join(self.config_dir, section_name + ".json") def get(self, section_name): """Retrieve the config data for the specified section. @@ -58,18 +57,17 @@ class BaseJSONConfigManager(LoggingConfigurable): """ filename = self.file_name(section_name) if os.path.isfile(filename): - with io.open(filename, encoding='utf-8') as f: + with open(filename, encoding="utf-8") as f: return json.load(f) else: return {} def set(self, section_name, data): - """Store the given config data. - """ + """Store the given config data.""" filename = self.file_name(section_name) self.ensure_config_dir_exists() - f = open(filename, 'w', encoding='utf-8') + f = open(filename, "w", encoding="utf-8") with f: json.dump(data, f, indent=2) diff --git a/contrib/python/traitlets/py3/traitlets/config/sphinxdoc.py b/contrib/python/traitlets/py3/traitlets/config/sphinxdoc.py index ce22e4a674..92c2d64d67 100644 --- a/contrib/python/traitlets/py3/traitlets/config/sphinxdoc.py +++ b/contrib/python/traitlets/py3/traitlets/config/sphinxdoc.py @@ -32,11 +32,11 @@ The generated rST syntax looks like this:: Cross reference like this: :configtrait:`Application.log_datefmt`. """ -from traitlets import Undefined -from traitlets.utils.text import indent from collections import defaultdict +from textwrap import dedent -from textwrap import indent as _indent, dedent +from traitlets import Undefined +from traitlets.utils.text import indent def setup(app): @@ -45,10 +45,11 @@ def setup(app): You shouldn't need to call this directly; configure Sphinx to use this module instead. """ - app.add_object_type('configtrait', 'configtrait', objname='Config option') - metadata = {'parallel_read_safe': True, 'parallel_write_safe': True} + app.add_object_type("configtrait", "configtrait", objname="Config option") + metadata = {"parallel_read_safe": True, "parallel_write_safe": True} return metadata + def interesting_default_value(dv): if (dv is None) or (dv is Undefined): return False @@ -56,12 +57,14 @@ def interesting_default_value(dv): return bool(dv) return True + def format_aliases(aliases): fmted = [] for a in aliases: - dashes = '-' if len(a) == 1 else '--' - fmted.append('``%s%s``' % (dashes, a)) - return ', '.join(fmted) + dashes = "-" if len(a) == 1 else "--" + fmted.append(f"``{dashes}{a}``") + return ", ".join(fmted) + def class_config_rst_doc(cls, trait_aliases): """Generate rST documentation for this class' config options. @@ -70,23 +73,19 @@ def class_config_rst_doc(cls, trait_aliases): """ lines = [] classname = cls.__name__ - for k, trait in sorted(cls.class_traits(config=True).items()): + for _, trait in sorted(cls.class_traits(config=True).items()): ttype = trait.__class__.__name__ - fullname = classname + '.' + trait.name - lines += ['.. configtrait:: ' + fullname, - '' - ] + fullname = classname + "." + trait.name + lines += [".. configtrait:: " + fullname, ""] help = trait.help.rstrip() or "No description" lines.append(indent(dedent(help)) + "\n") # Choices or type - if 'Enum' in ttype: + if "Enum" in ttype: # include Enum choices - lines.append( - indent(":options: " + ", ".join("``%r``" % x for x in trait.values)) - ) + lines.append(indent(":options: " + ", ".join("``%r``" % x for x in trait.values))) else: lines.append(indent(":trait type: " + ttype)) @@ -99,7 +98,7 @@ def class_config_rst_doc(cls, trait_aliases): dvr = None # ignore defaults we can't construct if dvr is not None: if len(dvr) > 64: - dvr = dvr[:61] + '...' + dvr = dvr[:61] + "..." # Double up backslashes, so they get to the rendered docs dvr = dvr.replace("\\n", "\\\\n") lines.append(indent(":default: ``%s``" % dvr)) @@ -110,13 +109,13 @@ def class_config_rst_doc(cls, trait_aliases): lines.append(indent(":CLI option: " + fmt_aliases)) # Blank line - lines.append('') + lines.append("") + + return "\n".join(lines) - return '\n'.join(lines) def reverse_aliases(app): - """Produce a mapping of trait names to lists of command line aliases. - """ + """Produce a mapping of trait names to lists of command line aliases.""" res = defaultdict(list) for alias, trait in app.aliases.items(): res[trait].append(alias) @@ -130,10 +129,11 @@ def reverse_aliases(app): if len(cls_cfg) == 1: traitname = list(cls_cfg)[0] if cls_cfg[traitname] is True: - res[classname+'.'+traitname].append(flag) + res[classname + "." + traitname].append(flag) return res + def write_doc(path, title, app, preamble=None): """Write a rst file documenting config options for a traitlets application. @@ -149,13 +149,13 @@ def write_doc(path, title, app, preamble=None): Extra text to add just after the title (optional) """ trait_aliases = reverse_aliases(app) - with open(path, 'w') as f: - f.write(title + '\n') - f.write(('=' * len(title)) + '\n') - f.write('\n') + with open(path, "w") as f: + f.write(title + "\n") + f.write(("=" * len(title)) + "\n") + f.write("\n") if preamble is not None: - f.write(preamble + '\n\n') + f.write(preamble + "\n\n") for c in app._classes_inc_parents(): f.write(class_config_rst_doc(c, trait_aliases)) - f.write('\n') + f.write("\n") diff --git a/contrib/python/traitlets/py3/traitlets/config/tests/test_application.py b/contrib/python/traitlets/py3/traitlets/config/tests/test_application.py index b3188bd7d1..860d1be2c9 100644 --- a/contrib/python/traitlets/py3/traitlets/config/tests/test_application.py +++ b/contrib/python/traitlets/py3/traitlets/config/tests/test_application.py @@ -1,4 +1,3 @@ -# coding: utf-8 """ Tests for traitlets.config.application.Application """ @@ -19,17 +18,7 @@ from unittest import TestCase import pytest from pytest import mark -from traitlets import ( - Bool, - Bytes, - Dict, - HasTraits, - Integer, - List, - Set, - Tuple, - Unicode, -) +from traitlets import Bool, Bytes, Dict, HasTraits, Integer, List, Set, Tuple, Unicode from traitlets.config.application import Application from traitlets.config.configurable import Configurable from traitlets.config.loader import Config @@ -42,73 +31,78 @@ from traitlets.tests.utils import ( try: from unittest import mock except ImportError: - import mock + from unittest import mock pjoin = os.path.join class Foo(Configurable): - i = Integer(0, help=""" + i = Integer( + 0, + help=""" The integer i. Details about i. - """).tag(config=True) + """, + ).tag(config=True) j = Integer(1, help="The integer j.").tag(config=True) - name = Unicode('Brian', help="First name.").tag(config=True) + name = Unicode("Brian", help="First name.").tag(config=True) la = List([]).tag(config=True) li = List(Integer()).tag(config=True) - fdict = Dict().tag(config=True, multiplicity='+') + fdict = Dict().tag(config=True, multiplicity="+") class Bar(Configurable): b = Integer(0, help="The integer b.").tag(config=True) enabled = Bool(True, help="Enable bar.").tag(config=True) - tb = Tuple(()).tag(config=True, multiplicity='*') - aset = Set().tag(config=True, multiplicity='+') + tb = Tuple(()).tag(config=True, multiplicity="*") + aset = Set().tag(config=True, multiplicity="+") bdict = Dict().tag(config=True) idict = Dict(value_trait=Integer()).tag(config=True) - key_dict = Dict(per_key_traits={'i': Integer(), 'b': Bytes()}).tag(config=True) + key_dict = Dict(per_key_traits={"i": Integer(), "b": Bytes()}).tag(config=True) class MyApp(Application): - name = Unicode('myapp') + name = Unicode("myapp") running = Bool(False, help="Is the app running?").tag(config=True) classes = List([Bar, Foo]) - config_file = Unicode('', help="Load this config file").tag(config=True) + config_file = Unicode("", help="Load this config file").tag(config=True) - warn_tpyo = Unicode("yes the name is wrong on purpose", config=True, - help="Should print a warning if `MyApp.warn-typo=...` command is passed") + warn_tpyo = Unicode( + "yes the name is wrong on purpose", + config=True, + help="Should print a warning if `MyApp.warn-typo=...` command is passed", + ) aliases = {} aliases.update(Application.aliases) - aliases.update({ - ('fooi', 'i') : 'Foo.i', - ('j', 'fooj') : ('Foo.j', "`j` terse help msg"), - 'name' : 'Foo.name', - 'la': 'Foo.la', - 'li': 'Foo.li', - 'tb': 'Bar.tb', - 'D': 'Bar.bdict', - 'enabled' : 'Bar.enabled', - 'enable' : 'Bar.enabled', - 'log-level' : 'Application.log_level', - }) + aliases.update( + { + ("fooi", "i"): "Foo.i", + ("j", "fooj"): ("Foo.j", "`j` terse help msg"), + "name": "Foo.name", + "la": "Foo.la", + "li": "Foo.li", + "tb": "Bar.tb", + "D": "Bar.bdict", + "enabled": "Bar.enabled", + "enable": "Bar.enabled", + "log-level": "Application.log_level", + } + ) flags = {} flags.update(Application.flags) - flags.update({('enable', 'e'): - ({'Bar': {'enabled' : True}}, - "Set Bar.enabled to True"), - ('d', 'disable'): - ({'Bar': {'enabled' : False}}, - "Set Bar.enabled to False"), - 'crit': - ({'Application' : {'log_level' : logging.CRITICAL}}, - "set level=CRITICAL"), - }) + flags.update( + { + ("enable", "e"): ({"Bar": {"enabled": True}}, "Set Bar.enabled to True"), + ("d", "disable"): ({"Bar": {"enabled": False}}, "Set Bar.enabled to False"), + "crit": ({"Application": {"log_level": logging.CRITICAL}}, "set level=CRITICAL"), + } + ) def init_foo(self): self.foo = Foo(parent=self) @@ -127,62 +121,76 @@ class TestApplication(TestCase): app = MyApp(log_level=logging.INFO) handler = logging.StreamHandler(stream) # trigger reconstruction of the log formatter - app.log.handlers = [handler] app.log_format = "%(message)s" app.log_datefmt = "%Y-%m-%d %H:%M" + app.log.handlers = [handler] app.log.info("hello") assert "hello" in stream.getvalue() def test_no_eval_cli_text(self): app = MyApp() - app.initialize(['--Foo.name=1']) + app.initialize(["--Foo.name=1"]) app.init_foo() - assert app.foo.name == '1' + assert app.foo.name == "1" def test_basic(self): app = MyApp() - self.assertEqual(app.name, 'myapp') + self.assertEqual(app.name, "myapp") self.assertEqual(app.running, False) self.assertEqual(app.classes, [MyApp, Bar, Foo]) - self.assertEqual(app.config_file, '') + self.assertEqual(app.config_file, "") def test_mro_discovery(self): app = MyApp() - self.assertSequenceEqual(class_to_names(app._classes_with_config_traits()), - ['Application', 'MyApp', 'Bar', 'Foo']) - self.assertSequenceEqual(class_to_names(app._classes_inc_parents()), - ['Configurable', 'LoggingConfigurable', 'SingletonConfigurable', - 'Application', 'MyApp', 'Bar', 'Foo']) + self.assertSequenceEqual( + class_to_names(app._classes_with_config_traits()), + ["Application", "MyApp", "Bar", "Foo"], + ) + self.assertSequenceEqual( + class_to_names(app._classes_inc_parents()), + [ + "Configurable", + "LoggingConfigurable", + "SingletonConfigurable", + "Application", + "MyApp", + "Bar", + "Foo", + ], + ) - self.assertSequenceEqual(class_to_names(app._classes_with_config_traits([Application])), - ['Application']) - self.assertSequenceEqual(class_to_names(app._classes_inc_parents([Application])), - ['Configurable', 'LoggingConfigurable', 'SingletonConfigurable', - 'Application']) + self.assertSequenceEqual( + class_to_names(app._classes_with_config_traits([Application])), ["Application"] + ) + self.assertSequenceEqual( + class_to_names(app._classes_inc_parents([Application])), + ["Configurable", "LoggingConfigurable", "SingletonConfigurable", "Application"], + ) - self.assertSequenceEqual(class_to_names(app._classes_with_config_traits([Foo])), - ['Foo']) - self.assertSequenceEqual(class_to_names(app._classes_inc_parents([Bar])), - ['Configurable', 'Bar']) + self.assertSequenceEqual(class_to_names(app._classes_with_config_traits([Foo])), ["Foo"]) + self.assertSequenceEqual( + class_to_names(app._classes_inc_parents([Bar])), ["Configurable", "Bar"] + ) class MyApp2(Application): # no defined `classes` attr pass - self.assertSequenceEqual(class_to_names(app._classes_with_config_traits([Foo])), - ['Foo']) - self.assertSequenceEqual(class_to_names(app._classes_inc_parents([Bar])), - ['Configurable', 'Bar']) - + self.assertSequenceEqual(class_to_names(app._classes_with_config_traits([Foo])), ["Foo"]) + self.assertSequenceEqual( + class_to_names(app._classes_inc_parents([Bar])), ["Configurable", "Bar"] + ) def test_config(self): app = MyApp() - app.parse_command_line([ - "--i=10", - "--Foo.j=10", - "--enable=False", - "--log-level=50", - ]) + app.parse_command_line( + [ + "--i=10", + "--Foo.j=10", + "--enable=False", + "--log-level=50", + ] + ) config = app.config print(config) self.assertEqual(config.Foo.i, 10) @@ -192,7 +200,9 @@ class TestApplication(TestCase): def test_config_seq_args(self): app = MyApp() - app.parse_command_line("--li 1 --li 3 --la 1 --tb AB 2 --Foo.la=ab --Bar.aset S1 --Bar.aset S2 --Bar.aset S1".split()) + app.parse_command_line( + "--li 1 --li 3 --la 1 --tb AB 2 --Foo.la=ab --Bar.aset S1 --Bar.aset S2 --Bar.aset S1".split() + ) assert app.extra_args == ["2"] config = app.config assert config.Foo.li == [1, 3] @@ -201,21 +211,21 @@ class TestApplication(TestCase): self.assertEqual(config.Bar.aset, {"S1", "S2"}) app.init_foo() assert app.foo.li == [1, 3] - assert app.foo.la == ['1', 'ab'] + assert app.foo.la == ["1", "ab"] app.init_bar() - self.assertEqual(app.bar.aset, {'S1', 'S2'}) - assert app.bar.tb == ('AB',) + self.assertEqual(app.bar.aset, {"S1", "S2"}) + assert app.bar.tb == ("AB",) def test_config_dict_args(self): app = MyApp() app.parse_command_line( "--Foo.fdict a=1 --Foo.fdict b=b --Foo.fdict c=3 " "--Bar.bdict k=1 -D=a=b -D 22=33 " - "--Bar.idict k=1 --Bar.idict b=2 --Bar.idict c=3 " - .split()) - fdict = {'a': '1', 'b': 'b', 'c': '3'} - bdict = {'k': '1', 'a': 'b', '22': '33'} - idict = {'k': 1, 'b': 2, 'c': 3} + "--Bar.idict k=1 --Bar.idict b=2 --Bar.idict c=3 ".split() + ) + fdict = {"a": "1", "b": "b", "c": "3"} + bdict = {"k": "1", "a": "b", "22": "33"} + idict = {"k": 1, "b": 2, "c": 3} config = app.config assert config.Bar.idict == idict self.assertDictEqual(config.Foo.fdict, fdict) @@ -228,7 +238,7 @@ class TestApplication(TestCase): def test_config_propagation(self): app = MyApp() - app.parse_command_line(["--i=10","--Foo.j=10","--enable=False","--log-level=50"]) + app.parse_command_line(["--i=10", "--Foo.j=10", "--enable=False", "--log-level=50"]) app.init_foo() app.init_bar() self.assertEqual(app.foo.i, 10) @@ -237,64 +247,66 @@ class TestApplication(TestCase): def test_cli_priority(self): """Test that loading config files does not override CLI options""" - name = 'config.py' + name = "config.py" + class TestApp(Application): value = Unicode().tag(config=True) config_file_loaded = Bool().tag(config=True) - aliases = {'v': 'TestApp.value'} + aliases = {"v": "TestApp.value"} + app = TestApp() with TemporaryDirectory() as td: config_file = pjoin(td, name) - with open(config_file, 'w') as f: - f.writelines([ - "c.TestApp.value = 'config file'\n", - "c.TestApp.config_file_loaded = True\n" - ]) + with open(config_file, "w") as f: + f.writelines( + ["c.TestApp.value = 'config file'\n", "c.TestApp.config_file_loaded = True\n"] + ) - app.parse_command_line(['--v=cli']) - assert 'value' in app.config.TestApp - assert app.config.TestApp.value == 'cli' - assert app.value == 'cli' + app.parse_command_line(["--v=cli"]) + assert "value" in app.config.TestApp + assert app.config.TestApp.value == "cli" + assert app.value == "cli" app.load_config_file(name, path=[td]) assert app.config_file_loaded - assert app.config.TestApp.value == 'cli' - assert app.value == 'cli' + assert app.config.TestApp.value == "cli" + assert app.value == "cli" def test_ipython_cli_priority(self): # this test is almost entirely redundant with above, # but we can keep it around in case of subtle issues creeping into # the exact sequence IPython follows. - name = 'config.py' + name = "config.py" + class TestApp(Application): value = Unicode().tag(config=True) config_file_loaded = Bool().tag(config=True) - aliases = {'v': ('TestApp.value', 'some help')} + aliases = {"v": ("TestApp.value", "some help")} + app = TestApp() with TemporaryDirectory() as td: config_file = pjoin(td, name) - with open(config_file, 'w') as f: - f.writelines([ - "c.TestApp.value = 'config file'\n", - "c.TestApp.config_file_loaded = True\n" - ]) + with open(config_file, "w") as f: + f.writelines( + ["c.TestApp.value = 'config file'\n", "c.TestApp.config_file_loaded = True\n"] + ) # follow IPython's config-loading sequence to ensure CLI priority is preserved - app.parse_command_line(['--v=cli']) + app.parse_command_line(["--v=cli"]) # this is where IPython makes a mistake: # it assumes app.config will not be modified, # and storing a reference is storing a copy cli_config = app.config - assert 'value' in app.config.TestApp - assert app.config.TestApp.value == 'cli' - assert app.value == 'cli' + assert "value" in app.config.TestApp + assert app.config.TestApp.value == "cli" + assert app.value == "cli" app.load_config_file(name, path=[td]) assert app.config_file_loaded # enforce cl-opts override config file opts: # this is where IPython makes a mistake: it assumes # that cl_config is a different object, but it isn't. app.update_config(cli_config) - assert app.config.TestApp.value == 'cli' - assert app.value == 'cli' + assert app.config.TestApp.value == "cli" + assert app.value == "cli" def test_cli_allow_none(self): class App(Application): @@ -395,7 +407,6 @@ class TestApplication(TestCase): self.assertIn("warn_typo", stream.getvalue()) self.assertIn("warn_tpyo", stream.getvalue()) - def test_flatten_flags(self): cfg = Config() cfg.MyApp.log_level = logging.WARN @@ -423,28 +434,26 @@ class TestApplication(TestCase): def test_extra_args(self): app = MyApp() - app.parse_command_line(["--Bar.b=5", 'extra', 'args', "--disable"]) + app.parse_command_line(["--Bar.b=5", "extra", "args", "--disable"]) app.init_bar() self.assertEqual(app.bar.enabled, False) self.assertEqual(app.bar.b, 5) - self.assertEqual(app.extra_args, ['extra', 'args']) + self.assertEqual(app.extra_args, ["extra", "args"]) app = MyApp() - app.parse_command_line(["--Bar.b=5", '--', 'extra', "--disable", 'args']) + app.parse_command_line(["--Bar.b=5", "--", "extra", "--disable", "args"]) app.init_bar() self.assertEqual(app.bar.enabled, True) self.assertEqual(app.bar.b, 5) - self.assertEqual(app.extra_args, ['extra', '--disable', 'args']) + self.assertEqual(app.extra_args, ["extra", "--disable", "args"]) app = MyApp() - app.parse_command_line( - ["--disable", "--la", "-", "-", "--Bar.b=1", "--", "-", "extra"] - ) + app.parse_command_line(["--disable", "--la", "-", "-", "--Bar.b=1", "--", "-", "extra"]) self.assertEqual(app.extra_args, ["-", "-", "extra"]) def test_unicode_argv(self): app = MyApp() - app.parse_command_line(['ünîcødé']) + app.parse_command_line(["ünîcødé"]) def test_document_config_option(self): app = MyApp() @@ -452,14 +461,17 @@ class TestApplication(TestCase): def test_generate_config_file(self): app = MyApp() - assert 'The integer b.' in app.generate_config_file() + assert "The integer b." in app.generate_config_file() def test_generate_config_file_classes_to_include(self): class NotInConfig(HasTraits): - from_hidden = Unicode('x', help="""From hidden class - + from_hidden = Unicode( + "x", + help="""From hidden class + Details about from_hidden. - """).tag(config=True) + """, + ).tag(config=True) class NoTraits(Foo, Bar, NotInConfig): pass @@ -469,34 +481,34 @@ class TestApplication(TestCase): conf_txt = app.generate_config_file() print(conf_txt) - self.assertIn('The integer b.', conf_txt) - self.assertIn('# Foo(Configurable)', conf_txt) - self.assertNotIn('# Configurable', conf_txt) - self.assertIn('# NoTraits(Foo, Bar)', conf_txt) + self.assertIn("The integer b.", conf_txt) + self.assertIn("# Foo(Configurable)", conf_txt) + self.assertNotIn("# Configurable", conf_txt) + self.assertIn("# NoTraits(Foo, Bar)", conf_txt) # inherited traits, parent in class list: - self.assertIn('# c.NoTraits.i', conf_txt) - self.assertIn('# c.NoTraits.j', conf_txt) - self.assertIn('# c.NoTraits.n', conf_txt) - self.assertIn('# See also: Foo.j', conf_txt) - self.assertIn('# See also: Bar.b', conf_txt) - self.assertEqual(conf_txt.count('Details about i.'), 1) + self.assertIn("# c.NoTraits.i", conf_txt) + self.assertIn("# c.NoTraits.j", conf_txt) + self.assertIn("# c.NoTraits.n", conf_txt) + self.assertIn("# See also: Foo.j", conf_txt) + self.assertIn("# See also: Bar.b", conf_txt) + self.assertEqual(conf_txt.count("Details about i."), 1) # inherited traits, parent not in class list: self.assertIn("# c.NoTraits.from_hidden", conf_txt) - self.assertNotIn('# See also: NotInConfig.', conf_txt) - self.assertEqual(conf_txt.count('Details about from_hidden.'), 1) + self.assertNotIn("# See also: NotInConfig.", conf_txt) + self.assertEqual(conf_txt.count("Details about from_hidden."), 1) self.assertNotIn("NotInConfig", conf_txt) def test_multi_file(self): app = MyApp() app.log = logging.getLogger() - name = 'config.py' - with TemporaryDirectory('_1') as td1: - with open(pjoin(td1, name), 'w') as f1: + name = "config.py" + with TemporaryDirectory("_1") as td1: + with open(pjoin(td1, name), "w") as f1: f1.write("get_config().MyApp.Bar.b = 1") - with TemporaryDirectory('_2') as td2: - with open(pjoin(td2, name), 'w') as f2: + with TemporaryDirectory("_2") as td2: + with open(pjoin(td2, name), "w") as f2: f2.write("get_config().MyApp.Bar.b = 2") app.load_config_file(name, path=[td2, td1]) app.init_bar() @@ -505,51 +517,47 @@ class TestApplication(TestCase): app.init_bar() self.assertEqual(app.bar.b, 1) - @mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs') + @mark.skipif(not hasattr(TestCase, "assertLogs"), reason="requires TestCase.assertLogs") def test_log_collisions(self): app = MyApp() app.log = logging.getLogger() app.log.setLevel(logging.INFO) - name = 'config' - with TemporaryDirectory('_1') as td: - with open(pjoin(td, name + '.py'), 'w') as f: + name = "config" + with TemporaryDirectory("_1") as td: + with open(pjoin(td, name + ".py"), "w") as f: f.write("get_config().Bar.b = 1") - with open(pjoin(td, name + '.json'), 'w') as f: - json.dump({ - 'Bar': { - 'b': 2 - } - }, f) + with open(pjoin(td, name + ".json"), "w") as f: + json.dump({"Bar": {"b": 2}}, f) with self.assertLogs(app.log, logging.WARNING) as captured: app.load_config_file(name, path=[td]) app.init_bar() assert app.bar.b == 2 - output = '\n'.join(captured.output) - assert 'Collision' in output - assert '1 ignored, using 2' in output - assert pjoin(td, name + '.py') in output - assert pjoin(td, name + '.json') in output + output = "\n".join(captured.output) + assert "Collision" in output + assert "1 ignored, using 2" in output + assert pjoin(td, name + ".py") in output + assert pjoin(td, name + ".json") in output - @mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs') + @mark.skipif(not hasattr(TestCase, "assertLogs"), reason="requires TestCase.assertLogs") def test_log_bad_config(self): app = MyApp() app.log = logging.getLogger() - name = 'config.py' + name = "config.py" with TemporaryDirectory() as td: - with open(pjoin(td, name), 'w') as f: + with open(pjoin(td, name), "w") as f: f.write("syntax error()") with self.assertLogs(app.log, logging.ERROR) as captured: app.load_config_file(name, path=[td]) - output = '\n'.join(captured.output) - self.assertIn('SyntaxError', output) + output = "\n".join(captured.output) + self.assertIn("SyntaxError", output) def test_raise_on_bad_config(self): app = MyApp() app.raise_config_file_errors = True app.log = logging.getLogger() - name = 'config.py' + name = "config.py" with TemporaryDirectory() as td: - with open(pjoin(td, name), 'w') as f: + with open(pjoin(td, name), "w") as f: f.write("syntax error()") with self.assertRaises(SyntaxError): app.load_config_file(name, path=[td]) @@ -557,20 +565,20 @@ class TestApplication(TestCase): def test_subcommands_instanciation(self): """Try all ways to specify how to create sub-apps.""" app = Root.instance() - app.parse_command_line(['sub1']) + app.parse_command_line(["sub1"]) self.assertIsInstance(app.subapp, Sub1) - ## Check parent hierarchy. + # Check parent hierarchy. self.assertIs(app.subapp.parent, app) Root.clear_instance() Sub1.clear_instance() # Otherwise, replaced spuriously and hierarchy check fails. app = Root.instance() - app.parse_command_line(['sub1', 'sub2']) + app.parse_command_line(["sub1", "sub2"]) self.assertIsInstance(app.subapp, Sub1) self.assertIsInstance(app.subapp.subapp, Sub2) - ## Check parent hierarchy. + # Check parent hierarchy. self.assertIs(app.subapp.parent, app) self.assertIs(app.subapp.subapp.parent, app.subapp) @@ -578,24 +586,22 @@ class TestApplication(TestCase): Sub1.clear_instance() # Otherwise, replaced spuriously and hierarchy check fails. app = Root.instance() - app.parse_command_line(['sub1', 'sub3']) + app.parse_command_line(["sub1", "sub3"]) self.assertIsInstance(app.subapp, Sub1) self.assertIsInstance(app.subapp.subapp, Sub3) - self.assertTrue(app.subapp.subapp.flag) # Set by factory. - ## Check parent hierarchy. + self.assertTrue(app.subapp.subapp.flag) # Set by factory. + # Check parent hierarchy. self.assertIs(app.subapp.parent, app) - self.assertIs(app.subapp.subapp.parent, app.subapp) # Set by factory. + self.assertIs(app.subapp.subapp.parent, app.subapp) # Set by factory. def test_loaded_config_files(self): app = MyApp() app.log = logging.getLogger() - name = 'config.py' - with TemporaryDirectory('_1') as td1: + name = "config.py" + with TemporaryDirectory("_1") as td1: config_file = pjoin(td1, name) - with open(config_file, 'w') as f: - f.writelines([ - "c.MyApp.running = True\n" - ]) + with open(config_file, "w") as f: + f.writelines(["c.MyApp.running = True\n"]) app.load_config_file(name, path=[td1]) self.assertEqual(len(app.loaded_config_files), 1) @@ -605,10 +611,8 @@ class TestApplication(TestCase): self.assertEqual(app.running, True) # emulate an app that allows dynamic updates and update config file - with open(config_file, 'w') as f: - f.writelines([ - "c.MyApp.running = False\n" - ]) + with open(config_file, "w") as f: + f.writelines(["c.MyApp.running = False\n"]) # reload and verify update, and that loaded_configs was not increased app.load_config_file(name, path=[td1]) @@ -629,7 +633,6 @@ class TestApplication(TestCase): self.assertEqual(app.running, False) - @mark.skip def test_cli_multi_scalar(caplog): class App(Application): @@ -650,7 +653,7 @@ def test_cli_multi_scalar(caplog): class Root(Application): subcommands = { - 'sub1': ('__tests__.config.tests.test_application.Sub1', 'import string'), + "sub1": ("__tests__.config.tests.test_application.Sub1", "import string"), } @@ -664,27 +667,30 @@ class Sub2(Application): class Sub1(Application): subcommands = { - 'sub2': (Sub2, 'Application class'), - 'sub3': (lambda root: Sub3(parent=root, flag=True), 'factory'), + "sub2": (Sub2, "Application class"), + "sub3": (lambda root: Sub3(parent=root, flag=True), "factory"), } class DeprecatedApp(Application): override_called = False parent_called = False + def _config_changed(self, name, old, new): self.override_called = True + def _capture(*args): self.parent_called = True - with mock.patch.object(self.log, 'debug', _capture): - super(DeprecatedApp, self)._config_changed(name, old, new) + + with mock.patch.object(self.log, "debug", _capture): + super()._config_changed(name, old, new) def test_deprecated_notifier(): app = DeprecatedApp() assert not app.override_called assert not app.parent_called - app.config = Config({'A': {'b': 'c'}}) + app.config = Config({"A": {"b": "c"}}) assert app.override_called assert app.parent_called @@ -698,15 +704,15 @@ def test_help_all_output(): def test_show_config_cli(): - out, err, ec = get_output_error_code([sys.executable, '-m', __name__, '--show-config']) + out, err, ec = get_output_error_code([sys.executable, "-m", __name__, "--show-config"]) assert ec == 0 - assert 'show_config' not in out + assert "show_config" not in out def test_show_config_json_cli(): - out, err, ec = get_output_error_code([sys.executable, '-m', __name__, '--show-config-json']) + out, err, ec = get_output_error_code([sys.executable, "-m", __name__, "--show-config-json"]) assert ec == 0 - assert 'show_config' not in out + assert "show_config" not in out def test_show_config(capsys): @@ -718,9 +724,9 @@ def test_show_config(capsys): app = MyApp(config=cfg, show_config=True) app.start() out, err = capsys.readouterr() - assert 'MyApp' in out - assert 'i = 5' in out - assert 'OtherApp' not in out + assert "MyApp" in out + assert "i = 5" in out + assert "OtherApp" not in out def test_show_config_json(capsys): @@ -736,22 +742,21 @@ def test_show_config_json(capsys): def test_deep_alias(): - from traitlets.config import Application, Configurable from traitlets import Int + from traitlets.config import Application, Configurable class Foo(Configurable): val = Int(default_value=5).tag(config=True) class Bar(Configurable): - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.foo = Foo(parent=self) class TestApp(Application): - name = 'test' + name = "test" - aliases = {'val': 'Bar.Foo.val'} + aliases = {"val": "Bar.Foo.val"} classes = [Foo, Bar] def initialize(self, *args, **kwargs): @@ -759,11 +764,47 @@ def test_deep_alias(): self.bar = Bar(parent=self) app = TestApp() - app.initialize(['--val=10']) + app.initialize(["--val=10"]) assert app.bar.foo.val == 10 assert len(list(app.emit_alias_help())) > 0 -if __name__ == '__main__': +def test_logging_config(tmp_path, capsys): + """We should be able to configure additional log handlers.""" + log_file = tmp_path / "log_file" + app = Application( + logging_config={ + "version": 1, + "handlers": { + "file": { + "class": "logging.FileHandler", + "level": "DEBUG", + "filename": str(log_file), + }, + }, + "loggers": { + "Application": { + "level": "DEBUG", + "handlers": ["console", "file"], + }, + }, + } + ) + # the default "console" handler + our new "file" handler + assert len(app.log.handlers) == 2 + + # log a couple of messages + app.log.info("info") + app.log.warning("warn") + + # test that log messages get written to the file + with open(log_file) as log_handle: + assert log_handle.read() == "info\nwarn\n" + + # test that log messages get written to stderr (default console handler) + assert capsys.readouterr().err == "[Application] WARNING | warn\n" + + +if __name__ == "__main__": # for test_help_output: MyApp.launch_instance() diff --git a/contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py b/contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py index b8d153b53f..00b7db2136 100644 --- a/contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py +++ b/contrib/python/traitlets/py3/traitlets/config/tests/test_configurable.py @@ -14,20 +14,29 @@ from traitlets.config.configurable import ( LoggingConfigurable, SingletonConfigurable, ) +from traitlets.config.loader import Config from traitlets.log import get_logger from traitlets.traitlets import ( - Integer, Float, Unicode, List, Dict, Set, Enum, FuzzyEnum, - CaselessStrEnum, _deprecations_shown, validate, + CaselessStrEnum, + Dict, + Enum, + Float, + FuzzyEnum, + Integer, + List, + Set, + Unicode, + _deprecations_shown, + validate, ) -from traitlets.config.loader import Config - from traitlets.tests._warnings import expected_warnings + class MyConfigurable(Configurable): a = Integer(1, help="The integer a.").tag(config=True) b = Float(1.0, help="The integer b.").tag(config=True) - c = Unicode('no config') + c = Unicode("no config") mc_help = """MyConfigurable(Configurable) options @@ -39,7 +48,7 @@ mc_help = """MyConfigurable(Configurable) options The integer b. Default: 1.0""" -mc_help_inst="""MyConfigurable(Configurable) options +mc_help_inst = """MyConfigurable(Configurable) options ------------------------------------ --MyConfigurable.a=<Integer> The integer a. @@ -52,22 +61,24 @@ mc_help_inst="""MyConfigurable(Configurable) options mc_help = mc_help.replace("<Integer>", "<Int>") mc_help_inst = mc_help_inst.replace("<Integer>", "<Int>") + class Foo(Configurable): a = Integer(0, help="The integer a.").tag(config=True) - b = Unicode('nope').tag(config=True) + b = Unicode("nope").tag(config=True) flist = List([]).tag(config=True) fdict = Dict().tag(config=True) class Bar(Foo): - b = Unicode('gotit', help="The string b.").tag(config=False) + b = Unicode("gotit", help="The string b.").tag(config=False) c = Float(help="The string c.").tag(config=True) - bset = Set([]).tag(config=True, multiplicity='+') - bset_values = Set([2,1,5]).tag(config=True, multiplicity='+') - bdict = Dict().tag(config=True, multiplicity='+') - bdict_values = Dict({1:'a','0':'b',5:'c'}).tag(config=True, multiplicity='+') + bset = Set([]).tag(config=True, multiplicity="+") + bset_values = Set([2, 1, 5]).tag(config=True, multiplicity="+") + bdict = Dict().tag(config=True, multiplicity="+") + bdict_values = Dict({1: "a", "0": "b", 5: "c"}).tag(config=True, multiplicity="+") + -foo_help="""Foo(Configurable) options +foo_help = """Foo(Configurable) options ------------------------- --Foo.a=<Int> The integer a. @@ -79,7 +90,7 @@ foo_help="""Foo(Configurable) options --Foo.flist=<list-item-1>... Default: []""" -bar_help="""Bar(Foo) options +bar_help = """Bar(Foo) options ---------------- --Bar.a=<Int> The integer a. @@ -102,7 +113,6 @@ bar_help="""Bar(Foo) options class TestConfigurable(TestCase): - def test_default(self): c1 = Configurable() c2 = Configurable(config=c1.config) @@ -112,8 +122,8 @@ class TestConfigurable(TestCase): def test_custom(self): config = Config() - config.foo = 'foo' - config.bar = 'bar' + config.foo = "foo" + config.bar = "bar" c1 = Configurable(config=config) c2 = Configurable(config=c1.config) c3 = Configurable(config=c2.config) @@ -142,14 +152,14 @@ class TestConfigurable(TestCase): config = Config() config.Foo.a = 10 config.Foo.b = "wow" - config.Bar.b = 'later' + config.Bar.b = "later" config.Bar.c = 100.0 f = Foo(config=config) - with expected_warnings(['`b` not recognized']): + with expected_warnings(["`b` not recognized"]): b = Bar(config=f.config) self.assertEqual(f.a, 10) - self.assertEqual(f.b, 'wow') - self.assertEqual(b.b, 'gotit') + self.assertEqual(f.b, "wow") + self.assertEqual(b.b, "gotit") self.assertEqual(b.c, 100.0) def test_override1(self): @@ -159,22 +169,22 @@ class TestConfigurable(TestCase): c = MyConfigurable(a=3, config=config) self.assertEqual(c.a, 3) self.assertEqual(c.b, config.MyConfigurable.b) - self.assertEqual(c.c, 'no config') + self.assertEqual(c.c, "no config") def test_override2(self): config = Config() config.Foo.a = 1 - config.Bar.b = 'or' # Up above b is config=False, so this won't do it. + config.Bar.b = "or" # Up above b is config=False, so this won't do it. config.Bar.c = 10.0 - with expected_warnings(['`b` not recognized']): + with expected_warnings(["`b` not recognized"]): c = Bar(config=config) self.assertEqual(c.a, config.Foo.a) - self.assertEqual(c.b, 'gotit') + self.assertEqual(c.b, "gotit") self.assertEqual(c.c, config.Bar.c) - with expected_warnings(['`b` not recognized']): - c = Bar(a=2, b='and', c=20.0, config=config) + with expected_warnings(["`b` not recognized"]): + c = Bar(a=2, b="and", c=20.0, config=config) self.assertEqual(c.a, 2) - self.assertEqual(c.b, 'and') + self.assertEqual(c.b, "and") self.assertEqual(c.c, 20.0) def test_help(self): @@ -188,8 +198,7 @@ class TestConfigurable(TestCase): def test_generated_config_enum_comments(self): class MyConf(Configurable): - an_enum = Enum('Choice1 choice2'.split(), - help="Many choices.").tag(config=True) + an_enum = Enum("Choice1 choice2".split(), help="Many choices.").tag(config=True) help_str = "Many choices." enum_choices_str = "Choices: any of ['Choice1', 'choice2']" @@ -207,9 +216,8 @@ class TestConfigurable(TestCase): self.assertIn(help_str, cls_cfg) self.assertIn(enum_choices_str, cls_cfg) self.assertNotIn(or_none_str, cls_help) - ## Check order of Help-msg <--> Choices sections - self.assertGreater(cls_cfg.index(enum_choices_str), - cls_cfg.index(help_str)) + # Check order of Help-msg <--> Choices sections + self.assertGreater(cls_cfg.index(enum_choices_str), cls_cfg.index(help_str)) rst_help = MyConf.class_config_rst_doc() @@ -218,10 +226,12 @@ class TestConfigurable(TestCase): self.assertNotIn(or_none_str, rst_help) class MyConf2(Configurable): - an_enum = Enum('Choice1 choice2'.split(), - allow_none=True, - default_value='choice2', - help="Many choices.").tag(config=True) + an_enum = Enum( + "Choice1 choice2".split(), + allow_none=True, + default_value="choice2", + help="Many choices.", + ).tag(config=True) defaults_str = "Default: 'choice2'" @@ -231,9 +241,8 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls2_msg) self.assertIn(or_none_str, cls2_msg) self.assertIn(defaults_str, cls2_msg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls2_msg.index(defaults_str), - cls2_msg.index(enum_choices_str)) + # Check order of Default <--> Choices sections + self.assertGreater(cls2_msg.index(defaults_str), cls2_msg.index(enum_choices_str)) cls2_cfg = MyConf2.class_config_section() @@ -241,9 +250,8 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls2_cfg) self.assertIn(or_none_str, cls2_cfg) self.assertIn(defaults_str, cls2_cfg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls2_cfg.index(defaults_str), - cls2_cfg.index(enum_choices_str)) + # Check order of Default <--> Choices sections + self.assertGreater(cls2_cfg.index(defaults_str), cls2_cfg.index(enum_choices_str)) def test_generated_config_strenum_comments(self): help_str = "Many choices." @@ -251,13 +259,14 @@ class TestConfigurable(TestCase): or_none_str = "or None" class MyConf3(Configurable): - an_enum = CaselessStrEnum('Choice1 choice2'.split(), - allow_none=True, - default_value='choice2', - help="Many choices.").tag(config=True) + an_enum = CaselessStrEnum( + "Choice1 choice2".split(), + allow_none=True, + default_value="choice2", + help="Many choices.", + ).tag(config=True) - enum_choices_str = ("Choices: any of ['Choice1', 'choice2'] " - "(case-insensitive)") + enum_choices_str = "Choices: any of ['Choice1', 'choice2'] (case-insensitive)" cls3_msg = MyConf3.class_get_help() @@ -265,9 +274,8 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls3_msg) self.assertIn(or_none_str, cls3_msg) self.assertIn(defaults_str, cls3_msg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls3_msg.index(defaults_str), - cls3_msg.index(enum_choices_str)) + # Check order of Default <--> Choices sections + self.assertGreater(cls3_msg.index(defaults_str), cls3_msg.index(enum_choices_str)) cls3_cfg = MyConf3.class_config_section() @@ -275,18 +283,18 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls3_cfg) self.assertIn(or_none_str, cls3_cfg) self.assertIn(defaults_str, cls3_cfg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls3_cfg.index(defaults_str), - cls3_cfg.index(enum_choices_str)) + # Check order of Default <--> Choices sections + self.assertGreater(cls3_cfg.index(defaults_str), cls3_cfg.index(enum_choices_str)) class MyConf4(Configurable): - an_enum = FuzzyEnum('Choice1 choice2'.split(), - allow_none=True, - default_value='choice2', - help="Many choices.").tag(config=True) + an_enum = FuzzyEnum( + "Choice1 choice2".split(), + allow_none=True, + default_value="choice2", + help="Many choices.", + ).tag(config=True) - enum_choices_str = ("Choices: any case-insensitive prefix " - "of ['Choice1', 'choice2']") + enum_choices_str = "Choices: any case-insensitive prefix of ['Choice1', 'choice2']" cls4_msg = MyConf4.class_get_help() @@ -294,9 +302,8 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls4_msg) self.assertIn(or_none_str, cls4_msg) self.assertIn(defaults_str, cls4_msg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls4_msg.index(defaults_str), - cls4_msg.index(enum_choices_str)) + # Check order of Default <--> Choices sections + self.assertGreater(cls4_msg.index(defaults_str), cls4_msg.index(enum_choices_str)) cls4_cfg = MyConf4.class_config_section() @@ -304,16 +311,15 @@ class TestConfigurable(TestCase): self.assertIn(enum_choices_str, cls4_cfg) self.assertIn(or_none_str, cls4_cfg) self.assertIn(defaults_str, cls4_cfg) - ## Check order of Default <--> Choices sections - self.assertGreater(cls4_cfg.index(defaults_str), - cls4_cfg.index(enum_choices_str)) - + # Check order of Default <--> Choices sections + self.assertGreater(cls4_cfg.index(defaults_str), cls4_cfg.index(enum_choices_str)) class TestSingletonConfigurable(TestCase): - def test_instance(self): - class Foo(SingletonConfigurable): pass + class Foo(SingletonConfigurable): + pass + self.assertEqual(Foo.initialized(), False) foo = Foo.instance() self.assertEqual(Foo.initialized(), True) @@ -321,8 +327,12 @@ class TestSingletonConfigurable(TestCase): self.assertEqual(SingletonConfigurable._instance, None) def test_inheritance(self): - class Bar(SingletonConfigurable): pass - class Bam(Bar): pass + class Bar(SingletonConfigurable): + pass + + class Bam(Bar): + pass + self.assertEqual(Bar.initialized(), False) self.assertEqual(Bam.initialized(), False) bam = Bam.instance() @@ -334,10 +344,13 @@ class TestSingletonConfigurable(TestCase): class TestLoggingConfigurable(TestCase): - def test_parent_logger(self): - class Parent(LoggingConfigurable): pass - class Child(LoggingConfigurable): pass + class Parent(LoggingConfigurable): + pass + + class Child(LoggingConfigurable): + pass + log = get_logger().getChild("TestLoggingConfigurable") parent = Parent(log=log) @@ -351,8 +364,12 @@ class TestLoggingConfigurable(TestCase): self.assertEqual(child.log, log) def test_parent_not_logging_configurable(self): - class Parent(Configurable): pass - class Child(LoggingConfigurable): pass + class Parent(Configurable): + pass + + class Child(LoggingConfigurable): + pass + parent = Parent() child = Child(parent=parent) self.assertEqual(child.log, get_logger()) @@ -361,175 +378,191 @@ class TestLoggingConfigurable(TestCase): class MyParent(Configurable): pass + class MyParent2(MyParent): pass -class TestParentConfigurable(TestCase): +class TestParentConfigurable(TestCase): def test_parent_config(self): - cfg = Config({ - 'MyParent' : { - 'MyConfigurable' : { - 'b' : 2.0, + cfg = Config( + { + "MyParent": { + "MyConfigurable": { + "b": 2.0, + } } } - }) + ) parent = MyParent(config=cfg) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent.MyConfigurable.b) def test_parent_inheritance(self): - cfg = Config({ - 'MyParent' : { - 'MyConfigurable' : { - 'b' : 2.0, + cfg = Config( + { + "MyParent": { + "MyConfigurable": { + "b": 2.0, + } } } - }) + ) parent = MyParent2(config=cfg) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent.MyConfigurable.b) def test_multi_parent(self): - cfg = Config({ - 'MyParent2' : { - 'MyParent' : { - 'MyConfigurable' : { - 'b' : 2.0, - } - }, - # this one shouldn't count - 'MyConfigurable' : { - 'b' : 3.0, - }, + cfg = Config( + { + "MyParent2": { + "MyParent": { + "MyConfigurable": { + "b": 2.0, + } + }, + # this one shouldn't count + "MyConfigurable": { + "b": 3.0, + }, + } } - }) + ) parent2 = MyParent2(config=cfg) parent = MyParent(parent=parent2) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent2.MyParent.MyConfigurable.b) def test_parent_priority(self): - cfg = Config({ - 'MyConfigurable' : { - 'b' : 2.0, - }, - 'MyParent' : { - 'MyConfigurable' : { - 'b' : 3.0, - } - }, - 'MyParent2' : { - 'MyConfigurable' : { - 'b' : 4.0, - } + cfg = Config( + { + "MyConfigurable": { + "b": 2.0, + }, + "MyParent": { + "MyConfigurable": { + "b": 3.0, + } + }, + "MyParent2": { + "MyConfigurable": { + "b": 4.0, + } + }, } - }) + ) parent = MyParent2(config=cfg) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent2.MyConfigurable.b) def test_multi_parent_priority(self): - cfg = Config({ - 'MyConfigurable': { - 'b': 2.0, - }, - 'MyParent': { - 'MyConfigurable': { - 'b': 3.0, + cfg = Config( + { + "MyConfigurable": { + "b": 2.0, }, - }, - 'MyParent2': { - 'MyConfigurable': { - 'b': 4.0, + "MyParent": { + "MyConfigurable": { + "b": 3.0, + }, }, - 'MyParent': { - 'MyConfigurable': { - 'b': 5.0, + "MyParent2": { + "MyConfigurable": { + "b": 4.0, + }, + "MyParent": { + "MyConfigurable": { + "b": 5.0, + }, }, }, - }, - }) + } + ) parent2 = MyParent2(config=cfg) parent = MyParent2(parent=parent2) myc = MyConfigurable(parent=parent) self.assertEqual(myc.b, parent.config.MyParent2.MyParent.MyConfigurable.b) + class Containers(Configurable): lis = List().tag(config=True) + def _lis_default(self): return [-1] s = Set().tag(config=True) + def _s_default(self): - return {'a'} + return {"a"} d = Dict().tag(config=True) + def _d_default(self): - return {'a' : 'b'} + return {"a": "b"} + class TestConfigContainers(TestCase): def test_extend(self): c = Config() c.Containers.lis.extend(list(range(5))) obj = Containers(config=c) - self.assertEqual(obj.lis, list(range(-1,5))) + self.assertEqual(obj.lis, list(range(-1, 5))) def test_insert(self): c = Config() - c.Containers.lis.insert(0, 'a') - c.Containers.lis.insert(1, 'b') + c.Containers.lis.insert(0, "a") + c.Containers.lis.insert(1, "b") obj = Containers(config=c) - self.assertEqual(obj.lis, ['a', 'b', -1]) + self.assertEqual(obj.lis, ["a", "b", -1]) def test_prepend(self): c = Config() - c.Containers.lis.prepend([1,2]) - c.Containers.lis.prepend([2,3]) + c.Containers.lis.prepend([1, 2]) + c.Containers.lis.prepend([2, 3]) obj = Containers(config=c) - self.assertEqual(obj.lis, [2,3,1,2,-1]) + self.assertEqual(obj.lis, [2, 3, 1, 2, -1]) def test_prepend_extend(self): c = Config() - c.Containers.lis.prepend([1,2]) - c.Containers.lis.extend([2,3]) + c.Containers.lis.prepend([1, 2]) + c.Containers.lis.extend([2, 3]) obj = Containers(config=c) - self.assertEqual(obj.lis, [1,2,-1,2,3]) + self.assertEqual(obj.lis, [1, 2, -1, 2, 3]) def test_append_extend(self): c = Config() - c.Containers.lis.append([1,2]) - c.Containers.lis.extend([2,3]) + c.Containers.lis.append([1, 2]) + c.Containers.lis.extend([2, 3]) obj = Containers(config=c) - self.assertEqual(obj.lis, [-1,[1,2],2,3]) + self.assertEqual(obj.lis, [-1, [1, 2], 2, 3]) def test_extend_append(self): c = Config() - c.Containers.lis.extend([2,3]) - c.Containers.lis.append([1,2]) + c.Containers.lis.extend([2, 3]) + c.Containers.lis.append([1, 2]) obj = Containers(config=c) - self.assertEqual(obj.lis, [-1,2,3,[1,2]]) + self.assertEqual(obj.lis, [-1, 2, 3, [1, 2]]) def test_insert_extend(self): c = Config() c.Containers.lis.insert(0, 1) - c.Containers.lis.extend([2,3]) + c.Containers.lis.extend([2, 3]) obj = Containers(config=c) - self.assertEqual(obj.lis, [1,-1,2,3]) + self.assertEqual(obj.lis, [1, -1, 2, 3]) def test_set_update(self): c = Config() - c.Containers.s.update({0,1,2}) + c.Containers.s.update({0, 1, 2}) c.Containers.s.update({3}) obj = Containers(config=c) - self.assertEqual(obj.s, {'a', 0, 1, 2, 3}) + self.assertEqual(obj.s, {"a", 0, 1, 2, 3}) def test_dict_update(self): c = Config() - c.Containers.d.update({'c' : 'd'}) - c.Containers.d.update({'e' : 'f'}) + c.Containers.d.update({"c": "d"}) + c.Containers.d.update({"e": "f"}) obj = Containers(config=c) - self.assertEqual(obj.d, {'a':'b', 'c':'d', 'e':'f'}) + self.assertEqual(obj.d, {"a": "b", "c": "d", "e": "f"}) def test_update_twice(self): c = Config() @@ -562,6 +595,7 @@ class TestConfigContainers(TestCase): class DefaultConfigurable(Configurable): a = Integer().tag(config=True) + def _config_default(self): if SomeSingleton.initialized(): return SomeSingleton.instance().config @@ -581,14 +615,17 @@ class TestConfigContainers(TestCase): def test_config_default_deprecated(self): """Make sure configurables work even with the deprecations in traitlets""" + class SomeSingleton(SingletonConfigurable): pass # reset deprecation limiter _deprecations_shown.clear() with expected_warnings([]): + class DefaultConfigurable(Configurable): a = Integer(config=True) + def _config_default(self): if SomeSingleton.initialized(): return SomeSingleton.instance().config @@ -613,68 +650,67 @@ class TestConfigContainers(TestCase): # - kwargs are set before config # - kwargs have priority over config class A(Configurable): - a = Unicode('default', config=True) - b = Unicode('default', config=True) - c = Unicode('default', config=True) - c_during_config = Unicode('never') - @validate('b') + a = Unicode("default", config=True) + b = Unicode("default", config=True) + c = Unicode("default", config=True) + c_during_config = Unicode("never") + + @validate("b") def _record_c(self, proposal): # setting b from config records c's value at the time self.c_during_config = self.c return proposal.value cfg = Config() - cfg.A.a = 'a-config' - cfg.A.b = 'b-config' - obj = A(a='a-kwarg', c='c-kwarg', config=cfg) - assert obj.a == 'a-kwarg' - assert obj.b == 'b-config' - assert obj.c == 'c-kwarg' - assert obj.c_during_config == 'c-kwarg' + cfg.A.a = "a-config" + cfg.A.b = "b-config" + obj = A(a="a-kwarg", c="c-kwarg", config=cfg) + assert obj.a == "a-kwarg" + assert obj.b == "b-config" + assert obj.c == "c-kwarg" + assert obj.c_during_config == "c-kwarg" class TestLogger(TestCase): - class A(LoggingConfigurable): - foo = Integer(config=True) - bar = Integer(config=True) - baz = Integer(config=True) + foo = Integer(config=True) + bar = Integer(config=True) + baz = Integer(config=True) - @mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs') + @mark.skipif(not hasattr(TestCase, "assertLogs"), reason="requires TestCase.assertLogs") def test_warn_match(self): - logger = logging.getLogger('test_warn_match') - cfg = Config({'A': {'bat': 5}}) + logger = logging.getLogger("test_warn_match") + cfg = Config({"A": {"bat": 5}}) with self.assertLogs(logger, logging.WARNING) as captured: TestLogger.A(config=cfg, log=logger) - output = '\n'.join(captured.output) - self.assertIn('Did you mean one of: `bar, baz`?', output) - self.assertIn('Config option `bat` not recognized by `A`.', output) + output = "\n".join(captured.output) + self.assertIn("Did you mean one of: `bar, baz`?", output) + self.assertIn("Config option `bat` not recognized by `A`.", output) - cfg = Config({'A': {'fool': 5}}) + cfg = Config({"A": {"fool": 5}}) with self.assertLogs(logger, logging.WARNING) as captured: TestLogger.A(config=cfg, log=logger) - output = '\n'.join(captured.output) - self.assertIn('Config option `fool` not recognized by `A`.', output) - self.assertIn('Did you mean `foo`?', output) + output = "\n".join(captured.output) + self.assertIn("Config option `fool` not recognized by `A`.", output) + self.assertIn("Did you mean `foo`?", output) - cfg = Config({'A': {'totally_wrong': 5}}) + cfg = Config({"A": {"totally_wrong": 5}}) with self.assertLogs(logger, logging.WARNING) as captured: TestLogger.A(config=cfg, log=logger) - output = '\n'.join(captured.output) - self.assertIn('Config option `totally_wrong` not recognized by `A`.', output) - self.assertNotIn('Did you mean', output) + output = "\n".join(captured.output) + self.assertIn("Config option `totally_wrong` not recognized by `A`.", output) + self.assertNotIn("Did you mean", output) - def test_logger_adapter(self): - logger = logging.getLogger("test_logger_adapter") - adapter = logging.LoggerAdapter(logger, {"key": "adapted"}) - with self.assertLogs(logger, logging.INFO) as captured: - app = Application(log=adapter, log_level=logging.INFO) - app.log_format = "%(key)s %(message)s" - app.log.info("test message") +def test_logger_adapter(caplog, capsys): + logger = logging.getLogger("Application") + adapter = logging.LoggerAdapter(logger, {"key": "adapted"}) - output = "\n".join(captured.output) - assert "adapted test message" in output + app = Application(log=adapter, log_level=logging.INFO) + app.log_format = "%(key)s %(message)s" + app.log.info("test message") + + assert "adapted test message" in capsys.readouterr().err diff --git a/contrib/python/traitlets/py3/traitlets/config/tests/test_loader.py b/contrib/python/traitlets/py3/traitlets/config/tests/test_loader.py index 103eeeff6d..c26e699106 100644 --- a/contrib/python/traitlets/py3/traitlets/config/tests/test_loader.py +++ b/contrib/python/traitlets/py3/traitlets/config/tests/test_loader.py @@ -1,4 +1,3 @@ -# encoding: utf-8 """Tests for traitlets.config.loader""" # Copyright (c) IPython Development Team. @@ -13,24 +12,17 @@ from unittest import TestCase import pytest +from traitlets import Dict, Integer, List, Tuple, Unicode +from traitlets.config import Configurable from traitlets.config.loader import ( + ArgParseConfigLoader, Config, - LazyConfigValue, - PyFileConfigLoader, JSONFileConfigLoader, KeyValueConfigLoader, - ArgParseConfigLoader, KVArgParseConfigLoader, + LazyConfigValue, + PyFileConfigLoader, ) -from traitlets import ( - List, - Tuple, - Dict, - Unicode, - Integer, -) -from traitlets.config import Configurable - pyfile = """ c = get_config() @@ -70,7 +62,8 @@ json2file = """ """ import logging -log = logging.getLogger('devnull') + +log = logging.getLogger("devnull") log.setLevel(0) @@ -80,11 +73,11 @@ class TestFileCL(TestCase): self.assertEqual(config.b, 20) self.assertEqual(config.Foo.Bar.value, 10) self.assertEqual(config.Foo.Bam.value, list(range(10))) - self.assertEqual(config.D.C.value, 'hi there') + self.assertEqual(config.D.C.value, "hi there") def test_python(self): - fd, fname = mkstemp('.py', prefix='μnïcø∂e') - f = os.fdopen(fd, 'w') + fd, fname = mkstemp(".py", prefix="μnïcø∂e") + f = os.fdopen(fd, "w") f.write(pyfile) f.close() # Unlink the file @@ -93,8 +86,8 @@ class TestFileCL(TestCase): self._check_conf(config) def test_json(self): - fd, fname = mkstemp('.json', prefix='μnïcø∂e') - f = os.fdopen(fd, 'w') + fd, fname = mkstemp(".json", prefix="μnïcø∂e") + f = os.fdopen(fd, "w") f.write(json1file) f.close() # Unlink the file @@ -104,14 +97,14 @@ class TestFileCL(TestCase): def test_context_manager(self): - fd, fname = mkstemp('.json', prefix='μnïcø∂e') - f = os.fdopen(fd, 'w') - f.write('{}') + fd, fname = mkstemp(".json", prefix="μnïcø∂e") + f = os.fdopen(fd, "w") + f.write("{}") f.close() cl = JSONFileConfigLoader(fname, log=log) - value = 'context_manager' + value = "context_manager" with cl as c: c.MyAttr.value = value @@ -119,13 +112,13 @@ class TestFileCL(TestCase): self.assertEqual(cl.config.MyAttr.value, value) # check that another loader does see the change - cl2 = JSONFileConfigLoader(fname, log=log) + _ = JSONFileConfigLoader(fname, log=log) self.assertEqual(cl.config.MyAttr.value, value) def test_json_context_bad_write(self): - fd, fname = mkstemp('.json', prefix='μnïcø∂e') - f = os.fdopen(fd, 'w') - f.write('{}') + fd, fname = mkstemp(".json", prefix="μnïcø∂e") + f = os.fdopen(fd, "w") + f.write("{}") f.close() with JSONFileConfigLoader(fname, log=log) as config: @@ -138,7 +131,7 @@ class TestFileCL(TestCase): loader = JSONFileConfigLoader(fname, log=log) cfg = loader.load_config() assert cfg.A.b == 1 - assert 'cant_json' not in cfg.A + assert "cant_json" not in cfg.A def test_collision(self): a = Config() @@ -150,27 +143,36 @@ class TestFileCL(TestCase): b.A.trait1 = 1 self.assertEqual(a.collisions(b), {}) b.A.trait1 = 0 - self.assertEqual(a.collisions(b), { - 'A': { - 'trait1': "1 ignored, using 0", - } - }) - self.assertEqual(b.collisions(a), { - 'A': { - 'trait1': "0 ignored, using 1", - } - }) + self.assertEqual( + a.collisions(b), + { + "A": { + "trait1": "1 ignored, using 0", + } + }, + ) + self.assertEqual( + b.collisions(a), + { + "A": { + "trait1": "0 ignored, using 1", + } + }, + ) a.A.trait2 = 3 - self.assertEqual(b.collisions(a), { - 'A': { - 'trait1': "0 ignored, using 1", - 'trait2': "2 ignored, using 3", - } - }) + self.assertEqual( + b.collisions(a), + { + "A": { + "trait1": "0 ignored, using 1", + "trait2": "2 ignored, using 3", + } + }, + ) def test_v2raise(self): - fd, fname = mkstemp('.json', prefix='μnïcø∂e') - f = os.fdopen(fd, 'w') + fd, fname = mkstemp(".json", prefix="μnïcø∂e") + f = os.fdopen(fd, "w") f.write(json2file) f.close() # Unlink the file @@ -182,67 +184,66 @@ class TestFileCL(TestCase): def _parse_int_or_str(v): try: return int(v) - except: + except Exception: return str(v) class MyLoader1(ArgParseConfigLoader): def _add_arguments(self, aliases=None, flags=None, classes=None): p = self.parser - p.add_argument('-f', '--foo', dest='Global.foo', type=str) - p.add_argument('-b', dest='MyClass.bar', type=int) - p.add_argument('-n', dest='n', action='store_true') - p.add_argument('Global.bam', type=str) - p.add_argument('--list1', action='append', type=_parse_int_or_str) - p.add_argument('--list2', nargs='+', type=int) + p.add_argument("-f", "--foo", dest="Global.foo", type=str) + p.add_argument("-b", dest="MyClass.bar", type=int) + p.add_argument("-n", dest="n", action="store_true") + p.add_argument("Global.bam", type=str) + p.add_argument("--list1", action="append", type=_parse_int_or_str) + p.add_argument("--list2", nargs="+", type=int) class MyLoader2(ArgParseConfigLoader): def _add_arguments(self, aliases=None, flags=None, classes=None): - subparsers = self.parser.add_subparsers(dest='subparser_name') - subparser1 = subparsers.add_parser('1') - subparser1.add_argument('-x', dest='Global.x') - subparser2 = subparsers.add_parser('2') - subparser2.add_argument('y') + subparsers = self.parser.add_subparsers(dest="subparser_name") + subparser1 = subparsers.add_parser("1") + subparser1.add_argument("-x", dest="Global.x") + subparser2 = subparsers.add_parser("2") + subparser2.add_argument("y") class TestArgParseCL(TestCase): - def test_basic(self): cl = MyLoader1() - config = cl.load_config('-f hi -b 10 -n wow'.split()) - self.assertEqual(config.Global.foo, 'hi') + config = cl.load_config("-f hi -b 10 -n wow".split()) + self.assertEqual(config.Global.foo, "hi") self.assertEqual(config.MyClass.bar, 10) self.assertEqual(config.n, True) - self.assertEqual(config.Global.bam, 'wow') - config = cl.load_config(['wow']) - self.assertEqual(list(config.keys()), ['Global']) - self.assertEqual(list(config.Global.keys()), ['bam']) - self.assertEqual(config.Global.bam, 'wow') + self.assertEqual(config.Global.bam, "wow") + config = cl.load_config(["wow"]) + self.assertEqual(list(config.keys()), ["Global"]) + self.assertEqual(list(config.Global.keys()), ["bam"]) + self.assertEqual(config.Global.bam, "wow") def test_add_arguments(self): cl = MyLoader2() - config = cl.load_config('2 frobble'.split()) - self.assertEqual(config.subparser_name, '2') - self.assertEqual(config.y, 'frobble') - config = cl.load_config('1 -x frobble'.split()) - self.assertEqual(config.subparser_name, '1') - self.assertEqual(config.Global.x, 'frobble') + config = cl.load_config("2 frobble".split()) + self.assertEqual(config.subparser_name, "2") + self.assertEqual(config.y, "frobble") + config = cl.load_config("1 -x frobble".split()) + self.assertEqual(config.subparser_name, "1") + self.assertEqual(config.Global.x, "frobble") def test_argv(self): - cl = MyLoader1(argv='-f hi -b 10 -n wow'.split()) + cl = MyLoader1(argv="-f hi -b 10 -n wow".split()) config = cl.load_config() - self.assertEqual(config.Global.foo, 'hi') + self.assertEqual(config.Global.foo, "hi") self.assertEqual(config.MyClass.bar, 10) self.assertEqual(config.n, True) - self.assertEqual(config.Global.bam, 'wow') + self.assertEqual(config.Global.bam, "wow") def test_list_args(self): cl = MyLoader1() - config = cl.load_config('--list1 1 wow --list2 1 2 3 --list1 B'.split()) - self.assertEqual(list(config.Global.keys()), ['bam']) - self.assertEqual(config.Global.bam, 'wow') - self.assertEqual(config.list1, [1, 'B']) + config = cl.load_config("--list1 1 wow --list2 1 2 3 --list1 B".split()) + self.assertEqual(list(config.Global.keys()), ["bam"]) + self.assertEqual(config.Global.bam, "wow") + self.assertEqual(config.list1, [1, "B"]) self.assertEqual(config.list2, [1, 2, 3]) @@ -272,52 +273,54 @@ class TestKeyValueCL(TestCase): def test_eval(self): cl = self.klass(log=log) - config = cl.load_config('--C.str_trait=all --C.int_trait=5 --C.list_trait=["hello",5]'.split()) + config = cl.load_config( + '--C.str_trait=all --C.int_trait=5 --C.list_trait=["hello",5]'.split() + ) c = C(config=config) - assert c.str_trait == 'all' + assert c.str_trait == "all" assert c.int_trait == 5 assert c.list_trait == ["hello", 5] def test_basic(self): cl = self.klass(log=log) - argv = [ '--' + s[2:] for s in pyfile.split('\n') if s.startswith('c.') ] + argv = ["--" + s[2:] for s in pyfile.split("\n") if s.startswith("c.")] config = cl.load_config(argv) - assert config.a == '10' - assert config.b == '20' - assert config.Foo.Bar.value == '10' + assert config.a == "10" + assert config.b == "20" + assert config.Foo.Bar.value == "10" # non-literal expressions are not evaluated - self.assertEqual(config.Foo.Bam.value, 'list(range(10))') - self.assertEqual(Unicode().from_string(config.D.C.value), 'hi there') + self.assertEqual(config.Foo.Bam.value, "list(range(10))") + self.assertEqual(Unicode().from_string(config.D.C.value), "hi there") def test_expanduser(self): cl = self.klass(log=log) - argv = ['--a=~/1/2/3', '--b=~', '--c=~/', '--d="~/"'] + argv = ["--a=~/1/2/3", "--b=~", "--c=~/", '--d="~/"'] config = cl.load_config(argv) u = Unicode() - self.assertEqual(u.from_string(config.a), os.path.expanduser('~/1/2/3')) - self.assertEqual(u.from_string(config.b), os.path.expanduser('~')) - self.assertEqual(u.from_string(config.c), os.path.expanduser('~/')) - self.assertEqual(u.from_string(config.d), '~/') + self.assertEqual(u.from_string(config.a), os.path.expanduser("~/1/2/3")) + self.assertEqual(u.from_string(config.b), os.path.expanduser("~")) + self.assertEqual(u.from_string(config.c), os.path.expanduser("~/")) + self.assertEqual(u.from_string(config.d), "~/") def test_extra_args(self): cl = self.klass(log=log) - config = cl.load_config(['--a=5', 'b', 'd', '--c=10']) - self.assertEqual(cl.extra_args, ['b', 'd']) - assert config.a == '5' - assert config.c == '10' - config = cl.load_config(['--', '--a=5', '--c=10']) - self.assertEqual(cl.extra_args, ['--a=5', '--c=10']) + config = cl.load_config(["--a=5", "b", "d", "--c=10"]) + self.assertEqual(cl.extra_args, ["b", "d"]) + assert config.a == "5" + assert config.c == "10" + config = cl.load_config(["--", "--a=5", "--c=10"]) + self.assertEqual(cl.extra_args, ["--a=5", "--c=10"]) cl = self.klass(log=log) - config = cl.load_config(['extra', '--a=2', '--c=1', '--', '-']) - self.assertEqual(cl.extra_args, ['extra', '-']) + config = cl.load_config(["extra", "--a=2", "--c=1", "--", "-"]) + self.assertEqual(cl.extra_args, ["extra", "-"]) def test_unicode_args(self): cl = self.klass(log=log) - argv = ['--a=épsîlön'] + argv = ["--a=épsîlön"] config = cl.load_config(argv) print(config, cl.extra_args) - self.assertEqual(config.a, 'épsîlön') + self.assertEqual(config.a, "épsîlön") def test_list_append(self): cl = self.klass(log=log) @@ -351,15 +354,15 @@ class TestKeyValueCL(TestCase): class CBase(Configurable): a = List().tag(config=True) - b = List(Integer()).tag(config=True, multiplicity='*') - c = List().tag(config=True, multiplicity='append') + b = List(Integer()).tag(config=True, multiplicity="*") + c = List().tag(config=True, multiplicity="append") adict = Dict().tag(config=True) class CSub(CBase): d = Tuple().tag(config=True) - e = Tuple().tag(config=True, multiplicity='+') - bdict = Dict().tag(config=True, multiplicity='*') + e = Tuple().tag(config=True, multiplicity="+") + bdict = Dict().tag(config=True, multiplicity="*") class TestArgParseKVCL(TestKeyValueCL): @@ -381,61 +384,63 @@ class TestArgParseKVCL(TestKeyValueCL): def test_unicode_alias(self): cl = self.klass(log=log) - argv = ['--a=épsîlön'] - config = cl.load_config(argv, aliases=dict(a='A.a')) + argv = ["--a=épsîlön"] + config = cl.load_config(argv, aliases=dict(a="A.a")) print(dict(config)) print(cl.extra_args) print(cl.aliases) - self.assertEqual(config.A.a, 'épsîlön') + self.assertEqual(config.A.a, "épsîlön") def test_expanduser2(self): cl = self.klass(log=log) - argv = ['-a', '~/1/2/3', '--b', "'~/1/2/3'"] - config = cl.load_config(argv, aliases=dict(a='A.a', b='A.b')) + argv = ["-a", "~/1/2/3", "--b", "'~/1/2/3'"] + config = cl.load_config(argv, aliases=dict(a="A.a", b="A.b")) class A(Configurable): a = Unicode(config=True) b = Unicode(config=True) a = A(config=config) - self.assertEqual(a.a, os.path.expanduser('~/1/2/3')) - self.assertEqual(a.b, '~/1/2/3') + self.assertEqual(a.a, os.path.expanduser("~/1/2/3")) + self.assertEqual(a.b, "~/1/2/3") def test_eval(self): cl = self.klass(log=log) - argv = ['-c', 'a=5'] - config = cl.load_config(argv, aliases=dict(c='A.c')) + argv = ["-c", "a=5"] + config = cl.load_config(argv, aliases=dict(c="A.c")) self.assertEqual(config.A.c, "a=5") def test_seq_traits(self): cl = self.klass(log=log, classes=(CBase, CSub)) - aliases = {'a3': 'CBase.c', 'a5': 'CSub.e'} - argv = ("--CBase.a A --CBase.a 2 --CBase.b 1 --CBase.b 3 --a3 AA --CBase.c BB " - "--CSub.d 1 --CSub.d BBB --CSub.e 1 --CSub.e=bcd a b c ").split() + aliases = {"a3": "CBase.c", "a5": "CSub.e"} + argv = ( + "--CBase.a A --CBase.a 2 --CBase.b 1 --CBase.b 3 --a3 AA --CBase.c BB " + "--CSub.d 1 --CSub.d BBB --CSub.e 1 --CSub.e=bcd a b c " + ).split() config = cl.load_config(argv, aliases=aliases) assert cl.extra_args == ["a", "b", "c"] - assert config.CBase.a == ['A', '2'] + assert config.CBase.a == ["A", "2"] assert config.CBase.b == [1, 3] - self.assertEqual(config.CBase.c, ['AA', 'BB']) + self.assertEqual(config.CBase.c, ["AA", "BB"]) - assert config.CSub.d == ('1', 'BBB') - assert config.CSub.e == ('1', 'bcd') + assert config.CSub.d == ("1", "BBB") + assert config.CSub.e == ("1", "bcd") def test_seq_traits_single_empty_string(self): - cl = self.klass(log=log, classes=(CBase, )) - aliases = {'seqopt': 'CBase.c'} - argv = ['--seqopt', ''] + cl = self.klass(log=log, classes=(CBase,)) + aliases = {"seqopt": "CBase.c"} + argv = ["--seqopt", ""] config = cl.load_config(argv, aliases=aliases) - self.assertEqual(config.CBase.c, ['']) + self.assertEqual(config.CBase.c, [""]) def test_dict_traits(self): cl = self.klass(log=log, classes=(CBase, CSub)) - aliases = {'D': 'CBase.adict', 'E': 'CSub.bdict'} + aliases = {"D": "CBase.adict", "E": "CSub.bdict"} argv = ["-D", "k1=v1", "-D=k2=2", "-D", "k3=v 3", "-E", "k=v", "-E", "22=222"] config = cl.load_config(argv, aliases=aliases) c = CSub(config=config) - assert c.adict == {'k1': 'v1', 'k2': '2', 'k3': 'v 3'} - assert c.bdict == {'k': 'v', '22': '222'} + assert c.adict == {"k1": "v1", "k2": "2", "k3": "v 3"} + assert c.bdict == {"k": "v", "22": "222"} def test_mixed_seq_positional(self): aliases = {"c": "Class.trait"} @@ -461,22 +466,21 @@ class TestArgParseKVCL(TestKeyValueCL): class TestConfig(TestCase): - def test_setget(self): c = Config() c.a = 10 self.assertEqual(c.a, 10) - self.assertEqual('b' in c, False) + self.assertEqual("b" in c, False) def test_auto_section(self): c = Config() - self.assertNotIn('A', c) - assert not c._has_section('A') + self.assertNotIn("A", c) + assert not c._has_section("A") A = c.A - A.foo = 'hi there' - self.assertIn('A', c) - assert c._has_section('A') - self.assertEqual(c.A.foo, 'hi there') + A.foo = "hi there" + self.assertIn("A", c) + assert c._has_section("A") + self.assertEqual(c.A.foo, "hi there") del c.A self.assertEqual(c.A, Config()) @@ -511,10 +515,10 @@ class TestConfig(TestCase): c1 = Config() c1.Foo.bar = 10 c1.Foo.bam = 30 - c1.a = 'asdf' + c1.a = "asdf" c1.b = range(10) - c1.Test.logger = logging.Logger('test') - c1.Test.get_logger = logging.getLogger('test') + c1.Test.logger = logging.Logger("test") + c1.Test.get_logger = logging.getLogger("test") c2 = copy.deepcopy(c1) self.assertEqual(c1, c2) self.assertTrue(c1 is not c2) @@ -528,33 +532,33 @@ class TestConfig(TestCase): c1.format = "json" def test_fromdict(self): - c1 = Config({'Foo' : {'bar' : 1}}) + c1 = Config({"Foo": {"bar": 1}}) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) def test_fromdictmerge(self): c1 = Config() - c2 = Config({'Foo' : {'bar' : 1}}) + c2 = Config({"Foo": {"bar": 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) def test_fromdictmerge2(self): - c1 = Config({'Foo' : {'baz' : 2}}) - c2 = Config({'Foo' : {'bar' : 1}}) + c1 = Config({"Foo": {"baz": 2}}) + c2 = Config({"Foo": {"bar": 1}}) c1.merge(c2) self.assertEqual(c1.Foo.__class__, Config) self.assertEqual(c1.Foo.bar, 1) self.assertEqual(c1.Foo.baz, 2) - self.assertNotIn('baz', c2.Foo) + self.assertNotIn("baz", c2.Foo) def test_contains(self): - c1 = Config({'Foo' : {'baz' : 2}}) - c2 = Config({'Foo' : {'bar' : 1}}) - self.assertIn('Foo', c1) - self.assertIn('Foo.baz', c1) - self.assertIn('Foo.bar', c2) - self.assertNotIn('Foo.bar', c1) + c1 = Config({"Foo": {"baz": 2}}) + c2 = Config({"Foo": {"bar": 1}}) + self.assertIn("Foo", c1) + self.assertIn("Foo.baz", c1) + self.assertIn("Foo.bar", c2) + self.assertNotIn("Foo.bar", c1) def test_pickle_config(self): cfg = Config() @@ -565,53 +569,52 @@ class TestConfig(TestCase): def test_getattr_section(self): cfg = Config() - self.assertNotIn('Foo', cfg) + self.assertNotIn("Foo", cfg) Foo = cfg.Foo assert isinstance(Foo, Config) - self.assertIn('Foo', cfg) + self.assertIn("Foo", cfg) def test_getitem_section(self): cfg = Config() - self.assertNotIn('Foo', cfg) - Foo = cfg['Foo'] + self.assertNotIn("Foo", cfg) + Foo = cfg["Foo"] assert isinstance(Foo, Config) - self.assertIn('Foo', cfg) + self.assertIn("Foo", cfg) def test_getattr_not_section(self): cfg = Config() - self.assertNotIn('foo', cfg) + self.assertNotIn("foo", cfg) foo = cfg.foo assert isinstance(foo, LazyConfigValue) - self.assertIn('foo', cfg) + self.assertIn("foo", cfg) def test_getattr_private_missing(self): cfg = Config() - self.assertNotIn('_repr_html_', cfg) + self.assertNotIn("_repr_html_", cfg) with self.assertRaises(AttributeError): _ = cfg._repr_html_ - self.assertNotIn('_repr_html_', cfg) + self.assertNotIn("_repr_html_", cfg) self.assertEqual(len(cfg), 0) def test_lazy_config_repr(self): cfg = Config() cfg.Class.lazy.append(1) cfg_repr = repr(cfg) - assert '<LazyConfigValue' in cfg_repr + assert "<LazyConfigValue" in cfg_repr assert "extend" in cfg_repr assert " [1]}>" in cfg_repr - assert 'value=' not in cfg_repr + assert "value=" not in cfg_repr cfg.Class.lazy.get_value([0]) repr2 = repr(cfg) - assert repr([0,1]) in repr2 - assert 'value=' in repr2 - + assert repr([0, 1]) in repr2 + assert "value=" in repr2 def test_getitem_not_section(self): cfg = Config() - self.assertNotIn('foo', cfg) - foo = cfg['foo'] + self.assertNotIn("foo", cfg) + foo = cfg["foo"] assert isinstance(foo, LazyConfigValue) - self.assertIn('foo', cfg) + self.assertIn("foo", cfg) def test_merge_no_copies(self): c = Config() @@ -623,7 +626,6 @@ class TestConfig(TestCase): self.assertEqual(c.Foo.trait, [1]) self.assertEqual(c2.Foo.trait, [1]) - def test_merge_multi_lazy(self): """ With multiple config files (systemwide and users), we want compounding. @@ -641,10 +643,8 @@ class TestConfig(TestCase): c.merge(c1) c.merge(c2) - self.assertEqual(c.Foo.trait, [1,2] ) - + self.assertEqual(c.Foo.trait, [1, 2]) - def test_merge_multi_lazyII(self): """ With multiple config files (systemwide and users), we want compounding. @@ -661,7 +661,7 @@ class TestConfig(TestCase): c.merge(c1) c.merge(c2) - self.assertEqual(c.Foo.trait._extend, [1,2] ) + self.assertEqual(c.Foo.trait._extend, [1, 2]) def test_merge_multi_lazy_III(self): """ @@ -679,7 +679,7 @@ class TestConfig(TestCase): c.merge(c1) c.merge(c2) - self.assertEqual(c.Foo.trait, [0, 1] ) + self.assertEqual(c.Foo.trait, [0, 1]) def test_merge_multi_lazy_IV(self): """ diff --git a/contrib/python/traitlets/py3/traitlets/log.py b/contrib/python/traitlets/py3/traitlets/log.py index af86b325f5..016529fcac 100644 --- a/contrib/python/traitlets/py3/traitlets/log.py +++ b/contrib/python/traitlets/py3/traitlets/log.py @@ -7,6 +7,7 @@ import logging _logger = None + def get_logger(): """Grab the global logger instance. @@ -17,10 +18,11 @@ def get_logger(): if _logger is None: from .config import Application + if Application.initialized(): _logger = Application.instance().log else: - _logger = logging.getLogger('traitlets') + _logger = logging.getLogger("traitlets") # Add a NullHandler to silence warnings about not being # initialized, per best practice for libraries. _logger.addHandler(logging.NullHandler()) diff --git a/contrib/python/traitlets/py3/traitlets/py.typed b/contrib/python/traitlets/py3/traitlets/py.typed new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/contrib/python/traitlets/py3/traitlets/py.typed diff --git a/contrib/python/traitlets/py3/traitlets/tests/_warnings.py b/contrib/python/traitlets/py3/traitlets/tests/_warnings.py index 05e916806f..e3c3a0ac6d 100644 --- a/contrib/python/traitlets/py3/traitlets/tests/_warnings.py +++ b/contrib/python/traitlets/py3/traitlets/tests/_warnings.py @@ -1,7 +1,7 @@ # From scikit-image: https://github.com/scikit-image/scikit-image/blob/c2f8c4ab123ebe5f7b827bc495625a32bb225c10/skimage/_shared/_warnings.py # Licensed under modified BSD license -__all__ = ['all_warnings', 'expected_warnings'] +__all__ = ["all_warnings", "expected_warnings"] import inspect import os @@ -21,16 +21,19 @@ def all_warnings(): >>> import warnings >>> def foo(): ... warnings.warn(RuntimeWarning("bar")) + We raise the warning once, while the warning filter is set to "once". Hereafter, the warning is invisible, even with custom filters: >>> with warnings.catch_warnings(): ... warnings.simplefilter('once') ... foo() + We can now run ``foo()`` without a warning being raised: - >>> from numpy.testing import assert_warns - >>> foo() + >>> from numpy.testing import assert_warns # doctest: +SKIP + >>> foo() # doctest: +SKIP + To catch the warning, we call in the help of ``all_warnings``: - >>> with all_warnings(): + >>> with all_warnings(): # doctest: +SKIP ... assert_warns(RuntimeWarning, foo) """ @@ -46,17 +49,18 @@ def all_warnings(): frame = inspect.currentframe() if frame: for f in inspect.getouterframes(frame): - f[0].f_locals['__warningregistry__'] = {} + f[0].f_locals["__warningregistry__"] = {} del frame - for mod_name, mod in list(sys.modules.items()): + for _, mod in list(sys.modules.items()): try: mod.__warningregistry__.clear() except AttributeError: pass - with warnings.catch_warnings(record=True) as w, \ - mock.patch.dict(os.environ, {'TRAITLETS_ALL_DEPRECATIONS': '1'}): + with warnings.catch_warnings(record=True) as w, mock.patch.dict( + os.environ, {"TRAITLETS_ALL_DEPRECATIONS": "1"} + ): warnings.simplefilter("always") yield w @@ -72,18 +76,18 @@ def expected_warnings(matching): Examples -------- - >>> from skimage import data, img_as_ubyte, img_as_float - >>> with expected_warnings(["precision loss"]): - ... d = img_as_ubyte(img_as_float(data.coins())) + >>> from skimage import data, img_as_ubyte, img_as_float # doctest: +SKIP + >>> with expected_warnings(["precision loss"]): # doctest: +SKIP + ... d = img_as_ubyte(img_as_float(data.coins())) # doctest: +SKIP Notes ----- Uses `all_warnings` to ensure all warnings are raised. Upon exiting, it checks the recorded warnings for the desired matching - pattern(s). + pattern(s). Raises a ValueError if any match was not found or an unexpected - warning was raised. - Allows for three types of behaviors: "and", "or", and "optional" matches. + warning was raised. + Allows for three types of behaviors: "and", "or", and "optional" matches. This is done to accomodate different build enviroments or loop conditions that may produce different warnings. The behaviors can be combined. If you pass multiple patterns, you get an orderless "and", where all of the @@ -95,7 +99,7 @@ def expected_warnings(matching): # enter context yield w # exited user context, check the recorded warnings - remaining = [m for m in matching if not r'\A\Z' in m.split('|')] + remaining = [m for m in matching if r"\A\Z" not in m.split("|")] for warn in w: found = False for match in matching: @@ -104,7 +108,7 @@ def expected_warnings(matching): if match in remaining: remaining.remove(match) if not found: - raise ValueError('Unexpected warning: %s' % str(warn.message)) + raise ValueError("Unexpected warning: %s" % str(warn.message)) if len(remaining) > 0: - msg = 'No warning raised matching:\n%s' % '\n'.join(remaining) + msg = "No warning raised matching:\n%s" % "\n".join(remaining) raise ValueError(msg) diff --git a/contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py b/contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py index e42dbc01d0..4b145afd02 100644 --- a/contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py +++ b/contrib/python/traitlets/py3/traitlets/tests/test_traitlets.py @@ -1,4 +1,3 @@ -# encoding: utf-8 """Tests for traitlets.traitlets.""" # Copyright (c) IPython Development Team. @@ -13,85 +12,85 @@ from unittest import TestCase import pytest -from traitlets.tests._warnings import expected_warnings from traitlets import ( - HasTraits, - MetaHasTraits, - TraitType, + All, Any, + BaseDescriptor, Bool, + Bytes, + Callable, CBytes, - Dict, - Enum, - Int, + CFloat, CInt, - Long, CLong, - Integer, - Float, - CFloat, Complex, - Bytes, - Unicode, - TraitError, - Union, - Callable, - All, - Undefined, - Set, - Type, - This, + CRegExp, + CUnicode, + Dict, + DottedObjectName, + Enum, + Float, + ForwardDeclaredInstance, + ForwardDeclaredType, + HasDescriptors, + HasTraits, Instance, - TCPAddress, + Int, + Integer, List, + Long, + MetaHasTraits, + ObjectName, Set, + TCPAddress, + This, + TraitError, + TraitType, Tuple, - ObjectName, - DottedObjectName, - CRegExp, - link, + Type, + Undefined, + Unicode, + Union, + default, directional_link, - ForwardDeclaredType, - ForwardDeclaredInstance, - validate, + link, observe, - default, observe_compat, - BaseDescriptor, - HasDescriptors, - CUnicode, + validate, ) from traitlets.utils import cast_unicode +from traitlets.tests._warnings import expected_warnings + def change_dict(*ordered_values): - change_names = ('name', 'old', 'new', 'owner', 'type') + change_names = ("name", "old", "new", "owner", "type") return dict(zip(change_names, ordered_values)) -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Helper classes for testing -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class HasTraitsStub(HasTraits): - def notify_change(self, change): - self._notify_name = change['name'] - self._notify_old = change['old'] - self._notify_new = change['new'] - self._notify_type = change['type'] + self._notify_name = change["name"] + self._notify_old = change["old"] + self._notify_new = change["new"] + self._notify_type = change["type"] -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Test classes -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class TestTraitType(TestCase): - def test_get_undefined(self): class A(HasTraits): a = TraitType + a = A() assert a.a is Undefined @@ -102,7 +101,7 @@ class TestTraitType(TestCase): a = A() a.a = 10 self.assertEqual(a.a, 10) - self.assertEqual(a._notify_name, 'a') + self.assertEqual(a._notify_name, "a") self.assertEqual(a._notify_old, Undefined) self.assertEqual(a._notify_new, 10) @@ -110,6 +109,7 @@ class TestTraitType(TestCase): class MyTT(TraitType): def validate(self, inst, value): return -1 + class A(HasTraitsStub): tt = MyTT @@ -123,35 +123,43 @@ class TestTraitType(TestCase): if isinstance(value, int): return value self.error(obj, value) + class A(HasTraits): tt = MyIntTT(10) + a = A() self.assertEqual(a.tt, 10) # Defaults are validated when the HasTraits is instantiated class B(HasTraits): - tt = MyIntTT('bad default') - self.assertRaises(TraitError, getattr, B(), 'tt') + tt = MyIntTT("bad default") + + self.assertRaises(TraitError, getattr, B(), "tt") def test_info(self): class A(HasTraits): tt = TraitType + a = A() - self.assertEqual(A.tt.info(), 'any value') + self.assertEqual(A.tt.info(), "any value") def test_error(self): class A(HasTraits): tt = TraitType() + a = A() self.assertRaises(TraitError, A.tt.error, a, 10) def test_deprecated_dynamic_initializer(self): class A(HasTraits): x = Int(10) + def _x_default(self): return 11 + class B(A): x = Int(20) + class C(A): def _x_default(self): return 21 @@ -159,36 +167,38 @@ class TestTraitType(TestCase): a = A() self.assertEqual(a._trait_values, {}) self.assertEqual(a.x, 11) - self.assertEqual(a._trait_values, {'x': 11}) + self.assertEqual(a._trait_values, {"x": 11}) b = B() self.assertEqual(b.x, 20) - self.assertEqual(b._trait_values, {'x': 20}) + self.assertEqual(b._trait_values, {"x": 20}) c = C() self.assertEqual(c._trait_values, {}) self.assertEqual(c.x, 21) - self.assertEqual(c._trait_values, {'x': 21}) + self.assertEqual(c._trait_values, {"x": 21}) # Ensure that the base class remains unmolested when the _default # initializer gets overridden in a subclass. a = A() c = C() self.assertEqual(a._trait_values, {}) self.assertEqual(a.x, 11) - self.assertEqual(a._trait_values, {'x': 11}) + self.assertEqual(a._trait_values, {"x": 11}) def test_deprecated_method_warnings(self): with expected_warnings([]): + class ShouldntWarn(HasTraits): x = Integer() - @default('x') + + @default("x") def _x_default(self): return 10 - @validate('x') + @validate("x") def _x_validate(self, proposal): return proposal.value - @observe('x') + @observe("x") def _x_changed(self, change): pass @@ -197,7 +207,8 @@ class TestTraitType(TestCase): assert obj.x == 5 - with expected_warnings(['@validate', '@observe']) as w: + with expected_warnings(["@validate", "@observe"]) as w: + class ShouldWarn(HasTraits): x = Integer() @@ -216,11 +227,10 @@ class TestTraitType(TestCase): assert obj.x == 5 def test_dynamic_initializer(self): - class A(HasTraits): x = Int(10) - @default('x') + @default("x") def _default_x(self): return 11 @@ -228,114 +238,124 @@ class TestTraitType(TestCase): x = Int(20) class C(A): - - @default('x') + @default("x") def _default_x(self): return 21 a = A() self.assertEqual(a._trait_values, {}) self.assertEqual(a.x, 11) - self.assertEqual(a._trait_values, {'x': 11}) + self.assertEqual(a._trait_values, {"x": 11}) b = B() self.assertEqual(b.x, 20) - self.assertEqual(b._trait_values, {'x': 20}) + self.assertEqual(b._trait_values, {"x": 20}) c = C() self.assertEqual(c._trait_values, {}) self.assertEqual(c.x, 21) - self.assertEqual(c._trait_values, {'x': 21}) + self.assertEqual(c._trait_values, {"x": 21}) # Ensure that the base class remains unmolested when the _default # initializer gets overridden in a subclass. a = A() c = C() self.assertEqual(a._trait_values, {}) self.assertEqual(a.x, 11) - self.assertEqual(a._trait_values, {'x': 11}) + self.assertEqual(a._trait_values, {"x": 11}) def test_tag_metadata(self): class MyIntTT(TraitType): - metadata = {'a': 1, 'b': 2} + metadata = {"a": 1, "b": 2} + a = MyIntTT(10).tag(b=3, c=4) - self.assertEqual(a.metadata, {'a': 1, 'b': 3, 'c': 4}) + self.assertEqual(a.metadata, {"a": 1, "b": 3, "c": 4}) def test_metadata_localized_instance(self): class MyIntTT(TraitType): - metadata = {'a': 1, 'b': 2} + metadata = {"a": 1, "b": 2} + a = MyIntTT(10) b = MyIntTT(10) - a.metadata['c'] = 3 + a.metadata["c"] = 3 # make sure that changing a's metadata didn't change b's metadata - self.assertNotIn('c', b.metadata) + self.assertNotIn("c", b.metadata) def test_union_metadata(self): class Foo(HasTraits): - bar = (Int().tag(ta=1) | Dict().tag(ta=2, ti='b')).tag(ti='a') + bar = (Int().tag(ta=1) | Dict().tag(ta=2, ti="b")).tag(ti="a") + foo = Foo() # At this point, no value has been set for bar, so value-specific # is not set. - self.assertEqual(foo.trait_metadata('bar', 'ta'), None) - self.assertEqual(foo.trait_metadata('bar', 'ti'), 'a') + self.assertEqual(foo.trait_metadata("bar", "ta"), None) + self.assertEqual(foo.trait_metadata("bar", "ti"), "a") foo.bar = {} - self.assertEqual(foo.trait_metadata('bar', 'ta'), 2) - self.assertEqual(foo.trait_metadata('bar', 'ti'), 'b') + self.assertEqual(foo.trait_metadata("bar", "ta"), 2) + self.assertEqual(foo.trait_metadata("bar", "ti"), "b") foo.bar = 1 - self.assertEqual(foo.trait_metadata('bar', 'ta'), 1) - self.assertEqual(foo.trait_metadata('bar', 'ti'), 'a') + self.assertEqual(foo.trait_metadata("bar", "ta"), 1) + self.assertEqual(foo.trait_metadata("bar", "ti"), "a") def test_union_default_value(self): class Foo(HasTraits): bar = Union([Dict(), Int()], default_value=1) + foo = Foo() self.assertEqual(foo.bar, 1) def test_union_validation_priority(self): class Foo(HasTraits): bar = Union([CInt(), Unicode()]) + foo = Foo() - foo.bar = '1' + foo.bar = "1" # validation in order of the TraitTypes given self.assertEqual(foo.bar, 1) def test_union_trait_default_value(self): class Foo(HasTraits): bar = Union([Dict(), Int()]) + self.assertEqual(Foo().bar, {}) def test_deprecated_metadata_access(self): class MyIntTT(TraitType): - metadata = {'a': 1, 'b': 2} + metadata = {"a": 1, "b": 2} + a = MyIntTT(10) - with expected_warnings(["use the instance .metadata dictionary directly"]*2): - a.set_metadata('key', 'value') - v = a.get_metadata('key') - self.assertEqual(v, 'value') - with expected_warnings(["use the instance .help string directly"]*2): - a.set_metadata('help', 'some help') - v = a.get_metadata('help') - self.assertEqual(v, 'some help') + with expected_warnings(["use the instance .metadata dictionary directly"] * 2): + a.set_metadata("key", "value") + v = a.get_metadata("key") + self.assertEqual(v, "value") + with expected_warnings(["use the instance .help string directly"] * 2): + a.set_metadata("help", "some help") + v = a.get_metadata("help") + self.assertEqual(v, "some help") def test_trait_types_deprecated(self): with expected_warnings(["Traits should be given as instances"]): + class C(HasTraits): t = Int def test_trait_types_list_deprecated(self): with expected_warnings(["Traits should be given as instances"]): + class C(HasTraits): t = List(Int) def test_trait_types_tuple_deprecated(self): with expected_warnings(["Traits should be given as instances"]): + class C(HasTraits): t = Tuple(Int) def test_trait_types_dict_deprecated(self): with expected_warnings(["Traits should be given as instances"]): + class C(HasTraits): t = Dict(Int) -class TestHasDescriptorsMeta(TestCase): +class TestHasDescriptorsMeta(TestCase): def test_metaclass(self): self.assertEqual(type(HasTraits), MetaHasTraits) @@ -344,59 +364,59 @@ class TestHasDescriptorsMeta(TestCase): a = A() self.assertEqual(type(a.__class__), MetaHasTraits) - self.assertEqual(a.a,0) + self.assertEqual(a.a, 0) a.a = 10 - self.assertEqual(a.a,10) + self.assertEqual(a.a, 10) class B(HasTraits): b = Int() b = B() - self.assertEqual(b.b,0) + self.assertEqual(b.b, 0) b.b = 10 - self.assertEqual(b.b,10) + self.assertEqual(b.b, 10) class C(HasTraits): c = Int(30) c = C() - self.assertEqual(c.c,30) + self.assertEqual(c.c, 30) c.c = 10 - self.assertEqual(c.c,10) + self.assertEqual(c.c, 10) def test_this_class(self): class A(HasTraits): t = This() tt = This() + class B(A): tt = This() ttt = This() + self.assertEqual(A.t.this_class, A) self.assertEqual(B.t.this_class, A) self.assertEqual(B.tt.this_class, B) self.assertEqual(B.ttt.this_class, B) -class TestHasDescriptors(TestCase): +class TestHasDescriptors(TestCase): def test_setup_instance(self): - class FooDescriptor(BaseDescriptor): - def instance_init(self, inst): - foo = inst.foo # instance should have the attr + foo = inst.foo # instance should have the attr class HasFooDescriptors(HasDescriptors): fd = FooDescriptor() def setup_instance(self, *args, **kwargs): - self.foo = kwargs.get('foo', None) - super(HasFooDescriptors, self).setup_instance(*args, **kwargs) + self.foo = kwargs.get("foo", None) + super().setup_instance(*args, **kwargs) - hfd = HasFooDescriptors(foo='bar') + hfd = HasFooDescriptors(foo="bar") -class TestHasTraitsNotify(TestCase): +class TestHasTraitsNotify(TestCase): def setUp(self): self._notify1 = [] self._notify2 = [] @@ -408,7 +428,6 @@ class TestHasTraitsNotify(TestCase): self._notify2.append((name, old, new)) def test_notify_all(self): - class A(HasTraits): a = Int() b = Float() @@ -416,37 +435,35 @@ class TestHasTraitsNotify(TestCase): a = A() a.on_trait_change(self.notify1) a.a = 0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.b = 0.0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.a = 10 - self.assertTrue(('a',0,10) in self._notify1) + self.assertTrue(("a", 0, 10) in self._notify1) a.b = 10.0 - self.assertTrue(('b',0.0,10.0) in self._notify1) - self.assertRaises(TraitError,setattr,a,'a','bad string') - self.assertRaises(TraitError,setattr,a,'b','bad string') + self.assertTrue(("b", 0.0, 10.0) in self._notify1) + self.assertRaises(TraitError, setattr, a, "a", "bad string") + self.assertRaises(TraitError, setattr, a, "b", "bad string") self._notify1 = [] - a.on_trait_change(self.notify1,remove=True) + a.on_trait_change(self.notify1, remove=True) a.a = 20 a.b = 20.0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) def test_notify_one(self): - class A(HasTraits): a = Int() b = Float() a = A() - a.on_trait_change(self.notify1, 'a') + a.on_trait_change(self.notify1, "a") a.a = 0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.a = 10 - self.assertTrue(('a',0,10) in self._notify1) - self.assertRaises(TraitError,setattr,a,'a','bad string') + self.assertTrue(("a", 0, 10) in self._notify1) + self.assertRaises(TraitError, setattr, a, "a", "bad string") def test_subclass(self): - class A(HasTraits): a = Int() @@ -454,15 +471,14 @@ class TestHasTraitsNotify(TestCase): b = Float() b = B() - self.assertEqual(b.a,0) - self.assertEqual(b.b,0.0) + self.assertEqual(b.a, 0) + self.assertEqual(b.b, 0.0) b.a = 100 b.b = 100.0 - self.assertEqual(b.a,100) - self.assertEqual(b.b,100.0) + self.assertEqual(b.a, 100) + self.assertEqual(b.b, 100.0) def test_notify_subclass(self): - class A(HasTraits): a = Int() @@ -470,54 +486,58 @@ class TestHasTraitsNotify(TestCase): b = Float() b = B() - b.on_trait_change(self.notify1, 'a') - b.on_trait_change(self.notify2, 'b') + b.on_trait_change(self.notify1, "a") + b.on_trait_change(self.notify2, "b") b.a = 0 b.b = 0.0 - self.assertEqual(len(self._notify1),0) - self.assertEqual(len(self._notify2),0) + self.assertEqual(len(self._notify1), 0) + self.assertEqual(len(self._notify2), 0) b.a = 10 b.b = 10.0 - self.assertTrue(('a',0,10) in self._notify1) - self.assertTrue(('b',0.0,10.0) in self._notify2) + self.assertTrue(("a", 0, 10) in self._notify1) + self.assertTrue(("b", 0.0, 10.0) in self._notify2) def test_static_notify(self): - class A(HasTraits): a = Int() _notify1 = [] + def _a_changed(self, name, old, new): self._notify1.append((name, old, new)) a = A() a.a = 0 # This is broken!!! - self.assertEqual(len(a._notify1),0) + self.assertEqual(len(a._notify1), 0) a.a = 10 - self.assertTrue(('a',0,10) in a._notify1) + self.assertTrue(("a", 0, 10) in a._notify1) class B(A): b = Float() _notify2 = [] + def _b_changed(self, name, old, new): self._notify2.append((name, old, new)) b = B() b.a = 10 b.b = 10.0 - self.assertTrue(('a',0,10) in b._notify1) - self.assertTrue(('b',0.0,10.0) in b._notify2) + self.assertTrue(("a", 0, 10) in b._notify1) + self.assertTrue(("b", 0.0, 10.0) in b._notify2) def test_notify_args(self): - def callback0(): self.cb = () + def callback1(name): self.cb = (name,) + def callback2(name, new): self.cb = (name, new) + def callback3(name, old, new): self.cb = (name, old, new) + def callback4(name, old, new, obj): self.cb = (name, old, new, obj) @@ -525,45 +545,44 @@ class TestHasTraitsNotify(TestCase): a = Int() a = A() - a.on_trait_change(callback0, 'a') + a.on_trait_change(callback0, "a") a.a = 10 - self.assertEqual(self.cb,()) - a.on_trait_change(callback0, 'a', remove=True) + self.assertEqual(self.cb, ()) + a.on_trait_change(callback0, "a", remove=True) - a.on_trait_change(callback1, 'a') + a.on_trait_change(callback1, "a") a.a = 100 - self.assertEqual(self.cb,('a',)) - a.on_trait_change(callback1, 'a', remove=True) + self.assertEqual(self.cb, ("a",)) + a.on_trait_change(callback1, "a", remove=True) - a.on_trait_change(callback2, 'a') + a.on_trait_change(callback2, "a") a.a = 1000 - self.assertEqual(self.cb,('a',1000)) - a.on_trait_change(callback2, 'a', remove=True) + self.assertEqual(self.cb, ("a", 1000)) + a.on_trait_change(callback2, "a", remove=True) - a.on_trait_change(callback3, 'a') + a.on_trait_change(callback3, "a") a.a = 10000 - self.assertEqual(self.cb,('a',1000,10000)) - a.on_trait_change(callback3, 'a', remove=True) + self.assertEqual(self.cb, ("a", 1000, 10000)) + a.on_trait_change(callback3, "a", remove=True) - a.on_trait_change(callback4, 'a') + a.on_trait_change(callback4, "a") a.a = 100000 - self.assertEqual(self.cb,('a',10000,100000,a)) - self.assertEqual(len(a._trait_notifiers['a']['change']), 1) - a.on_trait_change(callback4, 'a', remove=True) + self.assertEqual(self.cb, ("a", 10000, 100000, a)) + self.assertEqual(len(a._trait_notifiers["a"]["change"]), 1) + a.on_trait_change(callback4, "a", remove=True) - self.assertEqual(len(a._trait_notifiers['a']['change']), 0) + self.assertEqual(len(a._trait_notifiers["a"]["change"]), 0) def test_notify_only_once(self): - class A(HasTraits): - listen_to = ['a'] + listen_to = ["a"] a = Int(0) b = 0 def __init__(self, **kwargs): - super(A, self).__init__(**kwargs) - self.on_trait_change(self.listener1, ['a']) + super().__init__(**kwargs) + self.on_trait_change(self.listener1, ["a"]) def listener1(self, name, old, new): self.b += 1 @@ -574,7 +593,7 @@ class TestHasTraitsNotify(TestCase): d = 0 def __init__(self, **kwargs): - super(B, self).__init__(**kwargs) + super().__init__(**kwargs) self.on_trait_change(self.listener2) def listener2(self, name, old, new): @@ -591,8 +610,8 @@ class TestHasTraitsNotify(TestCase): self.assertEqual(b.b, b.c) self.assertEqual(b.b, b.d) -class TestObserveDecorator(TestCase): +class TestObserveDecorator(TestCase): def setUp(self): self._notify1 = [] self._notify2 = [] @@ -604,7 +623,6 @@ class TestObserveDecorator(TestCase): self._notify2.append(change) def test_notify_all(self): - class A(HasTraits): a = Int() b = Float() @@ -612,40 +630,38 @@ class TestObserveDecorator(TestCase): a = A() a.observe(self.notify1) a.a = 0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.b = 0.0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.a = 10 - change = change_dict('a', 0, 10, a, 'change') + change = change_dict("a", 0, 10, a, "change") self.assertTrue(change in self._notify1) a.b = 10.0 - change = change_dict('b', 0.0, 10.0, a, 'change') + change = change_dict("b", 0.0, 10.0, a, "change") self.assertTrue(change in self._notify1) - self.assertRaises(TraitError,setattr,a,'a','bad string') - self.assertRaises(TraitError,setattr,a,'b','bad string') + self.assertRaises(TraitError, setattr, a, "a", "bad string") + self.assertRaises(TraitError, setattr, a, "b", "bad string") self._notify1 = [] a.unobserve(self.notify1) a.a = 20 a.b = 20.0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) def test_notify_one(self): - class A(HasTraits): a = Int() b = Float() a = A() - a.observe(self.notify1, 'a') + a.observe(self.notify1, "a") a.a = 0 - self.assertEqual(len(self._notify1),0) + self.assertEqual(len(self._notify1), 0) a.a = 10 - change = change_dict('a', 0, 10, a, 'change') + change = change_dict("a", 0, 10, a, "change") self.assertTrue(change in self._notify1) - self.assertRaises(TraitError,setattr,a,'a','bad string') + self.assertRaises(TraitError, setattr, a, "a", "bad string") def test_subclass(self): - class A(HasTraits): a = Int() @@ -653,15 +669,14 @@ class TestObserveDecorator(TestCase): b = Float() b = B() - self.assertEqual(b.a,0) - self.assertEqual(b.b,0.0) + self.assertEqual(b.a, 0) + self.assertEqual(b.b, 0.0) b.a = 100 b.b = 100.0 - self.assertEqual(b.a,100) - self.assertEqual(b.b,100.0) + self.assertEqual(b.a, 100) + self.assertEqual(b.b, 100.0) def test_notify_subclass(self): - class A(HasTraits): a = Int() @@ -669,28 +684,27 @@ class TestObserveDecorator(TestCase): b = Float() b = B() - b.observe(self.notify1, 'a') - b.observe(self.notify2, 'b') + b.observe(self.notify1, "a") + b.observe(self.notify2, "b") b.a = 0 b.b = 0.0 - self.assertEqual(len(self._notify1),0) - self.assertEqual(len(self._notify2),0) + self.assertEqual(len(self._notify1), 0) + self.assertEqual(len(self._notify2), 0) b.a = 10 b.b = 10.0 - change = change_dict('a', 0, 10, b, 'change') + change = change_dict("a", 0, 10, b, "change") self.assertTrue(change in self._notify1) - change = change_dict('b', 0.0, 10.0, b, 'change') + change = change_dict("b", 0.0, 10.0, b, "change") self.assertTrue(change in self._notify2) def test_static_notify(self): - class A(HasTraits): a = Int() b = Int() _notify1 = [] _notify_any = [] - @observe('a') + @observe("a") def _a_changed(self, change): self._notify1.append(change) @@ -700,34 +714,35 @@ class TestObserveDecorator(TestCase): a = A() a.a = 0 - self.assertEqual(len(a._notify1),0) + self.assertEqual(len(a._notify1), 0) a.a = 10 - change = change_dict('a', 0, 10, a, 'change') + change = change_dict("a", 0, 10, a, "change") self.assertTrue(change in a._notify1) a.b = 1 self.assertEqual(len(a._notify_any), 2) - change = change_dict('b', 0, 1, a, 'change') + change = change_dict("b", 0, 1, a, "change") self.assertTrue(change in a._notify_any) class B(A): b = Float() _notify2 = [] - @observe('b') + + @observe("b") def _b_changed(self, change): self._notify2.append(change) b = B() b.a = 10 b.b = 10.0 - change = change_dict('a', 0, 10, b, 'change') + change = change_dict("a", 0, 10, b, "change") self.assertTrue(change in b._notify1) - change = change_dict('b', 0.0, 10.0, b, 'change') + change = change_dict("b", 0.0, 10.0, b, "change") self.assertTrue(change in b._notify2) def test_notify_args(self): - def callback0(): self.cb = () + def callback1(change): self.cb = change @@ -735,31 +750,30 @@ class TestObserveDecorator(TestCase): a = Int() a = A() - a.on_trait_change(callback0, 'a') + a.on_trait_change(callback0, "a") a.a = 10 - self.assertEqual(self.cb,()) - a.unobserve(callback0, 'a') + self.assertEqual(self.cb, ()) + a.unobserve(callback0, "a") - a.observe(callback1, 'a') + a.observe(callback1, "a") a.a = 100 - change = change_dict('a', 10, 100, a, 'change') + change = change_dict("a", 10, 100, a, "change") self.assertEqual(self.cb, change) - self.assertEqual(len(a._trait_notifiers['a']['change']), 1) - a.unobserve(callback1, 'a') + self.assertEqual(len(a._trait_notifiers["a"]["change"]), 1) + a.unobserve(callback1, "a") - self.assertEqual(len(a._trait_notifiers['a']['change']), 0) + self.assertEqual(len(a._trait_notifiers["a"]["change"]), 0) def test_notify_only_once(self): - class A(HasTraits): - listen_to = ['a'] + listen_to = ["a"] a = Int(0) b = 0 def __init__(self, **kwargs): - super(A, self).__init__(**kwargs) - self.observe(self.listener1, ['a']) + super().__init__(**kwargs) + self.observe(self.listener1, ["a"]) def listener1(self, change): self.b += 1 @@ -770,13 +784,13 @@ class TestObserveDecorator(TestCase): d = 0 def __init__(self, **kwargs): - super(B, self).__init__(**kwargs) + super().__init__(**kwargs) self.observe(self.listener2) def listener2(self, change): self.c += 1 - @observe('a') + @observe("a") def _a_changed(self, change): self.d += 1 @@ -790,65 +804,72 @@ class TestObserveDecorator(TestCase): class TestHasTraits(TestCase): - def test_trait_names(self): class A(HasTraits): i = Int() f = Float() + a = A() - self.assertEqual(sorted(a.trait_names()),['f','i']) - self.assertEqual(sorted(A.class_trait_names()),['f','i']) - self.assertTrue(a.has_trait('f')) - self.assertFalse(a.has_trait('g')) + self.assertEqual(sorted(a.trait_names()), ["f", "i"]) + self.assertEqual(sorted(A.class_trait_names()), ["f", "i"]) + self.assertTrue(a.has_trait("f")) + self.assertFalse(a.has_trait("g")) def test_trait_has_value(self): class A(HasTraits): i = Int() f = Float() + a = A() - self.assertFalse(a.trait_has_value('f')) - self.assertFalse(a.trait_has_value('g')) + self.assertFalse(a.trait_has_value("f")) + self.assertFalse(a.trait_has_value("g")) a.i = 1 a.f - self.assertTrue(a.trait_has_value('i')) - self.assertTrue(a.trait_has_value('f')) + self.assertTrue(a.trait_has_value("i")) + self.assertTrue(a.trait_has_value("f")) def test_trait_metadata_deprecated(self): - with expected_warnings([r'metadata should be set using the \.tag\(\) method']): + with expected_warnings([r"metadata should be set using the \.tag\(\) method"]): + class A(HasTraits): - i = Int(config_key='MY_VALUE') + i = Int(config_key="MY_VALUE") + a = A() - self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') + self.assertEqual(a.trait_metadata("i", "config_key"), "MY_VALUE") def test_trait_metadata(self): class A(HasTraits): - i = Int().tag(config_key='MY_VALUE') + i = Int().tag(config_key="MY_VALUE") + a = A() - self.assertEqual(a.trait_metadata('i','config_key'), 'MY_VALUE') + self.assertEqual(a.trait_metadata("i", "config_key"), "MY_VALUE") def test_trait_metadata_default(self): class A(HasTraits): i = Int() + a = A() - self.assertEqual(a.trait_metadata('i', 'config_key'), None) - self.assertEqual(a.trait_metadata('i', 'config_key', 'default'), 'default') + self.assertEqual(a.trait_metadata("i", "config_key"), None) + self.assertEqual(a.trait_metadata("i", "config_key", "default"), "default") def test_traits(self): class A(HasTraits): i = Int() f = Float() + a = A() self.assertEqual(a.traits(), dict(i=A.i, f=A.f)) self.assertEqual(A.class_traits(), dict(i=A.i, f=A.f)) def test_traits_metadata(self): class A(HasTraits): - i = Int().tag(config_key='VALUE1', other_thing='VALUE2') - f = Float().tag(config_key='VALUE3', other_thing='VALUE2') + i = Int().tag(config_key="VALUE1", other_thing="VALUE2") + f = Float().tag(config_key="VALUE3", other_thing="VALUE2") j = Int(0) + a = A() self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) - traits = a.traits(config_key='VALUE1', other_thing='VALUE2') + traits = a.traits(config_key="VALUE1", other_thing="VALUE2") self.assertEqual(traits, dict(i=A.i)) # This passes, but it shouldn't because I am replicating a bug in @@ -857,14 +878,16 @@ class TestHasTraits(TestCase): self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) def test_traits_metadata_deprecated(self): - with expected_warnings([r'metadata should be set using the \.tag\(\) method']*2): + with expected_warnings([r"metadata should be set using the \.tag\(\) method"] * 2): + class A(HasTraits): - i = Int(config_key='VALUE1', other_thing='VALUE2') - f = Float(config_key='VALUE3', other_thing='VALUE2') + i = Int(config_key="VALUE1", other_thing="VALUE2") + f = Float(config_key="VALUE3", other_thing="VALUE2") j = Int(0) + a = A() self.assertEqual(a.traits(), dict(i=A.i, f=A.f, j=A.j)) - traits = a.traits(config_key='VALUE1', other_thing='VALUE2') + traits = a.traits(config_key="VALUE1", other_thing="VALUE2") self.assertEqual(traits, dict(i=A.i)) # This passes, but it shouldn't because I am replicating a bug in @@ -872,11 +895,11 @@ class TestHasTraits(TestCase): traits = a.traits(config_key=lambda v: True) self.assertEqual(traits, dict(i=A.i, f=A.f, j=A.j)) - def test_init(self): class A(HasTraits): i = Int() x = Float() + a = A(i=1, x=10.0) self.assertEqual(a.i, 1) self.assertEqual(a.x, 10.0) @@ -884,8 +907,9 @@ class TestHasTraits(TestCase): def test_positional_args(self): class A(HasTraits): i = Int(0) + def __init__(self, i): - super(A, self).__init__() + super().__init__() self.i = i a = A(5) @@ -893,16 +917,17 @@ class TestHasTraits(TestCase): # should raise TypeError if no positional arg given self.assertRaises(TypeError, A) -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Tests for specific trait types -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class TestType(TestCase): - def test_default(self): + class B: + pass - class B(object): pass class A(HasTraits): klass = Type(allow_none=True) @@ -911,12 +936,15 @@ class TestType(TestCase): a.klass = B self.assertEqual(a.klass, B) - self.assertRaises(TraitError, setattr, a, 'klass', 10) + self.assertRaises(TraitError, setattr, a, "klass", 10) def test_default_options(self): + class B: + pass + + class C(B): + pass - class B(object): pass - class C(B): pass class A(HasTraits): # Different possible combinations of options for default_value # and klass. default_value=None is only valid with allow_none=True. @@ -949,70 +977,75 @@ class TestType(TestCase): self.assertIs(a.k6, C) def test_value(self): + class B: + pass + + class C: + pass - class B(object): pass - class C(object): pass class A(HasTraits): klass = Type(B) a = A() self.assertEqual(a.klass, B) - self.assertRaises(TraitError, setattr, a, 'klass', C) - self.assertRaises(TraitError, setattr, a, 'klass', object) + self.assertRaises(TraitError, setattr, a, "klass", C) + self.assertRaises(TraitError, setattr, a, "klass", object) a.klass = B def test_allow_none(self): + class B: + pass + + class C(B): + pass - class B(object): pass - class C(B): pass class A(HasTraits): klass = Type(B) a = A() self.assertEqual(a.klass, B) - self.assertRaises(TraitError, setattr, a, 'klass', None) + self.assertRaises(TraitError, setattr, a, "klass", None) a.klass = C self.assertEqual(a.klass, C) def test_validate_klass(self): - class A(HasTraits): - klass = Type('no strings allowed') + klass = Type("no strings allowed") self.assertRaises(ImportError, A) class A(HasTraits): - klass = Type('rub.adub.Duck') + klass = Type("rub.adub.Duck") self.assertRaises(ImportError, A) def test_validate_default(self): + class B: + pass - class B(object): pass class A(HasTraits): - klass = Type('bad default', B) + klass = Type("bad default", B) self.assertRaises(ImportError, A) class C(HasTraits): klass = Type(None, B) - self.assertRaises(TraitError, getattr, C(), 'klass') + self.assertRaises(TraitError, getattr, C(), "klass") def test_str_klass(self): - class A(HasTraits): klass = Type("traitlets.config.Config") from traitlets.config import Config + a = A() a.klass = Config self.assertEqual(a.klass, Config) - self.assertRaises(TraitError, setattr, a, 'klass', 10) + self.assertRaises(TraitError, setattr, a, "klass", 10) def test_set_str_klass(self): - class A(HasTraits): klass = Type() @@ -1021,12 +1054,17 @@ class TestType(TestCase): self.assertEqual(a.klass, Config) -class TestInstance(TestCase): +class TestInstance(TestCase): def test_basic(self): - class Foo(object): pass - class Bar(Foo): pass - class Bah(object): pass + class Foo: + pass + + class Bar(Foo): + pass + + class Bah: + pass class A(HasTraits): inst = Instance(Foo, allow_none=True) @@ -1037,14 +1075,19 @@ class TestInstance(TestCase): self.assertTrue(isinstance(a.inst, Foo)) a.inst = Bar() self.assertTrue(isinstance(a.inst, Foo)) - self.assertRaises(TraitError, setattr, a, 'inst', Foo) - self.assertRaises(TraitError, setattr, a, 'inst', Bar) - self.assertRaises(TraitError, setattr, a, 'inst', Bah()) + self.assertRaises(TraitError, setattr, a, "inst", Foo) + self.assertRaises(TraitError, setattr, a, "inst", Bar) + self.assertRaises(TraitError, setattr, a, "inst", Bah()) def test_default_klass(self): - class Foo(object): pass - class Bar(Foo): pass - class Bah(object): pass + class Foo: + pass + + class Bar(Foo): + pass + + class Bah: + pass class FooInstance(Instance): klass = Foo @@ -1058,45 +1101,56 @@ class TestInstance(TestCase): self.assertTrue(isinstance(a.inst, Foo)) a.inst = Bar() self.assertTrue(isinstance(a.inst, Foo)) - self.assertRaises(TraitError, setattr, a, 'inst', Foo) - self.assertRaises(TraitError, setattr, a, 'inst', Bar) - self.assertRaises(TraitError, setattr, a, 'inst', Bah()) + self.assertRaises(TraitError, setattr, a, "inst", Foo) + self.assertRaises(TraitError, setattr, a, "inst", Bar) + self.assertRaises(TraitError, setattr, a, "inst", Bah()) def test_unique_default_value(self): - class Foo(object): pass + class Foo: + pass + class A(HasTraits): - inst = Instance(Foo,(),{}) + inst = Instance(Foo, (), {}) a = A() b = A() self.assertTrue(a.inst is not b.inst) def test_args_kw(self): - class Foo(object): - def __init__(self, c): self.c = c - class Bar(object): pass - class Bah(object): + class Foo: + def __init__(self, c): + self.c = c + + class Bar: + pass + + class Bah: def __init__(self, c, d): - self.c = c; self.d = d + self.c = c + self.d = d class A(HasTraits): inst = Instance(Foo, (10,)) + a = A() self.assertEqual(a.inst.c, 10) class B(HasTraits): inst = Instance(Bah, args=(10,), kw=dict(d=20)) + b = B() self.assertEqual(b.inst.c, 10) self.assertEqual(b.inst.d, 20) class C(HasTraits): inst = Instance(Foo, allow_none=True) + c = C() self.assertTrue(c.inst is None) def test_bad_default(self): - class Foo(object): pass + class Foo: + pass class A(HasTraits): inst = Instance(Foo) @@ -1106,7 +1160,8 @@ class TestInstance(TestCase): a.inst def test_instance(self): - class Foo(object): pass + class Foo: + pass def inner(): class A(HasTraits): @@ -1116,7 +1171,6 @@ class TestInstance(TestCase): class TestThis(TestCase): - def test_this_class(self): class Foo(HasTraits): this = This() @@ -1126,7 +1180,7 @@ class TestThis(TestCase): g = Foo() f.this = g self.assertEqual(f.this, g) - self.assertRaises(TraitError, setattr, f, 'this', 10) + self.assertRaises(TraitError, setattr, f, "this", 10) def test_this_inst(self): class Foo(HasTraits): @@ -1139,8 +1193,10 @@ class TestThis(TestCase): def test_subclass(self): class Foo(HasTraits): t = This() + class Bar(Foo): pass + f = Foo() b = Bar() f.t = b @@ -1151,28 +1207,27 @@ class TestThis(TestCase): def test_subclass_override(self): class Foo(HasTraits): t = This() + class Bar(Foo): t = This() + f = Foo() b = Bar() f.t = b self.assertEqual(f.t, b) - self.assertRaises(TraitError, setattr, b, 't', f) + self.assertRaises(TraitError, setattr, b, "t", f) def test_this_in_container(self): - class Tree(HasTraits): value = Unicode() leaves = List(This()) - tree = Tree( - value='foo', - leaves=[Tree(value='bar'), Tree(value='buzz')] - ) + tree = Tree(value="foo", leaves=[Tree(value="bar"), Tree(value="buzz")]) with self.assertRaises(TraitError): tree.leaves = [1, 2] + class TraitTestBase(TestCase): """A best testing class for basic trait types.""" @@ -1183,13 +1238,13 @@ class TraitTestBase(TestCase): return value def test_good_values(self): - if hasattr(self, '_good_values'): + if hasattr(self, "_good_values"): for value in self._good_values: self.assign(value) self.assertEqual(self.obj.value, self.coerce(value)) def test_bad_values(self): - if hasattr(self, '_bad_values'): + if hasattr(self, "_bad_values"): for value in self._bad_values: try: self.assertRaises(TraitError, self.assign, value) @@ -1197,29 +1252,32 @@ class TraitTestBase(TestCase): assert False, value def test_default_value(self): - if hasattr(self, '_default_value'): + if hasattr(self, "_default_value"): self.assertEqual(self._default_value, self.obj.value) def test_allow_none(self): - if (hasattr(self, '_bad_values') and hasattr(self, '_good_values') and - None in self._bad_values): - trait=self.obj.traits()['value'] + if ( + hasattr(self, "_bad_values") + and hasattr(self, "_good_values") + and None in self._bad_values + ): + trait = self.obj.traits()["value"] try: trait.allow_none = True self._bad_values.remove(None) - #skip coerce. Allow None casts None to None. + # skip coerce. Allow None casts None to None. self.assign(None) - self.assertEqual(self.obj.value,None) + self.assertEqual(self.obj.value, None) self.test_good_values() self.test_bad_values() finally: - #tear down + # tear down trait.allow_none = False self._bad_values.append(None) def tearDown(self): # restore default value after tests, if set - if hasattr(self, '_default_value'): + if hasattr(self, "_default_value"): self.obj.value = self._default_value @@ -1227,150 +1285,192 @@ class AnyTrait(HasTraits): value = Any() + class AnyTraitTest(TraitTestBase): obj = AnyTrait() _default_value = None - _good_values = [10.0, 'ten', [10], {'ten': 10},(10,), None, 1j] - _bad_values = [] + _good_values = [10.0, "ten", [10], {"ten": 10}, (10,), None, 1j] + _bad_values = [] + class UnionTrait(HasTraits): value = Union([Type(), Bool()]) + class UnionTraitTest(TraitTestBase): obj = UnionTrait(value="traitlets.config.Config") _good_values = [int, float, True] _bad_values = [[], (0,), 1j] + class CallableTrait(HasTraits): value = Callable() + class CallableTraitTest(TraitTestBase): obj = CallableTrait(value=lambda x: type(x)) _good_values = [int, sorted, lambda x: print(x)] - _bad_values = [[], 1, ''] + _bad_values = [[], 1, ""] + class OrTrait(HasTraits): value = Bool() | Unicode() + class OrTraitTest(TraitTestBase): obj = OrTrait() - _good_values = [True, False, 'ten'] + _good_values = [True, False, "ten"] _bad_values = [[], (0,), 1j] + class IntTrait(HasTraits): value = Int(99, min=-100) + class TestInt(TraitTestBase): obj = IntTrait() _default_value = 99 - _good_values = [10, -10] - _bad_values = ['ten', [10], {'ten': 10}, (10,), None, 1j, - 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', - '10', '-10', -200] + _good_values = [10, -10] + _bad_values = [ + "ten", + [10], + {"ten": 10}, + (10,), + None, + 1j, + 10.1, + -10.1, + "10L", + "-10L", + "10.1", + "-10.1", + "10", + "-10", + -200, + ] class CIntTrait(HasTraits): - value = CInt('5') + value = CInt("5") + class TestCInt(TraitTestBase): obj = CIntTrait() _default_value = 5 - _good_values = ['10', '-10', 10, 10.0, -10.0, 10.1] - _bad_values = ['ten', [10], {'ten': 10},(10,), - None, 1j, '10.1'] + _good_values = ["10", "-10", 10, 10.0, -10.0, 10.1] + _bad_values = ["ten", [10], {"ten": 10}, (10,), None, 1j, "10.1"] def coerce(self, n): return int(n) class MinBoundCIntTrait(HasTraits): - value = CInt('5', min=3) + value = CInt("5", min=3) + class TestMinBoundCInt(TestCInt): obj = MinBoundCIntTrait() _default_value = 5 - _good_values = [3, 3.0, '3'] - _bad_values = [2.6, 2, -3, -3.0] + _good_values = [3, 3.0, "3"] + _bad_values = [2.6, 2, -3, -3.0] class LongTrait(HasTraits): value = Long(99) + class TestLong(TraitTestBase): obj = LongTrait() _default_value = 99 - _good_values = [10, -10] - _bad_values = ['ten', [10], {'ten': 10},(10,), - None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1', - '-10.1'] + _good_values = [10, -10] + _bad_values = [ + "ten", + [10], + {"ten": 10}, + (10,), + None, + 1j, + 10.1, + -10.1, + "10", + "-10", + "10L", + "-10L", + "10.1", + "-10.1", + ] class MinBoundLongTrait(HasTraits): value = Long(99, min=5) + class TestMinBoundLong(TraitTestBase): obj = MinBoundLongTrait() _default_value = 99 - _good_values = [5, 10] - _bad_values = [4, -10] + _good_values = [5, 10] + _bad_values = [4, -10] class MaxBoundLongTrait(HasTraits): value = Long(5, max=10) + class TestMaxBoundLong(TraitTestBase): obj = MaxBoundLongTrait() _default_value = 5 - _good_values = [10, -2] - _bad_values = [11, 20] + _good_values = [10, -2] + _bad_values = [11, 20] class CLongTrait(HasTraits): - value = CLong('5') + value = CLong("5") + class TestCLong(TraitTestBase): obj = CLongTrait() _default_value = 5 - _good_values = ['10', '-10', 10, 10.0, -10.0, 10.1] - _bad_values = ['ten', [10], {'ten': 10},(10,), - None, 1j, '10.1'] + _good_values = ["10", "-10", 10, 10.0, -10.0, 10.1] + _bad_values = ["ten", [10], {"ten": 10}, (10,), None, 1j, "10.1"] def coerce(self, n): return int(n) class MaxBoundCLongTrait(HasTraits): - value = CLong('5', max=10) + value = CLong("5", max=10) + class TestMaxBoundCLong(TestCLong): obj = MaxBoundCLongTrait() _default_value = 5 - _good_values = [10, '10', 10.3] - _bad_values = [11.0, '11'] + _good_values = [10, "10", 10.3] + _bad_values = [11.0, "11"] class IntegerTrait(HasTraits): value = Integer(1) + class TestInteger(TestLong): obj = IntegerTrait() _default_value = 1 @@ -1382,51 +1482,67 @@ class TestInteger(TestLong): class MinBoundIntegerTrait(HasTraits): value = Integer(5, min=3) + class TestMinBoundInteger(TraitTestBase): obj = MinBoundIntegerTrait() _default_value = 5 - _good_values = 3, 20 - _bad_values = [2, -10] + _good_values = 3, 20 + _bad_values = [2, -10] class MaxBoundIntegerTrait(HasTraits): value = Integer(1, max=3) + class TestMaxBoundInteger(TraitTestBase): obj = MaxBoundIntegerTrait() _default_value = 1 - _good_values = 3, -2 - _bad_values = [4, 10] + _good_values = 3, -2 + _bad_values = [4, 10] class FloatTrait(HasTraits): value = Float(99.0, max=200.0) + class TestFloat(TraitTestBase): obj = FloatTrait() _default_value = 99.0 - _good_values = [10, -10, 10.1, -10.1] - _bad_values = ['ten', [10], {'ten': 10}, (10,), None, - 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', 201.0] + _good_values = [10, -10, 10.1, -10.1] + _bad_values = [ + "ten", + [10], + {"ten": 10}, + (10,), + None, + 1j, + "10", + "-10", + "10L", + "-10L", + "10.1", + "-10.1", + 201.0, + ] class CFloatTrait(HasTraits): - value = CFloat('99.0', max=200.0) + value = CFloat("99.0", max=200.0) + class TestCFloat(TraitTestBase): obj = CFloatTrait() _default_value = 99.0 - _good_values = [10, 10.0, 10.5, '10.0', '10', '-10'] - _bad_values = ['ten', [10], {'ten': 10}, (10,), None, 1j, - 200.1, '200.1'] + _good_values = [10, 10.0, 10.5, "10.0", "10", "-10"] + _bad_values = ["ten", [10], {"ten": 10}, (10,), None, 1j, 200.1, "200.1"] def coerce(self, v): return float(v) @@ -1434,47 +1550,55 @@ class TestCFloat(TraitTestBase): class ComplexTrait(HasTraits): - value = Complex(99.0-99.0j) + value = Complex(99.0 - 99.0j) + class TestComplex(TraitTestBase): obj = ComplexTrait() - _default_value = 99.0-99.0j - _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j, - 10.1j, 10.1+10.1j, 10.1-10.1j] - _bad_values = ['10L', '-10L', 'ten', [10], {'ten': 10},(10,), None] + _default_value = 99.0 - 99.0j + _good_values = [ + 10, + -10, + 10.1, + -10.1, + 10j, + 10 + 10j, + 10 - 10j, + 10.1j, + 10.1 + 10.1j, + 10.1 - 10.1j, + ] + _bad_values = ["10L", "-10L", "ten", [10], {"ten": 10}, (10,), None] class BytesTrait(HasTraits): - value = Bytes(b'string') + value = Bytes(b"string") + class TestBytes(TraitTestBase): obj = BytesTrait() - _default_value = b'string' - _good_values = [b'10', b'-10', b'10L', - b'-10L', b'10.1', b'-10.1', b'string'] - _bad_values = [10, -10, 10.1, -10.1, 1j, [10], - ['ten'],{'ten': 10},(10,), None, 'string'] + _default_value = b"string" + _good_values = [b"10", b"-10", b"10L", b"-10L", b"10.1", b"-10.1", b"string"] + _bad_values = [10, -10, 10.1, -10.1, 1j, [10], ["ten"], {"ten": 10}, (10,), None, "string"] class UnicodeTrait(HasTraits): - value = Unicode('unicode') + value = Unicode("unicode") class TestUnicode(TraitTestBase): obj = UnicodeTrait() - _default_value = 'unicode' - _good_values = ['10', '-10', '10L', '-10L', '10.1', - '-10.1', '', 'string', "€", b"bytestring"] - _bad_values = [10, -10, 10.1, -10.1, 1j, - [10], ['ten'], {'ten': 10},(10,), None] + _default_value = "unicode" + _good_values = ["10", "-10", "10L", "-10L", "10.1", "-10.1", "", "string", "€", b"bytestring"] + _bad_values = [10, -10, 10.1, -10.1, 1j, [10], ["ten"], {"ten": 10}, (10,), None] def coerce(self, v): return cast_unicode(v) @@ -1483,19 +1607,34 @@ class TestUnicode(TraitTestBase): class ObjectNameTrait(HasTraits): value = ObjectName("abc") + class TestObjectName(TraitTestBase): obj = ObjectNameTrait() _default_value = "abc" _good_values = ["a", "gh", "g9", "g_", "_G", "a345_"] - _bad_values = [1, "", "€", "9g", "!", "#abc", "aj@", "a.b", "a()", "a[0]", - None, object(), object] + _bad_values = [ + 1, + "", + "€", + "9g", + "!", + "#abc", + "aj@", + "a.b", + "a()", + "a[0]", + None, + object(), + object, + ] _good_values.append("þ") # þ=1 is valid in Python 3 (PEP 3131). class DottedObjectNameTrait(HasTraits): value = DottedObjectName("a.b") + class TestDottedObjectName(TraitTestBase): obj = DottedObjectNameTrait() @@ -1509,13 +1648,14 @@ class TestDottedObjectName(TraitTestBase): class TCPAddressTrait(HasTraits): value = TCPAddress() + class TestTCPAddress(TraitTestBase): obj = TCPAddressTrait() - _default_value = ('127.0.0.1',0) - _good_values = [('localhost',0),('192.168.0.1',1000),('www.google.com',80)] - _bad_values = [(0,0),('localhost',10.0),('localhost',-1), None] + _default_value = ("127.0.0.1", 0) + _good_values = [("localhost", 0), ("192.168.0.1", 1000), ("www.google.com", 80)] + _bad_values = [(0, 0), ("localhost", 10.0), ("localhost", -1), None] class ListTrait(HasTraits): @@ -1528,8 +1668,8 @@ class TestList(TraitTestBase): obj = ListTrait() _default_value = [] - _good_values = [[], [1], list(range(10)), (1,2)] - _bad_values = [10, [1,'a'], 'a'] + _good_values = [[], [1], list(range(10)), (1, 2)] + _bad_values = [10, [1, "a"], "a"] def coerce(self, value): if value is not None: @@ -1537,13 +1677,15 @@ class TestList(TraitTestBase): return value -class Foo(object): +class Foo: pass + class NoneInstanceListTrait(HasTraits): value = List(Instance(Foo)) + class TestNoneInstanceList(TraitTestBase): obj = NoneInstanceListTrait() @@ -1555,7 +1697,8 @@ class TestNoneInstanceList(TraitTestBase): class InstanceListTrait(HasTraits): - value = List(Instance(__name__+'.Foo')) + value = List(Instance(__name__ + ".Foo")) + class TestInstanceList(TraitTestBase): @@ -1563,53 +1706,66 @@ class TestInstanceList(TraitTestBase): def test_klass(self): """Test that the instance klass is properly assigned.""" - self.assertIs(self.obj.traits()['value']._trait.klass, Foo) + self.assertIs(self.obj.traits()["value"]._trait.klass, Foo) _default_value = [] _good_values = [[Foo(), Foo()], []] - _bad_values = [['1', 2,], '1', [Foo], None] + _bad_values = [ + [ + "1", + 2, + ], + "1", + [Foo], + None, + ] + class UnionListTrait(HasTraits): value = List(Int() | Bool()) + class TestUnionListTrait(TraitTestBase): obj = UnionListTrait() _default_value = [] _good_values = [[True, 1], [False, True]] - _bad_values = [[1, 'True'], False] + _bad_values = [[1, "True"], False] class LenListTrait(HasTraits): value = List(Int(), [0], minlen=1, maxlen=2) + class TestLenList(TraitTestBase): obj = LenListTrait() _default_value = [0] - _good_values = [[1], [1,2], (1,2)] - _bad_values = [10, [1,'a'], 'a', [], list(range(3))] + _good_values = [[1], [1, 2], (1, 2)] + _bad_values = [10, [1, "a"], "a", [], list(range(3))] def coerce(self, value): if value is not None: value = list(value) return value + class TupleTrait(HasTraits): value = Tuple(Int(allow_none=True), default_value=(1,)) + class TestTupleTrait(TraitTestBase): obj = TupleTrait() _default_value = (1,) _good_values = [(1,), (0,), [1]] - _bad_values = [10, (1, 2), ('a'), (), None] + _bad_values = [10, (1, 2), ("a"), (), None] def coerce(self, value): if value is not None: @@ -1618,20 +1774,22 @@ class TestTupleTrait(TraitTestBase): def test_invalid_args(self): self.assertRaises(TypeError, Tuple, 5) - self.assertRaises(TypeError, Tuple, default_value='hello') - t = Tuple(Int(), CBytes(), default_value=(1,5)) + self.assertRaises(TypeError, Tuple, default_value="hello") + t = Tuple(Int(), CBytes(), default_value=(1, 5)) + class LooseTupleTrait(HasTraits): - value = Tuple((1,2,3)) + value = Tuple((1, 2, 3)) + class TestLooseTupleTrait(TraitTestBase): obj = LooseTupleTrait() - _default_value = (1,2,3) - _good_values = [(1,), [1], (0,), tuple(range(5)), tuple('hello'), ('a',5), ()] - _bad_values = [10, 'hello', {}, None] + _default_value = (1, 2, 3) + _good_values = [(1,), [1], (0,), tuple(range(5)), tuple("hello"), ("a", 5), ()] + _bad_values = [10, "hello", {}, None] def coerce(self, value): if value is not None: @@ -1640,25 +1798,34 @@ class TestLooseTupleTrait(TraitTestBase): def test_invalid_args(self): self.assertRaises(TypeError, Tuple, 5) - self.assertRaises(TypeError, Tuple, default_value='hello') - t = Tuple(Int(), CBytes(), default_value=(1,5)) + self.assertRaises(TypeError, Tuple, default_value="hello") + t = Tuple(Int(), CBytes(), default_value=(1, 5)) class MultiTupleTrait(HasTraits): - value = Tuple(Int(), Bytes(), default_value=[99,b'bottles']) + value = Tuple(Int(), Bytes(), default_value=[99, b"bottles"]) + class TestMultiTuple(TraitTestBase): obj = MultiTupleTrait() - _default_value = (99,b'bottles') - _good_values = [(1,b'a'), (2,b'b')] - _bad_values = ((),10, b'a', (1,b'a',3), (b'a',1), (1, 'a')) + _default_value = (99, b"bottles") + _good_values = [(1, b"a"), (2, b"b")] + _bad_values = ((), 10, b"a", (1, b"a", 3), (b"a", 1), (1, "a")) @pytest.mark.parametrize( - "Trait", (List, Tuple, Set, Dict, Integer, Unicode,), + "Trait", + ( + List, + Tuple, + Set, + Dict, + Integer, + Unicode, + ), ) def test_allow_none_default_value(Trait): class C(HasTraits): @@ -1709,92 +1876,89 @@ def test_subclass_default_value(Trait, default_value): class CRegExpTrait(HasTraits): - value = CRegExp(r'') + value = CRegExp(r"") class TestCRegExp(TraitTestBase): - def coerce(self, value): return re.compile(value) obj = CRegExpTrait() - _default_value = re.compile(r'') - _good_values = [r'\d+', re.compile(r'\d+')] - _bad_values = ['(', None, ()] + _default_value = re.compile(r"") + _good_values = [r"\d+", re.compile(r"\d+")] + _bad_values = ["(", None, ()] + class DictTrait(HasTraits): value = Dict() + def test_dict_assignment(): - d = dict() + d = {} c = DictTrait() c.value = d - d['a'] = 5 + d["a"] = 5 assert d == c.value assert c.value is d class UniformlyValueValidatedDictTrait(HasTraits): - value = Dict(trait=Unicode(), - default_value={'foo': '1'}) + value = Dict(trait=Unicode(), default_value={"foo": "1"}) class TestInstanceUniformlyValueValidatedDict(TraitTestBase): obj = UniformlyValueValidatedDictTrait() - _default_value = {'foo': '1'} - _good_values = [{'foo': '0', 'bar': '1'}] - _bad_values = [{'foo': 0, 'bar': '1'}] + _default_value = {"foo": "1"} + _good_values = [{"foo": "0", "bar": "1"}] + _bad_values = [{"foo": 0, "bar": "1"}] class NonuniformlyValueValidatedDictTrait(HasTraits): - value = Dict(traits={'foo': Int()}, - default_value={'foo': 1}) + value = Dict(traits={"foo": Int()}, default_value={"foo": 1}) class TestInstanceNonuniformlyValueValidatedDict(TraitTestBase): obj = NonuniformlyValueValidatedDictTrait() - _default_value = {'foo': 1} - _good_values = [{'foo': 0, 'bar': '1'}, {'foo': 0, 'bar': 1}] - _bad_values = [{'foo': '0', 'bar': '1'}] + _default_value = {"foo": 1} + _good_values = [{"foo": 0, "bar": "1"}, {"foo": 0, "bar": 1}] + _bad_values = [{"foo": "0", "bar": "1"}] class KeyValidatedDictTrait(HasTraits): - value = Dict(key_trait=Unicode(), - default_value={'foo': '1'}) + value = Dict(key_trait=Unicode(), default_value={"foo": "1"}) class TestInstanceKeyValidatedDict(TraitTestBase): obj = KeyValidatedDictTrait() - _default_value = {'foo': '1'} - _good_values = [{'foo': '0', 'bar': '1'}] - _bad_values = [{'foo': '0', 0: '1'}] + _default_value = {"foo": "1"} + _good_values = [{"foo": "0", "bar": "1"}] + _bad_values = [{"foo": "0", 0: "1"}] class FullyValidatedDictTrait(HasTraits): - value = Dict(trait=Unicode(), - key_trait=Unicode(), - traits={'foo': Int()}, - default_value={'foo': 1}) + value = Dict( + trait=Unicode(), key_trait=Unicode(), traits={"foo": Int()}, default_value={"foo": 1} + ) class TestInstanceFullyValidatedDict(TraitTestBase): obj = FullyValidatedDictTrait() - _default_value = {'foo': 1} - _good_values = [{'foo': 0, 'bar': '1'}, {'foo': 1, 'bar': '2'}] - _bad_values = [{'foo': 0, 'bar': 1}, {'foo': '0', 'bar': '1'}, {'foo': 0, 0: '1'}] + _default_value = {"foo": 1} + _good_values = [{"foo": 0, "bar": "1"}, {"foo": 1, "bar": "2"}] + _bad_values = [{"foo": 0, "bar": 1}, {"foo": "0", "bar": "1"}, {"foo": 0, 0: "1"}] def test_dict_default_value(): @@ -1807,36 +1971,35 @@ def test_dict_default_value(): foo = Foo() assert foo.d1 == {} - assert foo.d2 == {} + assert foo.d2 == {} assert foo.d1 is not foo.d2 class TestValidationHook(TestCase): - def test_parity_trait(self): """Verify that the early validation hook is effective""" class Parity(HasTraits): value = Int(0) - parity = Enum(['odd', 'even'], default_value='even') + parity = Enum(["odd", "even"], default_value="even") - @validate('value') + @validate("value") def _value_validate(self, proposal): - value = proposal['value'] - if self.parity == 'even' and value % 2: - raise TraitError('Expected an even number') - if self.parity == 'odd' and (value % 2 == 0): - raise TraitError('Expected an odd number') + value = proposal["value"] + if self.parity == "even" and value % 2: + raise TraitError("Expected an even number") + if self.parity == "odd" and (value % 2 == 0): + raise TraitError("Expected an odd number") return value u = Parity() - u.parity = 'odd' + u.parity = "odd" u.value = 1 # OK with self.assertRaises(TraitError): u.value = 2 # Trait Error - u.parity = 'even' + u.parity = "even" u.value = 2 # OK def test_multiple_validate(self): @@ -1847,12 +2010,12 @@ class TestValidationHook(TestCase): odd = Int(1) even = Int(0) - @validate('odd', 'even') + @validate("odd", "even") def check_valid(self, proposal): - if proposal['trait'].name == 'odd' and not proposal['value'] % 2: - raise TraitError('odd should be odd') - if proposal['trait'].name == 'even' and proposal['value'] % 2: - raise TraitError('even should be even') + if proposal["trait"].name == "odd" and not proposal["value"] % 2: + raise TraitError("odd should be odd") + if proposal["trait"].name == "even" and proposal["value"] % 2: + raise TraitError("even should be even") u = OddEven() u.odd = 3 # OK @@ -1864,20 +2027,19 @@ class TestValidationHook(TestCase): u.even = 3 # Trait Error - class TestLink(TestCase): - def test_connect_same(self): """Verify two traitlets of the same type can be linked together using link.""" # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Conenct the two classes. - c = link((a, 'value'), (b, 'value')) + c = link((a, "value"), (b, "value")) # Make sure the values are the same at the point of linking. self.assertEqual(a.value, b.value) @@ -1894,13 +2056,15 @@ class TestLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + class B(HasTraits): count = Int() + a = A(value=9) b = B(count=8) # Conenct the two classes. - c = link((a, 'value'), (b, 'count')) + c = link((a, "value"), (b, "count")) # Make sure the values are the same at the point of linking. self.assertEqual(a.value, b.count) @@ -1917,11 +2081,12 @@ class TestLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Connect the two classes. - c = link((a, 'value'), (b, 'value')) + c = link((a, "value"), (b, "value")) a.value = 4 c.unlink() @@ -1939,35 +2104,41 @@ class TestLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + class B(HasTraits): count = Int() + a = A(value=9) b = B(count=8) # Register callbacks that count. callback_count = [] + def a_callback(name, old, new): - callback_count.append('a') - a.on_trait_change(a_callback, 'value') + callback_count.append("a") + + a.on_trait_change(a_callback, "value") + def b_callback(name, old, new): - callback_count.append('b') - b.on_trait_change(b_callback, 'count') + callback_count.append("b") + + b.on_trait_change(b_callback, "count") # Connect the two classes. - c = link((a, 'value'), (b, 'count')) + c = link((a, "value"), (b, "count")) # Make sure b's count was set to a's value once. - self.assertEqual(''.join(callback_count), 'b') + self.assertEqual("".join(callback_count), "b") del callback_count[:] # Make sure a's value was set to b's count once. b.count = 5 - self.assertEqual(''.join(callback_count), 'ba') + self.assertEqual("".join(callback_count), "ba") del callback_count[:] # Make sure b's count was set to a's value once. a.value = 4 - self.assertEqual(''.join(callback_count), 'ab') + self.assertEqual("".join(callback_count), "ab") del callback_count[:] def test_tranform(self): @@ -1976,12 +2147,12 @@ class TestLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Conenct the two classes. - c = link((a, 'value'), (b, 'value'), - transform=(lambda x: 2 * x, lambda x: int(x / 2.))) + c = link((a, "value"), (b, "value"), transform=(lambda x: 2 * x, lambda x: int(x / 2.0))) # Make sure the values are correct at the point of linking. self.assertEqual(b.value, 2 * a.value) @@ -2004,12 +2175,12 @@ class TestLink(TestCase): self.i = change.new * 2 mc = MyClass() - l = link((mc, "i"), (mc, "j")) - self.assertRaises(TraitError, setattr, mc, 'i', 2) + l = link((mc, "i"), (mc, "j")) # noqa + self.assertRaises(TraitError, setattr, mc, "i", 2) def test_link_broken_at_target(self): class MyClass(HasTraits): - i =Int() + i = Int() j = Int() @observe("i") @@ -2017,8 +2188,9 @@ class TestLink(TestCase): self.j = change.new * 2 mc = MyClass() - l = link((mc, "i"), (mc, "j")) - self.assertRaises(TraitError, setattr, mc, 'j', 2) + l = link((mc, "i"), (mc, "j")) # noqa + self.assertRaises(TraitError, setattr, mc, "j", 2) + class TestDirectionalLink(TestCase): def test_connect_same(self): @@ -2027,11 +2199,12 @@ class TestDirectionalLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Conenct the two classes. - c = directional_link((a, 'value'), (b, 'value')) + c = directional_link((a, "value"), (b, "value")) # Make sure the values are the same at the point of linking. self.assertEqual(a.value, b.value) @@ -2049,11 +2222,12 @@ class TestDirectionalLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Conenct the two classes. - c = directional_link((a, 'value'), (b, 'value'), lambda x: 2 * x) + c = directional_link((a, "value"), (b, "value"), lambda x: 2 * x) # Make sure the values are correct at the point of linking. self.assertEqual(b.value, 2 * a.value) @@ -2071,13 +2245,15 @@ class TestDirectionalLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + class B(HasTraits): count = Int() + a = A(value=9) b = B(count=8) # Conenct the two classes. - c = directional_link((a, 'value'), (b, 'count')) + c = directional_link((a, "value"), (b, "count")) # Make sure the values are the same at the point of linking. self.assertEqual(a.value, b.count) @@ -2095,11 +2271,12 @@ class TestDirectionalLink(TestCase): # Create two simple classes with Int traitlets. class A(HasTraits): value = Int() + a = A(value=9) b = A(value=8) # Connect the two classes. - c = directional_link((a, 'value'), (b, 'value')) + c = directional_link((a, "value"), (b, "value")) a.value = 4 c.unlink() @@ -2111,21 +2288,26 @@ class TestDirectionalLink(TestCase): a.value += 1 self.assertEqual(a.value, b.value) + class Pickleable(HasTraits): i = Int() - @observe('i') - def _i_changed(self, change): pass - @validate('i') + + @observe("i") + def _i_changed(self, change): + pass + + @validate("i") def _i_validate(self, commit): - return commit['value'] + return commit["value"] j = Int() def __init__(self): with self.hold_trait_notifications(): self.i = 1 - self.on_trait_change(self._i_changed, 'i') + self.on_trait_change(self._i_changed, "i") + def test_pickle_hastraits(): c = Pickleable() @@ -2155,7 +2337,7 @@ def test_hold_trait_notifications(): def _b_validate(self, value, trait): if value != 0: - raise TraitError('Only 0 is a valid value') + raise TraitError("Only 0 is a valid value") return value # Test context manager and nesting @@ -2181,25 +2363,24 @@ def test_hold_trait_notifications(): assert changes == [(0, 4)] # Test roll-back try: - with t.hold_trait_notifications(): - t.b = 1 # raises a Trait error - except: + with t.hold_trait_notifications(): + t.b = 1 # raises a Trait error + except Exception: pass assert t.b == 0 class RollBack(HasTraits): bar = Int() + def _bar_validate(self, value, trait): if value: - raise TraitError('foobar') + raise TraitError("foobar") return value class TestRollback(TestCase): - def test_roll_back(self): - def assign_rollback(): RollBack(bar=1) @@ -2238,7 +2419,7 @@ class OrderTraits(HasTraits): i = Unicode() j = Unicode() k = Unicode() - l = Unicode() + l = Unicode() # noqa def _notify(self, name, old, new): """check the value of all traits when each trait change is triggered @@ -2248,44 +2429,45 @@ class OrderTraits(HasTraits): """ # check the value of the other traits # when a given trait change notification fires - self.notified[name] = { - c: getattr(self, c) for c in 'abcdefghijkl' - } + self.notified[name] = {c: getattr(self, c) for c in "abcdefghijkl"} def __init__(self, **kwargs): self.on_trait_change(self._notify) - super(OrderTraits, self).__init__(**kwargs) + super().__init__(**kwargs) + def test_notification_order(): - d = {c:c for c in 'abcdefghijkl'} + d = {c: c for c in "abcdefghijkl"} obj = OrderTraits() assert obj.notified == {} obj = OrderTraits(**d) - notifications = { - c: d for c in 'abcdefghijkl' - } + notifications = {c: d for c in "abcdefghijkl"} assert obj.notified == notifications - ### # Traits for Forward Declaration Tests ### class ForwardDeclaredInstanceTrait(HasTraits): - value = ForwardDeclaredInstance('ForwardDeclaredBar', allow_none=True) + value = ForwardDeclaredInstance("ForwardDeclaredBar", allow_none=True) + class ForwardDeclaredTypeTrait(HasTraits): - value = ForwardDeclaredType('ForwardDeclaredBar', allow_none=True) + value = ForwardDeclaredType("ForwardDeclaredBar", allow_none=True) + class ForwardDeclaredInstanceListTrait(HasTraits): - value = List(ForwardDeclaredInstance('ForwardDeclaredBar')) + value = List(ForwardDeclaredInstance("ForwardDeclaredBar")) + class ForwardDeclaredTypeListTrait(HasTraits): - value = List(ForwardDeclaredType('ForwardDeclaredBar')) + value = List(ForwardDeclaredType("ForwardDeclaredBar")) + + ### # End Traits for Forward Declaration Tests ### @@ -2293,11 +2475,14 @@ class ForwardDeclaredTypeListTrait(HasTraits): ### # Classes for Forward Declaration Tests ### -class ForwardDeclaredBar(object): +class ForwardDeclaredBar: pass + class ForwardDeclaredBarSub(ForwardDeclaredBar): pass + + ### # End Classes for Forward Declaration Tests ### @@ -2310,14 +2495,16 @@ class TestForwardDeclaredInstanceTrait(TraitTestBase): obj = ForwardDeclaredInstanceTrait() _default_value = None _good_values = [None, ForwardDeclaredBar(), ForwardDeclaredBarSub()] - _bad_values = ['foo', 3, ForwardDeclaredBar, ForwardDeclaredBarSub] + _bad_values = ["foo", 3, ForwardDeclaredBar, ForwardDeclaredBarSub] + class TestForwardDeclaredTypeTrait(TraitTestBase): obj = ForwardDeclaredTypeTrait() _default_value = None _good_values = [None, ForwardDeclaredBar, ForwardDeclaredBarSub] - _bad_values = ['foo', 3, ForwardDeclaredBar(), ForwardDeclaredBarSub()] + _bad_values = ["foo", 3, ForwardDeclaredBar(), ForwardDeclaredBarSub()] + class TestForwardDeclaredInstanceList(TraitTestBase): @@ -2325,7 +2512,7 @@ class TestForwardDeclaredInstanceList(TraitTestBase): def test_klass(self): """Test that the instance klass is properly assigned.""" - self.assertIs(self.obj.traits()['value']._trait.klass, ForwardDeclaredBar) + self.assertIs(self.obj.traits()["value"]._trait.klass, ForwardDeclaredBar) _default_value = [] _good_values = [ @@ -2335,20 +2522,21 @@ class TestForwardDeclaredInstanceList(TraitTestBase): _bad_values = [ ForwardDeclaredBar(), [ForwardDeclaredBar(), 3, None], - '1', + "1", # Note that this is the type, not an instance. [ForwardDeclaredBar], [None], None, ] + class TestForwardDeclaredTypeList(TraitTestBase): obj = ForwardDeclaredTypeListTrait() def test_klass(self): """Test that the instance klass is properly assigned.""" - self.assertIs(self.obj.traits()['value']._trait.klass, ForwardDeclaredBar) + self.assertIs(self.obj.traits()["value"]._trait.klass, ForwardDeclaredBar) _default_value = [] _good_values = [ @@ -2358,18 +2546,20 @@ class TestForwardDeclaredTypeList(TraitTestBase): _bad_values = [ ForwardDeclaredBar, [ForwardDeclaredBar, 3], - '1', + "1", # Note that this is an instance, not the type. [ForwardDeclaredBar()], [None], None, ] + + ### # End Forward Declaration Tests ### -class TestDynamicTraits(TestCase): +class TestDynamicTraits(TestCase): def setUp(self): self._notify1 = [] @@ -2377,30 +2567,29 @@ class TestDynamicTraits(TestCase): self._notify1.append((name, old, new)) def test_notify_all(self): - class A(HasTraits): pass a = A() - self.assertTrue(not hasattr(a, 'x')) - self.assertTrue(not hasattr(a, 'y')) + self.assertTrue(not hasattr(a, "x")) + self.assertTrue(not hasattr(a, "y")) # Dynamically add trait x. a.add_traits(x=Int()) - self.assertTrue(hasattr(a, 'x')) - self.assertTrue(isinstance(a, (A, ))) + self.assertTrue(hasattr(a, "x")) + self.assertTrue(isinstance(a, (A,))) # Dynamically add trait y. a.add_traits(y=Float()) - self.assertTrue(hasattr(a, 'y')) - self.assertTrue(isinstance(a, (A, ))) + self.assertTrue(hasattr(a, "y")) + self.assertTrue(isinstance(a, (A,))) self.assertEqual(a.__class__.__name__, A.__name__) # Create a new instance and verify that x and y # aren't defined. b = A() - self.assertTrue(not hasattr(b, 'x')) - self.assertTrue(not hasattr(b, 'y')) + self.assertTrue(not hasattr(b, "x")) + self.assertTrue(not hasattr(b, "y")) # Verify that notification works like normal. a.on_trait_change(self.notify1) @@ -2409,11 +2598,11 @@ class TestDynamicTraits(TestCase): a.y = 0.0 self.assertEqual(len(self._notify1), 0) a.x = 10 - self.assertTrue(('x', 0, 10) in self._notify1) + self.assertTrue(("x", 0, 10) in self._notify1) a.y = 10.0 - self.assertTrue(('y', 0.0, 10.0) in self._notify1) - self.assertRaises(TraitError, setattr, a, 'x', 'bad string') - self.assertRaises(TraitError, setattr, a, 'y', 'bad string') + self.assertTrue(("y", 0.0, 10.0) in self._notify1) + self.assertRaises(TraitError, setattr, a, "x", "bad string") + self.assertRaises(TraitError, setattr, a, "y", "bad string") self._notify1 = [] a.on_trait_change(self.notify1, remove=True) a.x = 20 @@ -2423,24 +2612,24 @@ class TestDynamicTraits(TestCase): def test_enum_no_default(): class C(HasTraits): - t = Enum(['a', 'b']) + t = Enum(["a", "b"]) c = C() - c.t = 'a' - assert c.t == 'a' + c.t = "a" + assert c.t == "a" c = C() with pytest.raises(TraitError): t = c.t - c = C(t='b') - assert c.t == 'b' + c = C(t="b") + assert c.t == "b" def test_default_value_repr(): class C(HasTraits): - t = Type('traitlets.HasTraits') + t = Type("traitlets.HasTraits") t2 = Type(HasTraits) n = Integer(0) lis = List() @@ -2448,26 +2637,27 @@ def test_default_value_repr(): assert C.t.default_value_repr() == "'traitlets.HasTraits'" assert C.t2.default_value_repr() == "'traitlets.traitlets.HasTraits'" - assert C.n.default_value_repr() == '0' - assert C.lis.default_value_repr() == '[]' - assert C.d.default_value_repr() == '{}' + assert C.n.default_value_repr() == "0" + assert C.lis.default_value_repr() == "[]" + assert C.d.default_value_repr() == "{}" class TransitionalClass(HasTraits): d = Any() - @default('d') + + @default("d") def _d_default(self): return TransitionalClass parent_super = False calls_super = Integer(0) - @default('calls_super') + @default("calls_super") def _calls_super_default(self): return -1 - @observe('calls_super') + @observe("calls_super") @observe_compat def _calls_super_changed(self, change): self.parent_super = change @@ -2475,7 +2665,7 @@ class TransitionalClass(HasTraits): parent_override = False overrides = Integer(0) - @observe('overrides') + @observe("overrides") @observe_compat def _overrides_changed(self, change): self.parent_override = change @@ -2486,11 +2676,13 @@ class SubClass(TransitionalClass): return SubClass subclass_super = False + def _calls_super_changed(self, name, old, new): self.subclass_super = True - super(SubClass, self)._calls_super_changed(name, old, new) + super()._calls_super_changed(name, old, new) subclass_override = False + def _overrides_changed(self, name, old, new): self.subclass_override = True @@ -2510,7 +2702,8 @@ class DefinesHandler(HasTraits): parent_called = False trait = Integer() - @observe('trait') + + @observe("trait") def handler(self, change): self.parent_called = True @@ -2518,7 +2711,7 @@ class DefinesHandler(HasTraits): class OverridesHandler(DefinesHandler): child_called = False - @observe('trait') + @observe("trait") def handler(self, change): self.child_called = True @@ -2548,10 +2741,11 @@ def test_subclass_override_not_registered(): class AddsHandler(DefinesHandler): child_called = False - @observe('trait') + @observe("trait") def child_handler(self, change): self.child_called = True + def test_subclass_add_observer(): obj = AddsHandler() obj.trait = 5 @@ -2560,27 +2754,27 @@ def test_subclass_add_observer(): def test_observe_iterables(): - class C(HasTraits): i = Integer() s = Unicode() c = C() recorded = {} + def record(change): - recorded['change'] = change + recorded["change"] = change # observe with names=set - c.observe(record, names={'i', 's'}) + c.observe(record, names={"i", "s"}) c.i = 5 - assert recorded['change'].name == 'i' - assert recorded['change'].new == 5 - c.s = 'hi' - assert recorded['change'].name == 's' - assert recorded['change'].new == 'hi' + assert recorded["change"].name == "i" + assert recorded["change"].new == 5 + c.s = "hi" + assert recorded["change"].name == "s" + assert recorded["change"].new == "hi" # observe with names=custom container with iter, contains - class MyContainer(object): + class MyContainer: def __init__(self, container): self.container = container @@ -2590,17 +2784,17 @@ def test_observe_iterables(): def __contains__(self, key): return key in self.container - c.observe(record, names=MyContainer({'i', 's'})) + c.observe(record, names=MyContainer({"i", "s"})) c.i = 10 - assert recorded['change'].name == 'i' - assert recorded['change'].new == 10 - c.s = 'ok' - assert recorded['change'].name == 's' - assert recorded['change'].new == 'ok' + assert recorded["change"].name == "i" + assert recorded["change"].new == 10 + c.s = "ok" + assert recorded["change"].name == "s" + assert recorded["change"].new == "ok" def test_super_args(): - class SuperRecorder(object): + class SuperRecorder: def __init__(self, *args, **kwargs): self.super_args = args self.super_kwargs = kwargs @@ -2608,12 +2802,13 @@ def test_super_args(): class SuperHasTraits(HasTraits, SuperRecorder): i = Integer() - obj = SuperHasTraits('a1', 'a2', b=10, i=5, c='x') - assert obj.i == 5 - assert not hasattr(obj, 'b') - assert not hasattr(obj, 'c') - assert obj.super_args == ('a1' , 'a2') - assert obj.super_kwargs == {'b': 10 , 'c': 'x'} + obj = SuperHasTraits("a1", "a2", b=10, i=5, c="x") + assert obj.i == 5 + assert not hasattr(obj, "b") + assert not hasattr(obj, "c") + assert obj.super_args == ("a1", "a2") + assert obj.super_kwargs == {"b": 10, "c": "x"} + def test_super_bad_args(): class SuperHasTraits(HasTraits): @@ -2622,22 +2817,23 @@ def test_super_bad_args(): w = ["Passing unrecognized arguments"] with expected_warnings(w): obj = SuperHasTraits(a=1, b=2) - assert obj.a == 1 - assert not hasattr(obj, 'b') + assert obj.a == 1 + assert not hasattr(obj, "b") def test_default_mro(): """Verify that default values follow mro""" + class Base(HasTraits): - trait = Unicode('base') - attr = 'base' + trait = Unicode("base") + attr = "base" class A(Base): pass class B(Base): - trait = Unicode('B') - attr = 'B' + trait = Unicode("B") + attr = "B" class AB(A, B): pass @@ -2645,12 +2841,12 @@ def test_default_mro(): class BA(B, A): pass - assert A().trait == 'base' - assert A().attr == 'base' - assert BA().trait == 'B' - assert BA().attr == 'B' - assert AB().trait == 'B' - assert AB().attr == 'B' + assert A().trait == "base" + assert A().attr == "base" + assert BA().trait == "B" + assert BA().attr == "B" + assert AB().trait == "B" + assert AB().attr == "B" def test_cls_self_argument(): @@ -2663,35 +2859,40 @@ def test_cls_self_argument(): def test_override_default(): class C(HasTraits): - a = Unicode('hard default') + a = Unicode("hard default") + def _a_default(self): - return 'default method' + return "default method" - C._a_default = lambda self: 'overridden' + C._a_default = lambda self: "overridden" c = C() - assert c.a == 'overridden' + assert c.a == "overridden" + def test_override_default_decorator(): class C(HasTraits): - a = Unicode('hard default') - @default('a') + a = Unicode("hard default") + + @default("a") def _a_default(self): - return 'default method' + return "default method" - C._a_default = lambda self: 'overridden' + C._a_default = lambda self: "overridden" c = C() - assert c.a == 'overridden' + assert c.a == "overridden" + def test_override_default_instance(): class C(HasTraits): - a = Unicode('hard default') - @default('a') + a = Unicode("hard default") + + @default("a") def _a_default(self): - return 'default method' + return "default method" c = C() - c._a_default = lambda self: 'overridden' - assert c.a == 'overridden' + c._a_default = lambda self: "overridden" + assert c.a == "overridden" def test_copy_HasTraits(): diff --git a/contrib/python/traitlets/py3/traitlets/tests/test_traitlets_enum.py b/contrib/python/traitlets/py3/traitlets/tests/test_traitlets_enum.py index 769e830b33..892a8451a1 100644 --- a/contrib/python/traitlets/py3/traitlets/tests/test_traitlets_enum.py +++ b/contrib/python/traitlets/py3/traitlets/tests/test_traitlets_enum.py @@ -3,21 +3,23 @@ Test the trait-type ``UseEnum``. """ -import unittest import enum -from traitlets import HasTraits, TraitError, Enum, UseEnum, CaselessStrEnum, FuzzyEnum +import unittest +from traitlets import CaselessStrEnum, Enum, FuzzyEnum, HasTraits, TraitError, UseEnum # ----------------------------------------------------------------------------- # TEST SUPPORT: # ----------------------------------------------------------------------------- + class Color(enum.Enum): red = 1 green = 2 blue = 3 yellow = 4 + class OtherColor(enum.Enum): red = 0 green = 1 @@ -30,7 +32,7 @@ class CSColor(enum.Enum): YeLLoW = 4 -color_choices = 'red Green BLUE YeLLoW'.split() +color_choices = "red Green BLUE YeLLoW".split() # ----------------------------------------------------------------------------- @@ -49,7 +51,7 @@ class TestUseEnum(unittest.TestCase): def test_assign_all_enum_values(self): # pylint: disable=no-member - enum_values = [value for value in Color.__members__.values()] + enum_values = [value for value in Color.__members__.values()] for value in enum_values: self.assertIsInstance(value, Color) example = self.Example() @@ -107,8 +109,7 @@ class TestUseEnum(unittest.TestCase): def test_assign_enum_value_number(self): # -- CONVERT: number => Enum value (item) # pylint: disable=no-member - enum_numbers = [enum_val.value - for enum_val in Color.__members__.values()] + enum_numbers = [enum_val.value for enum_val in Color.__members__.values()] for value in enum_numbers: self.assertIsInstance(value, int) example = self.Example() @@ -142,12 +143,12 @@ class TestUseEnum(unittest.TestCase): example = Example2() self.assertEqual(example.color, Color.green) - def test_ctor_with_default_value_none_and_not_allow_none(self): # -- IMPLICIT: default_value = Color.red (first enum-value) class Example2(HasTraits): color1 = UseEnum(Color, default_value=None, allow_none=False) color2 = UseEnum(Color, default_value=None) + example = Example2() self.assertEqual(example.color1, Color.red) self.assertEqual(example.color2, Color.red) @@ -189,51 +190,50 @@ class TestUseEnum(unittest.TestCase): example.color = "BAD_VALUE" def test_info(self): - import sys - choices = color_choices + class Example(HasTraits): enum1 = Enum(choices, allow_none=False) enum2 = CaselessStrEnum(choices, allow_none=False) enum3 = FuzzyEnum(choices, allow_none=False) enum4 = UseEnum(CSColor, allow_none=False) - for i in range(1,5): - attr = 'enum%s' % i + for i in range(1, 5): + attr = "enum%s" % i enum = getattr(Example, attr) enum.allow_none = True info = enum.info() - self.assertEqual(len(info.split(', ')), len(choices), info.split(', ')) - self.assertIn('or None', info) + self.assertEqual(len(info.split(", ")), len(choices), info.split(", ")) + self.assertIn("or None", info) info = enum.info_rst() - self.assertEqual(len(info.split('|')), len(choices), info.split('|')) - self.assertIn('or `None`', info) - ## Check no single `\` exists. - self.assertNotRegex(info, r'\b\\\b') + self.assertEqual(len(info.split("|")), len(choices), info.split("|")) + self.assertIn("or `None`", info) + # Check no single `\` exists. + self.assertNotRegex(info, r"\b\\\b") enum.allow_none = False info = enum.info() - self.assertEqual(len(info.split(', ')), len(choices), info.split(', ')) - self.assertNotIn('None', info) + self.assertEqual(len(info.split(", ")), len(choices), info.split(", ")) + self.assertNotIn("None", info) info = enum.info_rst() - self.assertEqual(len(info.split('|')), len(choices), info.split('|')) - self.assertNotIn('None', info) - ## Check no single `\` exists. - self.assertNotRegex(info, r'\b\\\b') - + self.assertEqual(len(info.split("|")), len(choices), info.split("|")) + self.assertNotIn("None", info) + # Check no single `\` exists. + self.assertNotRegex(info, r"\b\\\b") # ----------------------------------------------------------------------------- # TESTSUITE: # ----------------------------------------------------------------------------- + class TestFuzzyEnum(unittest.TestCase): - ## Check mostly `validate()`, Ctor must be checked on generic `Enum` + # Check mostly `validate()`, Ctor must be checked on generic `Enum` # or `CaselessStrEnum`. def test_search_all_prefixes__overwrite(self): @@ -276,8 +276,7 @@ class TestFuzzyEnum(unittest.TestCase): def test_search_substrings__overwrite(self): class FuzzyExample(HasTraits): - color = FuzzyEnum(color_choices, help="Color enum", - substring_matching=True) + color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True) example = FuzzyExample() for color in color_choices: @@ -295,8 +294,7 @@ class TestFuzzyEnum(unittest.TestCase): def test_search_substrings__ctor(self): class FuzzyExample(HasTraits): - color = FuzzyEnum(color_choices, help="Color enum", - substring_matching=True) + color = FuzzyEnum(color_choices, help="Color enum", substring_matching=True) color = color_choices[-1] # 'YeLLoW' for end in (-1, len(color)): @@ -314,52 +312,55 @@ class TestFuzzyEnum(unittest.TestCase): def test_assign_other_raises(self): def new_trait_class(case_sensitive, substring_matching): class Example(HasTraits): - color = FuzzyEnum(color_choices, - case_sensitive=case_sensitive, - substring_matching=substring_matching) + color = FuzzyEnum( + color_choices, + case_sensitive=case_sensitive, + substring_matching=substring_matching, + ) return Example example = new_trait_class(case_sensitive=False, substring_matching=False)() with self.assertRaises(TraitError): - example.color = '' + example.color = "" with self.assertRaises(TraitError): - example.color = 'BAD COLOR' + example.color = "BAD COLOR" with self.assertRaises(TraitError): - example.color = 'ed' + example.color = "ed" example = new_trait_class(case_sensitive=True, substring_matching=False)() with self.assertRaises(TraitError): - example.color = '' + example.color = "" with self.assertRaises(TraitError): - example.color = 'Red' # not 'red' + example.color = "Red" # not 'red' example = new_trait_class(case_sensitive=True, substring_matching=True)() with self.assertRaises(TraitError): - example.color = '' + example.color = "" with self.assertRaises(TraitError): - example.color = 'BAD COLOR' + example.color = "BAD COLOR" with self.assertRaises(TraitError): - example.color = 'green' # not 'Green' + example.color = "green" # not 'Green' with self.assertRaises(TraitError): - example.color = 'lue' # not (b)'LUE' + example.color = "lue" # not (b)'LUE' with self.assertRaises(TraitError): - example.color = 'lUE' # not (b)'LUE' + example.color = "lUE" # not (b)'LUE' example = new_trait_class(case_sensitive=False, substring_matching=True)() with self.assertRaises(TraitError): - example.color = '' + example.color = "" with self.assertRaises(TraitError): - example.color = 'BAD COLOR' + example.color = "BAD COLOR" def test_ctor_with_default_value(self): - def new_trait_class(default_value, - case_sensitive, substring_matching): + def new_trait_class(default_value, case_sensitive, substring_matching): class Example(HasTraits): - color = FuzzyEnum(color_choices, - default_value=default_value, - case_sensitive=case_sensitive, - substring_matching=substring_matching) + color = FuzzyEnum( + color_choices, + default_value=default_value, + case_sensitive=case_sensitive, + substring_matching=substring_matching, + ) return Example @@ -374,7 +375,6 @@ class TestFuzzyEnum(unittest.TestCase): example = new_trait_class(color, True, False)() self.assertEqual(example.color, color) - ## FIXME: default value not validated! - #with self.assertRaises(TraitError): + # FIXME: default value not validated! + # with self.assertRaises(TraitError): # example = new_trait_class(color.lower(), True, False) - diff --git a/contrib/python/traitlets/py3/traitlets/tests/utils.py b/contrib/python/traitlets/py3/traitlets/tests/utils.py index c5ac591435..1a4caf7a5c 100644 --- a/contrib/python/traitlets/py3/traitlets/tests/utils.py +++ b/contrib/python/traitlets/py3/traitlets/tests/utils.py @@ -1,25 +1,25 @@ -from subprocess import Popen, PIPE import sys +from subprocess import PIPE, Popen import os def get_output_error_code(cmd): """Get stdout, stderr, and exit code from running a command""" env = os.environ.copy() - env['Y_PYTHON_ENTRY_POINT'] = ':main' + env["Y_PYTHON_ENTRY_POINT"] = ":main" p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env) out, err = p.communicate() - out = out.decode('utf8', 'replace') - err = err.decode('utf8', 'replace') + out = out.decode("utf8", "replace") + err = err.decode("utf8", "replace") return out, err, p.returncode def check_help_output(pkg, subcommand=None): """test that `python -m PKG [subcommand] -h` works""" - cmd = [sys.executable, '-m', pkg] + cmd = [sys.executable, "-m", pkg] if subcommand: cmd.extend(subcommand) - cmd.append('-h') + cmd.append("-h") out, err, rc = get_output_error_code(cmd) assert rc == 0, err assert "Traceback" not in err @@ -30,10 +30,10 @@ def check_help_output(pkg, subcommand=None): def check_help_all_output(pkg, subcommand=None): """test that `python -m PKG --help-all` works""" - cmd = [sys.executable, '-m', pkg] + cmd = [sys.executable, "-m", pkg] if subcommand: cmd.extend(subcommand) - cmd.append('--help-all') + cmd.append("--help-all") out, err, rc = get_output_error_code(cmd) assert rc == 0, err assert "Traceback" not in err diff --git a/contrib/python/traitlets/py3/traitlets/traitlets.py b/contrib/python/traitlets/py3/traitlets/traitlets.py index 6bdf7414d3..0927222163 100644 --- a/contrib/python/traitlets/py3/traitlets/traitlets.py +++ b/contrib/python/traitlets/py3/traitlets/traitlets.py @@ -39,21 +39,22 @@ Inheritance diagram: # Adapted from enthought.traits, Copyright (c) Enthought, Inc., # also under the terms of the Modified BSD License. -from ast import literal_eval import contextlib +import enum import inspect import os import re import sys import types -import enum +import typing as t +from ast import literal_eval from warnings import warn, warn_explicit +from .utils.bunch import Bunch +from .utils.descriptions import add_article, class_of, describe, repr_type from .utils.getargspec import getargspec from .utils.importstring import import_item from .utils.sentinel import Sentinel -from .utils.bunch import Bunch -from .utils.descriptions import describe, class_of, add_article, repr_type SequenceTypes = (list, tuple, set, frozenset) @@ -63,69 +64,117 @@ ClassTypes = (type,) # exports: __all__ = [ - "default", - "validate", - "observe", - "observe_compat", - "link", - "directional_link", - "dlink", - "Undefined", "All", - "NoDefaultSpecified", - "TraitError", + "Any", + "BaseDescriptor", + "Bool", + "Bytes", + "CBool", + "CBytes", + "CComplex", + "CFloat", + "CInt", + "CRegExp", + "CUnicode", + "Callable", + "CaselessStrEnum", + "ClassBasedTraitType", + "Complex", + "Container", + "DefaultHandler", + "Dict", + "DottedObjectName", + "Enum", + "EventHandler", + "Float", + "ForwardDeclaredInstance", + "ForwardDeclaredMixin", + "ForwardDeclaredType", + "FuzzyEnum", "HasDescriptors", "HasTraits", + "Instance", + "Int", + "List", "MetaHasDescriptors", "MetaHasTraits", - "BaseDescriptor", + "ObjectName", + "ObserveHandler", + "Set", + "TCPAddress", + "This", + "TraitError", "TraitType", + "Tuple", + "Type", + "Unicode", + "Undefined", + "Union", + "UseEnum", + "ValidateHandler", + "default", + "directional_link", + "dlink", + "link", + "observe", + "observe_compat", "parse_notifier_name", + "validate", ] # any TraitType subclass (that doesn't start with _) will be added automatically -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Basic classes -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- -Undefined = Sentinel('Undefined', 'traitlets', -''' +Undefined = Sentinel( + "Undefined", + "traitlets", + """ Used in Traitlets to specify that no defaults are set in kwargs -''' +""", ) -All = Sentinel('All', 'traitlets', -''' +All = Sentinel( + "All", + "traitlets", + """ Used in Traitlets to listen to all types of notification or to notifications from all trait attributes. -''' +""", ) # Deprecated alias NoDefaultSpecified = Undefined + class TraitError(Exception): pass -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Utilities -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$") + def isidentifier(s): return s.isidentifier() + _deprecations_shown = set() + + def _should_warn(key): """Add our own checks for too many deprecation warnings. Limit to once per package. """ - env_flag = os.environ.get('TRAITLETS_ALL_DEPRECATIONS') - if env_flag and env_flag != '0': + env_flag = os.environ.get("TRAITLETS_ALL_DEPRECATIONS") + if env_flag and env_flag != "0": return True if key not in _deprecations_shown: @@ -134,6 +183,7 @@ def _should_warn(key): else: return False + def _deprecated_method(method, cls, method_name, msg): """Show deprecation warning about a magic method definition. @@ -149,7 +199,7 @@ def _deprecated_method(method, cls, method_name, msg): cls = parent break # limit deprecation messages to once per package - package_name = cls.__module__.split('.', 1)[0] + package_name = cls.__module__.split(".", 1)[0] key = (package_name, msg) if not _should_warn(key): return @@ -158,10 +208,11 @@ def _deprecated_method(method, cls, method_name, msg): lineno = inspect.getsourcelines(method)[1] or 0 except (OSError, TypeError) as e: # Failed to inspect for some reason - warn(warn_msg + ('\n(inspection failed) %s' % e), DeprecationWarning) + warn(warn_msg + ("\n(inspection failed) %s" % e), DeprecationWarning) else: warn_explicit(warn_msg, DeprecationWarning, fname, lineno) + def _safe_literal_eval(s): """Safely evaluate an expression @@ -174,11 +225,10 @@ def _safe_literal_eval(s): except (NameError, SyntaxError, ValueError): return s + def is_trait(t): - """ Returns whether the given value is an instance or subclass of TraitType. - """ - return (isinstance(t, TraitType) or - (isinstance(t, type) and issubclass(t, TraitType))) + """Returns whether the given value is an instance or subclass of TraitType.""" + return isinstance(t, TraitType) or (isinstance(t, type) and issubclass(t, TraitType)) def parse_notifier_name(names): @@ -187,13 +237,13 @@ def parse_notifier_name(names): Examples -------- >>> parse_notifier_name([]) - [All] + [traitlets.All] >>> parse_notifier_name("a") ['a'] >>> parse_notifier_name(["a", "b"]) ['a', 'b'] >>> parse_notifier_name(All) - [All] + [traitlets.All] """ if names is All or isinstance(names, str): return [names] @@ -207,11 +257,15 @@ def parse_notifier_name(names): class _SimpleTest: - def __init__ ( self, value ): self.value = value - def __call__ ( self, test ): + def __init__(self, value): + self.value = value + + def __call__(self, test): return test == self.value + def __repr__(self): return "<SimpleTest(%r)" % self.value + def __str__(self): return self.__repr__() @@ -235,18 +289,22 @@ def getmembers(object, predicate=None): results.sort() return results + def _validate_link(*tuples): """Validate arguments for traitlet link functions""" - for t in tuples: - if not len(t) == 2: - raise TypeError("Each linked traitlet must be specified as (HasTraits, 'trait_name'), not %r" % t) - obj, trait_name = t + for tup in tuples: + if not len(tup) == 2: + raise TypeError( + "Each linked traitlet must be specified as (HasTraits, 'trait_name'), not %r" % t + ) + obj, trait_name = tup if not isinstance(obj, HasTraits): raise TypeError("Each object must be HasTraits, not %r" % type(obj)) - if not trait_name in obj.traits(): - raise TypeError("%r has no trait %r" % (obj, trait_name)) + if trait_name not in obj.traits(): + raise TypeError(f"{obj!r} has no trait {trait_name!r}") + -class link(object): +class link: """Link traits from different objects together so they remain in sync. Parameters @@ -258,23 +316,35 @@ class link(object): Examples -------- + >>> class X(HasTraits): + ... value = Int() + + >>> src = X(value=1) + >>> tgt = X(value=42) >>> c = link((src, "value"), (tgt, "value")) - >>> src.value = 5 # updates other objects as well + + Setting source updates target objects: + >>> src.value = 5 + >>> tgt.value + 5 """ + updating = False def __init__(self, source, target, transform=None): _validate_link(source, target) self.source, self.target = source, target - self._transform, self._transform_inv = ( - transform if transform else (lambda x: x,) * 2) + self._transform, self._transform_inv = transform if transform else (lambda x: x,) * 2 self.link() def link(self): try: - setattr(self.target[0], self.target[1], - self._transform(getattr(self.source[0], self.source[1]))) + setattr( + self.target[0], + self.target[1], + self._transform(getattr(self.source[0], self.source[1])), + ) finally: self.source[0].observe(self._update_target, names=self.source[1]) @@ -296,25 +366,26 @@ class link(object): if getattr(self.source[0], self.source[1]) != change.new: raise TraitError( "Broken link {}: the source value changed while updating " - "the target.".format(self)) + "the target.".format(self) + ) def _update_source(self, change): if self.updating: return with self._busy_updating(): - setattr(self.source[0], self.source[1], - self._transform_inv(change.new)) + setattr(self.source[0], self.source[1], self._transform_inv(change.new)) if getattr(self.target[0], self.target[1]) != change.new: raise TraitError( "Broken link {}: the target value changed while updating " - "the source.".format(self)) + "the source.".format(self) + ) def unlink(self): self.source[0].unobserve(self._update_target, names=self.source[1]) self.target[0].unobserve(self._update_source, names=self.target[1]) -class directional_link(object): +class directional_link: """Link the trait of a source object with traits of target objects. Parameters @@ -326,10 +397,25 @@ class directional_link(object): Examples -------- + >>> class X(HasTraits): + ... value = Int() + + >>> src = X(value=1) + >>> tgt = X(value=42) >>> c = directional_link((src, "value"), (tgt, "value")) - >>> src.value = 5 # updates target objects - >>> tgt.value = 6 # does not update source object + + Setting source updates target objects: + >>> src.value = 5 + >>> tgt.value + 5 + + Setting target does not update source object: + >>> tgt.value = 6 + >>> src.value + 5 + """ + updating = False def __init__(self, source, target, transform=None): @@ -340,8 +426,11 @@ class directional_link(object): def link(self): try: - setattr(self.target[0], self.target[1], - self._transform(getattr(self.source[0], self.source[1]))) + setattr( + self.target[0], + self.target[1], + self._transform(getattr(self.source[0], self.source[1])), + ) finally: self.source[0].observe(self._update, names=self.source[1]) @@ -357,21 +446,21 @@ class directional_link(object): if self.updating: return with self._busy_updating(): - setattr(self.target[0], self.target[1], - self._transform(change.new)) + setattr(self.target[0], self.target[1], self._transform(change.new)) def unlink(self): self.source[0].unobserve(self._update, names=self.source[1]) + dlink = directional_link -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Base Descriptor Class -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- -class BaseDescriptor(object): +class BaseDescriptor: """Base descriptor class Notes @@ -390,8 +479,8 @@ class BaseDescriptor(object): accept superclasses for :class:`This` values. """ - name = None - this_class = None + name: t.Optional[str] = None + this_class: t.Optional[t.Type[t.Any]] = None def class_init(self, cls, name): """Part of the initialization which may depend on the underlying @@ -423,25 +512,31 @@ class BaseDescriptor(object): class TraitType(BaseDescriptor): - """A base class for all trait types. - """ + """A base class for all trait types.""" - metadata = {} + metadata: t.Dict[str, t.Any] = {} allow_none = False read_only = False - info_text = 'any value' - default_value = Undefined + info_text = "any value" + default_value: t.Optional[t.Any] = Undefined - def __init__(self, default_value=Undefined, allow_none=False, read_only=None, help=None, - config=None, **kwargs): + def __init__( + self, + default_value=Undefined, + allow_none=False, + read_only=None, + help=None, + config=None, + **kwargs, + ): """Declare a traitlet. If *allow_none* is True, None is a valid value in addition to any values that are normally valid. The default is up to the subclass. For most trait types, the default value for ``allow_none`` is False. - + If *read_only* is True, attempts to directly modify a trait attribute raises a TraitError. - + Extra metadata can be associated with the traitlet using the .tag() convenience method or by using the traitlet instance's .metadata dictionary. """ @@ -451,23 +546,28 @@ class TraitType(BaseDescriptor): self.allow_none = allow_none if read_only is not None: self.read_only = read_only - self.help = help if help is not None else '' + self.help = help if help is not None else "" if len(kwargs) > 0: stacklevel = 1 f = inspect.currentframe() # count supers to determine stacklevel for warning - while f.f_code.co_name == '__init__': + assert f is not None + while f.f_code.co_name == "__init__": stacklevel += 1 f = f.f_back - mod = f.f_globals.get('__name__') or '' - pkg = mod.split('.', 1)[0] - key = tuple(['metadata-tag', pkg] + sorted(kwargs)) + assert f is not None + mod = f.f_globals.get("__name__") or "" + pkg = mod.split(".", 1)[0] + key = tuple(["metadata-tag", pkg] + sorted(kwargs)) if _should_warn(key): - warn("metadata %s was set from the constructor. " - "With traitlets 4.1, metadata should be set using the .tag() method, " - "e.g., Int().tag(key1='value1', key2='value2')" % (kwargs,), - DeprecationWarning, stacklevel=stacklevel) + warn( + "metadata %s was set from the constructor. " + "With traitlets 4.1, metadata should be set using the .tag() method, " + "e.g., Int().tag(key1='value1', key2='value2')" % (kwargs,), + DeprecationWarning, + stacklevel=stacklevel, + ) if len(self.metadata) > 0: self.metadata = self.metadata.copy() self.metadata.update(kwargs) @@ -476,12 +576,12 @@ class TraitType(BaseDescriptor): else: self.metadata = self.metadata.copy() if config is not None: - self.metadata['config'] = config + self.metadata["config"] = config # We add help to the metadata during a deprecation period so that # code that looks for the help string there can find it. if help is not None: - self.metadata['help'] = help + self.metadata["help"] = help def from_string(self, s): """Get a value from a config string @@ -495,7 +595,7 @@ class TraitType(BaseDescriptor): .. versionadded:: 5.0 """ - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None return s @@ -509,8 +609,8 @@ class TraitType(BaseDescriptor): """ if self.default_value is not Undefined: return self.default_value - elif hasattr(self, 'make_dynamic_default'): - return self.make_dynamic_default() + elif hasattr(self, "make_dynamic_default"): + return self.make_dynamic_default() # type:ignore[attr-defined] else: # Undefined will raise in TraitType.get return self.default_value @@ -519,15 +619,20 @@ class TraitType(BaseDescriptor): """DEPRECATED: Retrieve the static default value for this trait. Use self.default_value instead """ - warn("get_default_value is deprecated in traitlets 4.0: use the .default_value attribute", DeprecationWarning, - stacklevel=2) + warn( + "get_default_value is deprecated in traitlets 4.0: use the .default_value attribute", + DeprecationWarning, + stacklevel=2, + ) return self.default_value def init_default_value(self, obj): - """DEPRECATED: Set the static default value for the trait type. - """ - warn("init_default_value is deprecated in traitlets 4.0, and may be removed in the future", DeprecationWarning, - stacklevel=2) + """DEPRECATED: Set the static default value for the trait type.""" + warn( + "init_default_value is deprecated in traitlets 4.0, and may be removed in the future", + DeprecationWarning, + stacklevel=2, + ) value = self._validate(obj, self.default_value) obj._trait_values[self.name] = value return value @@ -544,22 +649,23 @@ class TraitType(BaseDescriptor): "is deprecated in traitlets 5.0, and may cause " "exceptions in the future.", DeprecationWarning, - stacklevel=2 + stacklevel=2, ) with obj.cross_validation_lock: value = self._validate(obj, default) obj._trait_values[self.name] = value - obj._notify_observers(Bunch( - name=self.name, - value=value, - owner=obj, - type='default', - )) + obj._notify_observers( + Bunch( + name=self.name, + value=value, + owner=obj, + type="default", + ) + ) return value except Exception: # This should never be reached. - raise TraitError('Unexpected error in TraitType: ' - 'default value not set properly') + raise TraitError("Unexpected error in TraitType: default value not set properly") else: return value @@ -608,21 +714,25 @@ class TraitType(BaseDescriptor): def _validate(self, obj, value): if value is None and self.allow_none: return value - if hasattr(self, 'validate'): - value = self.validate(obj, value) + if hasattr(self, "validate"): + value = self.validate(obj, value) # type:ignore[attr-defined] if obj._cross_validation_lock is False: value = self._cross_validate(obj, value) return value def _cross_validate(self, obj, value): if self.name in obj._trait_validators: - proposal = Bunch({'trait': self, 'value': value, 'owner': obj}) + proposal = Bunch({"trait": self, "value": value, "owner": obj}) value = obj._trait_validators[self.name](obj, proposal) - elif hasattr(obj, '_%s_validate' % self.name): - meth_name = '_%s_validate' % self.name + elif hasattr(obj, "_%s_validate" % self.name): + meth_name = "_%s_validate" % self.name cross_validate = getattr(obj, meth_name) - _deprecated_method(cross_validate, obj.__class__, meth_name, - "use @validate decorator instead.") + _deprecated_method( + cross_validate, + obj.__class__, + meth_name, + "use @validate decorator instead.", + ) value = cross_validate(value, self) return value @@ -668,13 +778,28 @@ class TraitType(BaseDescriptor): # this is the root trait that must format the final message chain = " of ".join(describe("a", t) for t in error.args[2:]) if obj is not None: - error.args = ("The '%s' trait of %s instance contains %s which " - "expected %s, not %s." % (self.name, describe("an", obj), - chain, error.args[1], describe("the", error.args[0])),) + error.args = ( + "The '%s' trait of %s instance contains %s which " + "expected %s, not %s." + % ( + self.name, + describe("an", obj), + chain, + error.args[1], + describe("the", error.args[0]), + ), + ) else: - error.args = ("The '%s' trait contains %s which " - "expected %s, not %s." % (self.name, chain, - error.args[1], describe("the", error.args[0])),) + error.args = ( + "The '%s' trait contains %s which " + "expected %s, not %s." + % ( + self.name, + chain, + error.args[1], + describe("the", error.args[0]), + ), + ) raise error else: # this trait caused an error @@ -684,11 +809,18 @@ class TraitType(BaseDescriptor): else: # this is the root trait if obj is not None: - e = "The '%s' trait of %s instance expected %s, not %s." % ( - self.name, class_of(obj), self.info(), describe("the", value)) + e = "The '{}' trait of {} instance expected {}, not {}.".format( + self.name, + class_of(obj), + self.info(), + describe("the", value), + ) else: - e = "The '%s' trait expected %s, not %s." % ( - self.name, self.info(), describe("the", value)) + e = "The '{}' trait expected {}, not {}.".format( + self.name, + self.info(), + describe("the", value), + ) raise TraitError(e) def get_metadata(self, key, default=None): @@ -696,7 +828,7 @@ class TraitType(BaseDescriptor): Use .metadata[key] or .metadata.get(key, default) instead. """ - if key == 'help': + if key == "help": msg = "use the instance .help string directly, like x.help" else: msg = "use the instance .metadata dictionary directly, like x.metadata[key] or x.metadata.get(key, default)" @@ -708,7 +840,7 @@ class TraitType(BaseDescriptor): Use .metadata[key] = value instead. """ - if key == 'help': + if key == "help": msg = "use the instance .help string directly, like x.help = value" else: msg = "use the instance .metadata dictionary directly, like x.metadata[key] = value" @@ -720,12 +852,22 @@ class TraitType(BaseDescriptor): This allows convenient metadata tagging when initializing the trait, such as: + Examples + -------- >>> Int(0).tag(config=True, sync=True) + <traitlets.traitlets.Int object at ...> + """ - maybe_constructor_keywords = set(metadata.keys()).intersection({'help','allow_none', 'read_only', 'default_value'}) + maybe_constructor_keywords = set(metadata.keys()).intersection( + {"help", "allow_none", "read_only", "default_value"} + ) if maybe_constructor_keywords: - warn('The following attributes are set in using `tag`, but seem to be constructor keywords arguments: %s '% - maybe_constructor_keywords, UserWarning, stacklevel=2) + warn( + "The following attributes are set in using `tag`, but seem to be constructor keywords arguments: %s " + % maybe_constructor_keywords, + UserWarning, + stacklevel=2, + ) self.metadata.update(metadata) return self @@ -733,11 +875,13 @@ class TraitType(BaseDescriptor): def default_value_repr(self): return repr(self.default_value) -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # The HasTraits implementation -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- -class _CallbackWrapper(object): + +class _CallbackWrapper: """An object adapting a on_trait_change callback into an observe callback. The comparison operator __eq__ is implemented to enable removal of wrapped @@ -749,8 +893,8 @@ class _CallbackWrapper(object): # Bound methods have an additional 'self' argument. offset = -1 if isinstance(self.cb, types.MethodType) else 0 self.nargs = len(getargspec(cb)[0]) + offset - if (self.nargs > 4): - raise TraitError('a trait changed callback must have 0-4 arguments.') + if self.nargs > 4: + raise TraitError("a trait changed callback must have 0-4 arguments.") def __eq__(self, other): # The wrapper is equal to the wrapped element @@ -772,6 +916,7 @@ class _CallbackWrapper(object): elif self.nargs == 4: self.cb(change.name, change.old, change.new, change.owner) + def _callback_wrapper(cb): if isinstance(cb, _CallbackWrapper): return cb @@ -793,17 +938,20 @@ class MetaHasDescriptors(type): # Support of deprecated behavior allowing for TraitType types # to be used instead of TraitType instances. if inspect.isclass(v) and issubclass(v, TraitType): - warn("Traits should be given as instances, not types (for example, `Int()`, not `Int`)." - " Passing types is deprecated in traitlets 4.1.", - DeprecationWarning, stacklevel=2) + warn( + "Traits should be given as instances, not types (for example, `Int()`, not `Int`)." + " Passing types is deprecated in traitlets 4.1.", + DeprecationWarning, + stacklevel=2, + ) classdict[k] = v() # ---------------------------------------------------------------- - return super(MetaHasDescriptors, mcls).__new__(mcls, name, bases, classdict) + return super().__new__(mcls, name, bases, classdict) def __init__(cls, name, bases, classdict): """Finish initializing the HasDescriptors class.""" - super(MetaHasDescriptors, cls).__init__(name, bases, classdict) + super().__init__(name, bases, classdict) cls.setup_class(classdict) def setup_class(cls, classdict): @@ -817,7 +965,7 @@ class MetaHasDescriptors(type): if isinstance(v, BaseDescriptor): v.class_init(cls, k) - for k, v in getmembers(cls): + for _, v in getmembers(cls): if isinstance(v, BaseDescriptor): v.subclass_init(cls) @@ -827,7 +975,7 @@ class MetaHasTraits(MetaHasDescriptors): def setup_class(cls, classdict): cls._trait_default_generators = {} - super(MetaHasTraits, cls).setup_class(classdict) + super().setup_class(classdict) def observe(*names, type="change"): @@ -873,21 +1021,26 @@ def observe_compat(func): With this, `super()._foo_changed(self, name, old, new)` in subclasses will still work. Allows adoption of new observer API without breaking subclasses that override and super. """ + def compatible_observer(self, change_or_name, old=Undefined, new=Undefined): if isinstance(change_or_name, dict): change = change_or_name else: clsname = self.__class__.__name__ - warn("A parent of %s._%s_changed has adopted the new (traitlets 4.1) @observe(change) API" % ( - clsname, change_or_name), DeprecationWarning) + warn( + "A parent of %s._%s_changed has adopted the new (traitlets 4.1) @observe(change) API" + % (clsname, change_or_name), + DeprecationWarning, + ) change = Bunch( - type='change', + type="change", old=old, new=new, name=change_or_name, owner=self, ) return func(self, change) + return compatible_observer @@ -925,7 +1078,7 @@ def validate(*names): def default(name): - """ A decorator which assigns a dynamic default for a Trait on a HasTraits object. + """A decorator which assigns a dynamic default for a Trait on a HasTraits object. Parameters ---------- @@ -966,14 +1119,13 @@ def default(name): class EventHandler(BaseDescriptor): - def _init_call(self, func): self.func = func return self def __call__(self, *args, **kwargs): """Pass `*args` and `**kwargs` to the handler's function if it exists.""" - if hasattr(self, 'func'): + if hasattr(self, "func"): return self.func(*args, **kwargs) else: return self._init_call(*args, **kwargs) @@ -985,7 +1137,6 @@ class EventHandler(BaseDescriptor): class ObserveHandler(EventHandler): - def __init__(self, names, type): self.trait_names = names self.type = type @@ -995,7 +1146,6 @@ class ObserveHandler(EventHandler): class ValidateHandler(EventHandler): - def __init__(self, names): self.trait_names = names @@ -1004,7 +1154,6 @@ class ValidateHandler(EventHandler): class DefaultHandler(EventHandler): - def __init__(self, name): self.trait_name = name @@ -1014,10 +1163,9 @@ class DefaultHandler(EventHandler): class HasDescriptors(metaclass=MetaHasDescriptors): - """The base class for all classes that have descriptors. - """ + """The base class for all classes that have descriptors.""" - def __new__(*args, **kwargs): + def __new__(*args: t.Any, **kwargs: t.Any) -> t.Any: # Pass cls as args[0] to allow "cls" as keyword argument cls = args[0] args = args[1:] @@ -1040,7 +1188,7 @@ class HasDescriptors(metaclass=MetaHasDescriptors): self = args[0] args = args[1:] - self._cross_validation_lock = False + self._cross_validation_lock = False # type:ignore[attr-defined] cls = self.__class__ for key in dir(cls): # Some descriptors raise AttributeError like zope.interface's @@ -1056,6 +1204,10 @@ class HasDescriptors(metaclass=MetaHasDescriptors): class HasTraits(HasDescriptors, metaclass=MetaHasTraits): + _trait_values: t.Dict[str, t.Any] + _trait_notifiers: t.Dict[str, t.Any] + _trait_validators: t.Dict[str, t.Any] + _cross_validation_lock: bool def setup_instance(*args, **kwargs): # Pass self as args[0] to allow "self" as keyword argument @@ -1065,6 +1217,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): self._trait_values = {} self._trait_notifiers = {} self._trait_validators = {} + self._cross_validation_lock = False super(HasTraits, self).setup_instance(*args, **kwargs) def __init__(self, *args, **kwargs): @@ -1081,19 +1234,19 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): # passthrough args that don't set traits to super super_kwargs[key] = value try: - super(HasTraits, self).__init__(*super_args, **super_kwargs) + super().__init__(*super_args, **super_kwargs) except TypeError as e: - arg_s_list = [ repr(arg) for arg in super_args ] + arg_s_list = [repr(arg) for arg in super_args] for k, v in super_kwargs.items(): - arg_s_list.append("%s=%r" % (k, v)) - arg_s = ', '.join(arg_s_list) + arg_s_list.append(f"{k}={v!r}") + arg_s = ", ".join(arg_s_list) warn( "Passing unrecognized arguments to super({classname}).__init__({arg_s}).\n" "{error}\n" "This is deprecated in traitlets 4.2." - "This error will be raised in a future release of traitlets." - .format( - arg_s=arg_s, classname=self.__class__.__name__, + "This error will be raised in a future release of traitlets.".format( + arg_s=arg_s, + classname=self.__class__.__name__, error=e, ), DeprecationWarning, @@ -1105,10 +1258,10 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): # event handlers stored on an instance are # expected to be reinstantiated during a # recall of instance_init during __setstate__ - d['_trait_notifiers'] = {} - d['_trait_validators'] = {} - d['_trait_values'] = self._trait_values.copy() - d['_cross_validation_lock'] = False # FIXME: raise if cloning locked! + d["_trait_notifiers"] = {} + d["_trait_validators"] = {} + d["_trait_values"] = self._trait_values.copy() + d["_cross_validation_lock"] = False # FIXME: raise if cloning locked! return d @@ -1129,7 +1282,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): if isinstance(value, EventHandler): value.instance_init(self) - @property + @property # type:ignore[misc] @contextlib.contextmanager def cross_validation_lock(self): """ @@ -1162,16 +1315,15 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): yield return else: - cache = {} - notify_change = self.notify_change + cache: t.Dict[str, t.Any] = {} def compress(past_changes, change): """Merges the provided change with the last if possible.""" if past_changes is None: return [change] else: - if past_changes[-1]['type'] == 'change' and change.type == 'change': - past_changes[-1]['new'] = change.new + if past_changes[-1]["type"] == "change" and change.type == "change": + past_changes[-1]["new"] = change.new else: # In case of changes other than 'change', append the notification. past_changes.append(change) @@ -1184,7 +1336,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): try: # Replace notify_change with `hold`, caching and compressing # notifications, disable cross validation and yield. - self.notify_change = hold + self.notify_change = hold # type:ignore[assignment] self._cross_validation_lock = True yield # Cross validate final values when context is released. @@ -1194,11 +1346,11 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): self.set_trait(name, value) except TraitError as e: # Roll back in case of TraitError during final cross validation. - self.notify_change = lambda x: None + self.notify_change = lambda x: None # type:ignore[assignment] for name, changes in cache.items(): for change in changes[::-1]: # TODO: Separate in a rollback function per notification type. - if change.type == 'change': + if change.type == "change": if change.old is not Undefined: self.set_trait(name, change.old) else: @@ -1216,13 +1368,15 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): self.notify_change(change) def _notify_trait(self, name, old_value, new_value): - self.notify_change(Bunch( - name=name, - old=old_value, - new=new_value, - owner=self, - type='change', - )) + self.notify_change( + Bunch( + name=name, + old=old_value, + new=new_value, + owner=self, + type="change", + ) + ) def notify_change(self, change): """Notify observers of a change event""" @@ -1238,16 +1392,24 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): callables = [] callables.extend(self._trait_notifiers.get(name, {}).get(type, [])) callables.extend(self._trait_notifiers.get(name, {}).get(All, [])) - callables.extend(self._trait_notifiers.get(All, {}).get(type, [])) - callables.extend(self._trait_notifiers.get(All, {}).get(All, [])) + callables.extend( + self._trait_notifiers.get(All, {}).get(type, []) # type:ignore[call-overload] + ) + callables.extend( + self._trait_notifiers.get(All, {}).get(All, []) # type:ignore[call-overload] + ) # Now static ones - magic_name = '_%s_changed' % name + magic_name = "_%s_changed" % name if event.type == "change" and hasattr(self, magic_name): class_value = getattr(self.__class__, magic_name) if not isinstance(class_value, ObserveHandler): - _deprecated_method(class_value, self.__class__, magic_name, - "use @observe and @unobserve instead.") + _deprecated_method( + class_value, + self.__class__, + magic_name, + "use @observe and @unobserve instead.", + ) cb = getattr(self, magic_name) # Only append the magic method if it was not manually registered if cb not in callables: @@ -1267,7 +1429,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): def _add_notifiers(self, handler, name, type): if name not in self._trait_notifiers: - nlist = [] + nlist: t.List[t.Any] = [] self._trait_notifiers[name] = {type: nlist} else: if type not in self._trait_notifiers[name]: @@ -1315,8 +1477,11 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): If False (the default), then install the handler. If True then unintall it. """ - warn("on_trait_change is deprecated in traitlets 4.1: use observe instead", - DeprecationWarning, stacklevel=2) + warn( + "on_trait_change is deprecated in traitlets 4.1: use observe instead", + DeprecationWarning, + stacklevel=2, + ) if name is None: name = All if remove: @@ -1324,7 +1489,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): else: self.observe(_callback_wrapper(handler), names=name) - def observe(self, handler, names=All, type='change'): + def observe(self, handler, names=All, type="change"): """Setup a handler to be called when a trait changes. This is used to setup dynamic notifications of trait changes. @@ -1354,7 +1519,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): for n in names: self._add_notifiers(handler, n, type) - def unobserve(self, handler, names=All, type='change'): + def unobserve(self, handler, names=All, type="change"): """Remove a trait change handler. This is used to unregister handlers to trait change notifications. @@ -1379,7 +1544,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): """Remove trait change handlers of any type for the specified name. If name is not specified, removes all trait notifiers.""" if name is All: - self._trait_notifiers = {} + self._trait_notifiers: t.Dict[str, t.Any] = {} else: try: del self._trait_notifiers[name] @@ -1407,12 +1572,16 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): The names of the traits that should be cross-validated """ for name in names: - magic_name = '_%s_validate' % name + magic_name = "_%s_validate" % name if hasattr(self, magic_name): class_value = getattr(self.__class__, magic_name) if not isinstance(class_value, ValidateHandler): - _deprecated_method(class_value, self.__class__, magic_name, - "use @validate decorator instead.") + _deprecated_method( + class_value, + self.__class__, + magic_name, + "use @validate decorator instead.", + ) for name in names: self._trait_validators[name] = handler @@ -1421,8 +1590,8 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): cls = self.__class__ attrs = {"__module__": cls.__module__} if hasattr(cls, "__qualname__"): - # __qualname__ introduced in Python 3.3 (see PEP 3155) - attrs["__qualname__"] = cls.__qualname__ + # __qualname__ introduced in Python 3.3 (see PEP 3155) + attrs["__qualname__"] = cls.__qualname__ attrs.update(traits) self.__class__ = type(cls.__name__, (cls,), attrs) for trait in traits.values(): @@ -1432,8 +1601,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): """Forcibly sets trait attribute, including read-only attributes.""" cls = self.__class__ if not self.has_trait(name): - raise TraitError("Class %s does not have a trait named %s" % - (cls.__name__, name)) + raise TraitError(f"Class {cls.__name__} does not have a trait named {name}") else: getattr(cls, name).set(self, value) @@ -1463,8 +1631,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): the output. If a metadata key doesn't exist, None will be passed to the function. """ - traits = dict([memb for memb in getmembers(cls) if - isinstance(memb[1], TraitType)]) + traits = dict([memb for memb in getmembers(cls) if isinstance(memb[1], TraitType)]) if len(metadata) == 0: return traits @@ -1488,8 +1655,11 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): Works like `class_traits`, except for excluding traits from parents. """ sup = super(cls, cls) - return {n: t for (n, t) in cls.class_traits(**metadata).items() - if getattr(sup, n, None) is not t} + return { + n: t + for (n, t) in cls.class_traits(**metadata).items() + if getattr(sup, n, None) is not t + } def has_trait(self, name): """Returns True if the object has a trait with the specified name.""" @@ -1544,7 +1714,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): Walk the MRO to resolve the correct default generator according to inheritance. """ - method_name = '_%s_default' % name + method_name = "_%s_default" % name if method_name in self.__dict__: return getattr(self, method_name) cls = self.__class__ @@ -1553,15 +1723,15 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): # truncate mro to the class on which the trait is defined mro = cls.mro() try: - mro = mro[:mro.index(trait.this_class) + 1] + mro = mro[: mro.index(trait.this_class) + 1] # type:ignore[arg-type] except ValueError: # this_class not in mro pass for c in mro: if method_name in c.__dict__: return getattr(c, method_name) - if name in c.__dict__.get('_trait_default_generators', {}): - return c._trait_default_generators[name] + if name in c.__dict__.get("_trait_default_generators", {}): + return c._trait_default_generators[name] # type:ignore[attr-defined] return trait.default def trait_defaults(self, *names, **metadata): @@ -1573,8 +1743,7 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): depend on the current state of the object.""" for n in names: if not self.has_trait(n): - raise TraitError("'%s' is not a trait of '%s' " - "instances" % (n, type(self).__name__)) + raise TraitError(f"'{n}' is not a trait of '{type(self).__name__}' instances") if len(names) == 1 and len(metadata) == 0: return self._get_trait_default_generator(names[0])(self) @@ -1605,8 +1774,9 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): the output. If a metadata key doesn't exist, None will be passed to the function. """ - traits = dict([memb for memb in getmembers(self.__class__) if - isinstance(memb[1], TraitType)]) + traits = dict( + [memb for memb in getmembers(self.__class__) if isinstance(memb[1], TraitType)] + ) if len(metadata) == 0: return traits @@ -1628,9 +1798,10 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): try: trait = getattr(self.__class__, traitname) except AttributeError: - raise TraitError("Class %s does not have a trait named %s" % - (self.__class__.__name__, traitname)) - metadata_name = '_' + traitname + '_metadata' + raise TraitError( + f"Class {self.__class__.__name__} does not have a trait named {traitname}" + ) + metadata_name = "_" + traitname + "_metadata" if hasattr(self, metadata_name) and key in getattr(self, metadata_name): return getattr(self, metadata_name).get(key, default) else: @@ -1643,8 +1814,11 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): Works like ``event_handlers``, except for excluding traits from parents. """ sup = super(cls, cls) - return {n: e for (n, e) in cls.events(name).items() - if getattr(sup, n, None) is not e} + return { + n: e + for (n, e) in cls.events(name).items() # type:ignore[attr-defined] + if getattr(sup, n, None) is not e + } @classmethod def trait_events(cls, name=None): @@ -1665,20 +1839,21 @@ class HasTraits(HasDescriptors, metaclass=MetaHasTraits): if isinstance(v, EventHandler): if name is None: events[k] = v - elif name in v.trait_names: + elif name in v.trait_names: # type:ignore[attr-defined] events[k] = v - elif hasattr(v, 'tags'): - if cls.trait_names(**v.tags): + elif hasattr(v, "tags"): + if cls.trait_names(**v.tags): # type:ignore[attr-defined] events[k] = v return events -#----------------------------------------------------------------------------- + +# ----------------------------------------------------------------------------- # Actual TraitTypes implementations/subclasses -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # TraitTypes subclasses for handling classes and instances of classes -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class ClassBasedTraitType(TraitType): @@ -1747,10 +1922,12 @@ class Type(ClassBasedTraitType): try: value = self._resolve_string(value) except ImportError: - raise TraitError("The '%s' trait of %s instance must be a type, but " - "%r could not be imported" % (self.name, obj, value)) + raise TraitError( + "The '%s' trait of %s instance must be a type, but " + "%r could not be imported" % (self.name, obj, value) + ) try: - if issubclass(value, self.klass): + if issubclass(value, self.klass): # type:ignore[arg-type] return value except Exception: pass @@ -1758,14 +1935,14 @@ class Type(ClassBasedTraitType): self.error(obj, value) def info(self): - """ Returns a description of the trait.""" + """Returns a description of the trait.""" if isinstance(self.klass, str): klass = self.klass else: - klass = self.klass.__module__ + '.' + self.klass.__name__ + klass = self.klass.__module__ + "." + self.klass.__name__ result = "a subclass of '%s'" % klass if self.allow_none: - return result + ' or None' + return result + " or None" return result def instance_init(self, obj): @@ -1780,10 +1957,11 @@ class Type(ClassBasedTraitType): def default_value_repr(self): value = self.default_value + assert value is not None if isinstance(value, str): return repr(value) else: - return repr(f'{value.__module__}.{value.__name__}') + return repr(f"{value.__module__}.{value.__name__}") class Instance(ClassBasedTraitType): @@ -1831,8 +2009,7 @@ class Instance(ClassBasedTraitType): if (klass is not None) and (inspect.isclass(klass) or isinstance(klass, str)): self.klass = klass else: - raise TraitError('The klass attribute must be a class' - ' not: %r' % klass) + raise TraitError("The klass attribute must be a class not: %r" % klass) if (kw is not None) and not isinstance(kw, dict): raise TraitError("The 'kw' argument must be a dict or None.") @@ -1842,10 +2019,11 @@ class Instance(ClassBasedTraitType): self.default_args = args self.default_kwargs = kw - super(Instance, self).__init__(**kwargs) + super().__init__(**kwargs) def validate(self, obj, value): - if isinstance(value, self.klass): + assert self.klass is not None + if isinstance(value, self.klass): # type:ignore[arg-type] return value else: self.error(obj, value) @@ -1856,7 +2034,7 @@ class Instance(ClassBasedTraitType): else: result = describe("a", self.klass) if self.allow_none: - result += ' or None' + result += " or None" return result def instance_init(self, obj): @@ -1870,8 +2048,10 @@ class Instance(ClassBasedTraitType): def make_dynamic_default(self): if (self.default_args is None) and (self.default_kwargs is None): return None - return self.klass(*(self.default_args or ()), - **(self.default_kwargs or {})) + assert self.klass is not None + return self.klass( + *(self.default_args or ()), **(self.default_kwargs or {}) + ) # type:ignore[operator] def default_value_repr(self): return repr(self.make_dynamic_default()) @@ -1880,23 +2060,25 @@ class Instance(ClassBasedTraitType): return _safe_literal_eval(s) -class ForwardDeclaredMixin(object): +class ForwardDeclaredMixin: """ Mixin for forward-declared versions of Instance and Type. """ + def _resolve_string(self, string): """ Find the specified class name by looking for it in the module in which our this_class attribute was defined. """ - modname = self.this_class.__module__ - return import_item('.'.join([modname, string])) + modname = self.this_class.__module__ # type:ignore[attr-defined] + return import_item(".".join([modname, string])) class ForwardDeclaredType(ForwardDeclaredMixin, Type): """ Forward-declared version of Type. """ + pass @@ -1904,6 +2086,7 @@ class ForwardDeclaredInstance(ForwardDeclaredMixin, Instance): """ Forward-declared version of Instance. """ + pass @@ -1915,15 +2098,16 @@ class This(ClassBasedTraitType): always validate default values, ``allow_none`` is *always* true. """ - info_text = 'an instance of the same type as the receiver or None' + info_text = "an instance of the same type as the receiver or None" def __init__(self, **kwargs): - super(This, self).__init__(None, **kwargs) + super().__init__(None, **kwargs) def validate(self, obj, value): # What if value is a superclass of obj.__class__? This is # complicated if it was the superclass that defined the This # trait. + assert self.this_class is not None if isinstance(value, self.this_class) or (value is None): return value else: @@ -1952,13 +2136,13 @@ class Union(TraitType): """ self.trait_types = list(trait_types) self.info_text = " or ".join([tt.info() for tt in self.trait_types]) - super(Union, self).__init__(**kwargs) + super().__init__(**kwargs) def default(self, obj=None): - default = super(Union, self).default(obj) - for t in self.trait_types: + default = super().default(obj) + for trait in self.trait_types: if default is Undefined: - default = t.default(obj) + default = trait.default(obj) else: break return default @@ -1966,12 +2150,12 @@ class Union(TraitType): def class_init(self, cls, name): for trait_type in reversed(self.trait_types): trait_type.class_init(cls, None) - super(Union, self).class_init(cls, name) + super().class_init(cls, name) def instance_init(self, obj): for trait_type in reversed(self.trait_types): trait_type.instance_init(obj) - super(Union, self).instance_init(obj) + super().instance_init(obj) def validate(self, obj, value): with obj.cross_validation_lock: @@ -1980,7 +2164,7 @@ class Union(TraitType): v = trait_type._validate(obj, value) # In the case of an element trait, the name is None if self.name is not None: - setattr(obj, '_' + self.name + '_metadata', trait_type.metadata) + setattr(obj, "_" + self.name + "_metadata", trait_type.metadata) return v except TraitError: continue @@ -1993,16 +2177,17 @@ class Union(TraitType): return Union(self.trait_types + [other]) -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Basic TraitTypes implementations/subclasses -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- class Any(TraitType): """A trait which allows any value.""" - default_value = None + + default_value: t.Optional[t.Any] = None allow_none = True - info_text = 'any value' + info_text = "any value" def _validate_bounds(trait, obj, value): @@ -2017,15 +2202,17 @@ def _validate_bounds(trait, obj, value): "The value of the '{name}' trait of {klass} instance should " "not be less than {min_bound}, but a value of {value} was " "specified".format( - name=trait.name, klass=class_of(obj), - value=value, min_bound=trait.min)) + name=trait.name, klass=class_of(obj), value=value, min_bound=trait.min + ) + ) if trait.max is not None and value > trait.max: raise TraitError( "The value of the '{name}' trait of {klass} instance should " "not be greater than {max_bound}, but a value of {value} was " "specified".format( - name=trait.name, klass=class_of(obj), - value=value, max_bound=trait.max)) + name=trait.name, klass=class_of(obj), value=value, max_bound=trait.max + ) + ) return value @@ -2033,13 +2220,12 @@ class Int(TraitType): """An int trait.""" default_value = 0 - info_text = 'an int' + info_text = "an int" def __init__(self, default_value=Undefined, allow_none=False, **kwargs): - self.min = kwargs.pop('min', None) - self.max = kwargs.pop('max', None) - super(Int, self).__init__(default_value=default_value, - allow_none=allow_none, **kwargs) + self.min = kwargs.pop("min", None) + self.max = kwargs.pop("max", None) + super().__init__(default_value=default_value, allow_none=allow_none, **kwargs) def validate(self, obj, value): if not isinstance(value, int): @@ -2047,7 +2233,7 @@ class Int(TraitType): return _validate_bounds(self, obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None return int(s) @@ -2071,13 +2257,12 @@ class Float(TraitType): """A float trait.""" default_value = 0.0 - info_text = 'a float' + info_text = "a float" def __init__(self, default_value=Undefined, allow_none=False, **kwargs): - self.min = kwargs.pop('min', -float('inf')) - self.max = kwargs.pop('max', float('inf')) - super(Float, self).__init__(default_value=default_value, - allow_none=allow_none, **kwargs) + self.min = kwargs.pop("min", -float("inf")) + self.max = kwargs.pop("max", float("inf")) + super().__init__(default_value=default_value, allow_none=allow_none, **kwargs) def validate(self, obj, value): if isinstance(value, int): @@ -2087,7 +2272,7 @@ class Float(TraitType): return _validate_bounds(self, obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None return float(s) @@ -2107,7 +2292,7 @@ class Complex(TraitType): """A trait for complex numbers.""" default_value = 0.0 + 0.0j - info_text = 'a complex number' + info_text = "a complex number" def validate(self, obj, value): if isinstance(value, complex): @@ -2117,7 +2302,7 @@ class Complex(TraitType): self.error(obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None return complex(s) @@ -2125,20 +2310,21 @@ class Complex(TraitType): class CComplex(Complex): """A casting version of the complex number trait.""" - def validate (self, obj, value): + def validate(self, obj, value): try: return complex(value) except Exception: self.error(obj, value) + # We should always be explicit about whether we're using bytes or unicode, both # for Python 3 conversion and for reliable unicode behaviour on Python 2. So # we don't have a Str type. class Bytes(TraitType): """A trait for byte strings.""" - default_value = b'' - info_text = 'a bytes object' + default_value = b"" + info_text = "a bytes object" def validate(self, obj, value): if isinstance(value, bytes): @@ -2157,7 +2343,8 @@ class Bytes(TraitType): warn( "Supporting extra quotes around Bytes is deprecated in traitlets 5.0. " "Use %r instead of %r." % (s, old_s), - FutureWarning) + FutureWarning, + ) break return s.encode("utf8") @@ -2175,22 +2362,22 @@ class CBytes(Bytes): class Unicode(TraitType): """A trait for unicode strings.""" - default_value = '' - info_text = 'a unicode string' + default_value = "" + info_text = "a unicode string" def validate(self, obj, value): if isinstance(value, str): return value if isinstance(value, bytes): try: - return value.decode('ascii', 'strict') + return value.decode("ascii", "strict") except UnicodeDecodeError: msg = "Could not decode {!r} for unicode trait '{}' of {} instance." raise TraitError(msg.format(value, self.name, class_of(obj))) self.error(obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None s = os.path.expanduser(s) if len(s) >= 2: @@ -2202,11 +2389,11 @@ class Unicode(TraitType): warn( "Supporting extra quotes around strings is deprecated in traitlets 5.0. " "You can use %r instead of %r if you require traitlets >=5." % (s, old_s), - FutureWarning) + FutureWarning, + ) return s - class CUnicode(Unicode): """A casting version of the unicode trait.""" @@ -2221,9 +2408,10 @@ class ObjectName(TraitType): """A string holding a valid object name in this version of Python. This does not check that the name exists in any scope.""" + info_text = "a valid object identifier in Python" - coerce_str = staticmethod(lambda _,s: s) + coerce_str = staticmethod(lambda _, s: s) # type:ignore[no-any-return] def validate(self, obj, value): value = self.coerce_str(obj, value) @@ -2233,17 +2421,18 @@ class ObjectName(TraitType): self.error(obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None return s + class DottedObjectName(ObjectName): """A string holding a valid dotted object name in Python, such as A.b3._c""" + def validate(self, obj, value): value = self.coerce_str(obj, value) - if isinstance(value, str) and all(isidentifier(a) - for a in value.split('.')): + if isinstance(value, str) and all(isidentifier(a) for a in value.split(".")): return value self.error(obj, value) @@ -2252,7 +2441,7 @@ class Bool(TraitType): """A boolean (True, False) trait.""" default_value = False - info_text = 'a boolean' + info_text = "a boolean" def validate(self, obj, value): if isinstance(value, bool): @@ -2265,12 +2454,12 @@ class Bool(TraitType): self.error(obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None s = s.lower() - if s in {'true', '1'}: + if s in {"true", "1"}: return True - elif s in {'false', '0'}: + elif s in {"false", "0"}: return False else: raise ValueError("%r is not 1, 0, true, or false") @@ -2291,30 +2480,28 @@ class Enum(TraitType): def __init__(self, values, default_value=Undefined, **kwargs): self.values = values - if kwargs.get('allow_none', False) and default_value is Undefined: + if kwargs.get("allow_none", False) and default_value is Undefined: default_value = None - super(Enum, self).__init__(default_value, **kwargs) + super().__init__(default_value, **kwargs) def validate(self, obj, value): if value in self.values: - return value + return value self.error(obj, value) def _choices_str(self, as_rst=False): - """ Returns a description of the trait choices (not none).""" + """Returns a description of the trait choices (not none).""" choices = self.values if as_rst: - choices = '|'.join('``%r``' % x for x in choices) + choices = "|".join("``%r``" % x for x in choices) else: choices = repr(list(choices)) return choices def _info(self, as_rst=False): - """ Returns a description of the trait.""" - none = (' or %s' % ('`None`' if as_rst else 'None') - if self.allow_none else - '') - return 'any of %s%s' % (self._choices_str(as_rst), none) + """Returns a description of the trait.""" + none = " or %s" % ("`None`" if as_rst else "None") if self.allow_none else "" + return f"any of {self._choices_str(as_rst)}{none}" def info(self): return self._info(as_rst=False) @@ -2345,11 +2532,9 @@ class CaselessStrEnum(Enum): self.error(obj, value) def _info(self, as_rst=False): - """ Returns a description of the trait.""" - none = (' or %s' % ('`None`' if as_rst else 'None') - if self.allow_none else - '') - return 'any of %s (case-insensitive)%s' % (self._choices_str(as_rst), none) + """Returns a description of the trait.""" + none = " or %s" % ("`None`" if as_rst else "None") if self.allow_none else "" + return f"any of {self._choices_str(as_rst)} (case-insensitive){none}" def info(self): return self._info(as_rst=False) @@ -2365,8 +2550,14 @@ class FuzzyEnum(Enum): #: If True, choices match anywhere in the string, otherwise match prefixes. substring_matching = False - def __init__(self, values, default_value=Undefined, - case_sensitive=False, substring_matching=False, **kwargs): + def __init__( + self, + values, + default_value=Undefined, + case_sensitive=False, + substring_matching=False, + **kwargs, + ): self.case_sensitive = case_sensitive self.substring_matching = substring_matching super().__init__(values, default_value=default_value, **kwargs) @@ -2377,9 +2568,11 @@ class FuzzyEnum(Enum): conv_func = (lambda c: c) if self.case_sensitive else lambda c: c.lower() substring_matching = self.substring_matching - match_func = ((lambda v, c: v in c) - if substring_matching - else (lambda v, c: c.startswith(v))) + match_func = ( + (lambda v, c: v in c) + if substring_matching + else (lambda v, c: c.startswith(v)) # type:ignore[no-any-return] + ) value = conv_func(value) choices = self.values matches = [match_func(value, conv_func(c)) for c in choices] @@ -2391,15 +2584,11 @@ class FuzzyEnum(Enum): self.error(obj, value) def _info(self, as_rst=False): - """ Returns a description of the trait.""" - none = (' or %s' % ('`None`' if as_rst else 'None') - if self.allow_none else - '') - case = 'sensitive' if self.case_sensitive else 'insensitive' - substr = 'substring' if self.substring_matching else 'prefix' - return 'any case-%s %s of %s%s' % (case, substr, - self._choices_str(as_rst), - none) + """Returns a description of the trait.""" + none = " or %s" % ("`None`" if as_rst else "None") if self.allow_none else "" + case = "sensitive" if self.case_sensitive else "insensitive" + substr = "substring" if self.substring_matching else "prefix" + return f"any case-{case} {substr} of {self._choices_str(as_rst)}{none}" def info(self): return self._info(as_rst=False) @@ -2414,11 +2603,11 @@ class Container(Instance): To be subclassed by overriding klass. """ - klass = None - _cast_types = () + klass: t.Optional[t.Union[str, t.Type[t.Any]]] = None + _cast_types: t.Any = () _valid_defaults = SequenceTypes _trait = None - _literal_from_string_pairs = ("[]", "()") + _literal_from_string_pairs: t.Any = ("[]", "()") def __init__(self, trait=None, default_value=Undefined, **kwargs): """Create a container trait type from a list, set, or tuple. @@ -2468,7 +2657,7 @@ class Container(Instance): default_value = Undefined if default_value is Undefined: - args = () + args: t.Any = () elif default_value is None: # default_value back on kwargs for super() to handle args = () @@ -2476,9 +2665,7 @@ class Container(Instance): elif isinstance(default_value, self._valid_defaults): args = (default_value,) else: - raise TypeError( - "default value of %s was %s" % (self.__class__.__name__, default_value) - ) + raise TypeError(f"default value of {self.__class__.__name__} was {default_value}") if is_trait(trait): if isinstance(trait, type): @@ -2490,16 +2677,15 @@ class Container(Instance): ) self._trait = trait() if isinstance(trait, type) else trait elif trait is not None: - raise TypeError( - "`trait` must be a Trait or None, got %s" % repr_type(trait) - ) + raise TypeError("`trait` must be a Trait or None, got %s" % repr_type(trait)) - super(Container, self).__init__(klass=self.klass, args=args, **kwargs) + super().__init__(klass=self.klass, args=args, **kwargs) def validate(self, obj, value): if isinstance(value, self._cast_types): - value = self.klass(value) - value = super(Container, self).validate(obj, value) + assert self.klass is not None + value = self.klass(value) # type:ignore[operator] + value = super().validate(obj, value) if value is None: return value @@ -2518,17 +2704,18 @@ class Container(Instance): self.error(obj, v, error) else: validated.append(v) - return self.klass(validated) + assert self.klass is not None + return self.klass(validated) # type:ignore[operator] def class_init(self, cls, name): if isinstance(self._trait, TraitType): self._trait.class_init(cls, None) - super(Container, self).class_init(cls, name) + super().class_init(cls, name) def instance_init(self, obj): if isinstance(self._trait, TraitType): self._trait.instance_init(obj) - super(Container, self).instance_init(obj) + super().instance_init(obj) def from_string(self, s): """Load value from a single string""" @@ -2545,6 +2732,7 @@ class Container(Instance): This is where we parse CLI configuration """ + assert self.klass is not None if len(s_list) == 1: # check for deprecated --Class.trait="['a', 'b', 'c']" r = s_list[0] @@ -2558,7 +2746,7 @@ class Container(Instance): clsname = self.this_class.__name__ + "." else: clsname = "" - + assert self.name is not None warn( "--{0}={1} for containers is deprecated in traitlets 5.0. " "You can pass `--{0} item` ... multiple times to add items to a list.".format( @@ -2566,16 +2754,17 @@ class Container(Instance): ), FutureWarning, ) - return self.klass(literal_eval(r)) + return self.klass(literal_eval(r)) # type:ignore[operator] sig = inspect.signature(self.item_from_string) if "index" in sig.parameters: item_from_string = self.item_from_string else: # backward-compat: allow item_from_string to ignore index arg - item_from_string = lambda s, index=None: self.item_from_string(s) + item_from_string = lambda s, index=None: self.item_from_string(s) # noqa[E371] + return self.klass( [item_from_string(s, index=idx) for idx, s in enumerate(s_list)] - ) + ) # type:ignore[operator] def item_from_string(self, s, index=None): """Cast a single item from a string @@ -2590,8 +2779,9 @@ class Container(Instance): class List(Container): """An instance of a Python list.""" + klass = list - _cast_types = (tuple,) + _cast_types: t.Any = (tuple,) def __init__( self, @@ -2629,12 +2819,13 @@ class List(Container): """ self._minlen = minlen self._maxlen = maxlen - super(List, self).__init__(trait=trait, default_value=default_value, - **kwargs) + super().__init__(trait=trait, default_value=default_value, **kwargs) def length_error(self, obj, value): - e = "The '%s' trait of %s instance must be of length %i <= L <= %i, but a value of %s was specified." \ + e = ( + "The '%s' trait of %s instance must be of length %i <= L <= %i, but a value of %s was specified." % (self.name, class_of(obj), self._minlen, self._maxlen, value) + ) raise TraitError(e) def validate_elements(self, obj, value): @@ -2642,7 +2833,7 @@ class List(Container): if length < self._minlen or length > self._maxlen: self.length_error(obj, value) - return super(List, self).validate_elements(obj, value) + return super().validate_elements(obj, value) def set(self, obj, value): if isinstance(value, str): @@ -2653,7 +2844,8 @@ class List(Container): class Set(List): """An instance of a Python set.""" - klass = set + + klass = set # type:ignore[assignment] _cast_types = (tuple, list) _literal_from_string_pairs = ("[]", "()", "{}") @@ -2693,14 +2885,14 @@ class Set(List): maxlen : Int [ default sys.maxsize ] The maximum length of the input list """ - super(Set, self).__init__(trait, default_value, minlen, maxlen, **kwargs) + super().__init__(trait, default_value, minlen, maxlen, **kwargs) def default_value_repr(self): # Ensure default value is sorted for a reproducible build list_repr = repr(sorted(self.make_dynamic_default())) - if list_repr == '[]': - return 'set()' - return '{'+list_repr[1:-1]+'}' + if list_repr == "[]": + return "set()" + return "{" + list_repr[1:-1] + "}" class Tuple(Container): @@ -2759,7 +2951,7 @@ class Tuple(Container): default_value = Undefined if default_value is Undefined: - args = () + args: t.Any = () elif default_value is None: # default_value back on kwargs for super() to handle args = () @@ -2767,9 +2959,7 @@ class Tuple(Container): elif isinstance(default_value, self._valid_defaults): args = (default_value,) else: - raise TypeError( - "default value of %s was %s" % (self.__class__.__name__, default_value) - ) + raise TypeError(f"default value of {self.__class__.__name__} was {default_value}") self._traits = [] for trait in traits: @@ -2804,14 +2994,16 @@ class Tuple(Container): # nothing to validate return value if len(value) != len(self._traits): - e = "The '%s' trait of %s instance requires %i elements, but a value of %s was specified." \ + e = ( + "The '%s' trait of %s instance requires %i elements, but a value of %s was specified." % (self.name, class_of(obj), len(self._traits), repr_type(value)) + ) raise TraitError(e) validated = [] - for t, v in zip(self._traits, value): + for trait, v in zip(self._traits, value): try: - v = t._validate(obj, v) + v = trait._validate(obj, v) except TraitError as error: self.error(obj, v, error) else: @@ -2845,11 +3037,18 @@ class Dict(Instance): .. versionchanged:: 5.0 Deprecated ambiguous ``trait``, ``traits`` args in favor of ``value_trait``, ``per_key_traits``. """ + _value_trait = None _key_trait = None - def __init__(self, value_trait=None, per_key_traits=None, key_trait=None, default_value=Undefined, - **kwargs): + def __init__( + self, + value_trait=None, + per_key_traits=None, + key_trait=None, + default_value=Undefined, + **kwargs, + ): """Create a dict trait type from a Python dict. The default value is created by doing ``dict(default_value)``, @@ -2874,23 +3073,26 @@ class Dict(Instance): Examples -------- - >>> d = Dict(Unicode()) a dict whose values must be text + >>> d = Dict(Unicode()) - >>> d2 = Dict(per_key_traits={"n": Integer(), "s": Unicode()}) d2['n'] must be an integer d2['s'] must be text + >>> d2 = Dict(per_key_traits={"n": Integer(), "s": Unicode()}) - >>> d3 = Dict(value_trait=Integer(), key_trait=Unicode()) d3's keys must be text d3's values must be integers + >>> d3 = Dict(value_trait=Integer(), key_trait=Unicode()) + """ # handle deprecated keywords - trait = kwargs.pop('trait', None) + trait = kwargs.pop("trait", None) if trait is not None: if value_trait is not None: - raise TypeError("Found a value for both `value_trait` and its deprecated alias `trait`.") + raise TypeError( + "Found a value for both `value_trait` and its deprecated alias `trait`." + ) value_trait = trait warn( "Keyword `trait` is deprecated in traitlets 5.0, use `value_trait` instead", @@ -2900,7 +3102,9 @@ class Dict(Instance): traits = kwargs.pop("traits", None) if traits is not None: if per_key_traits is not None: - raise TypeError("Found a value for both `per_key_traits` and its deprecated alias `traits`.") + raise TypeError( + "Found a value for both `per_key_traits` and its deprecated alias `traits`." + ) per_key_traits = traits warn( "Keyword `traits` is deprecated in traitlets 5.0, use `per_key_traits` instead", @@ -2923,30 +3127,38 @@ class Dict(Instance): if default_value is Undefined: default_value = {} if default_value is None: - args = None + args: t.Any = None elif isinstance(default_value, dict): args = (default_value,) elif isinstance(default_value, SequenceTypes): args = (default_value,) else: - raise TypeError('default value of Dict was %s' % default_value) + raise TypeError("default value of Dict was %s" % default_value) # Case where a type of TraitType is provided rather than an instance if is_trait(value_trait): if isinstance(value_trait, type): - warn("Traits should be given as instances, not types (for example, `Int()`, not `Int`)" - " Passing types is deprecated in traitlets 4.1.", - DeprecationWarning, stacklevel=2) + warn( + "Traits should be given as instances, not types (for example, `Int()`, not `Int`)" + " Passing types is deprecated in traitlets 4.1.", + DeprecationWarning, + stacklevel=2, + ) value_trait = value_trait() self._value_trait = value_trait elif value_trait is not None: - raise TypeError("`value_trait` must be a Trait or None, got %s" % repr_type(value_trait)) + raise TypeError( + "`value_trait` must be a Trait or None, got %s" % repr_type(value_trait) + ) if is_trait(key_trait): if isinstance(key_trait, type): - warn("Traits should be given as instances, not types (for example, `Int()`, not `Int`)" - " Passing types is deprecated in traitlets 4.1.", - DeprecationWarning, stacklevel=2) + warn( + "Traits should be given as instances, not types (for example, `Int()`, not `Int`)" + " Passing types is deprecated in traitlets 4.1.", + DeprecationWarning, + stacklevel=2, + ) key_trait = key_trait() self._key_trait = key_trait elif key_trait is not None: @@ -2954,15 +3166,18 @@ class Dict(Instance): self._per_key_traits = per_key_traits - super(Dict, self).__init__(klass=dict, args=args, **kwargs) + super().__init__(klass=dict, args=args, **kwargs) - def element_error(self, obj, element, validator, side='Values'): - e = side + " of the '%s' trait of %s instance must be %s, but a value of %s was specified." \ + def element_error(self, obj, element, validator, side="Values"): + e = ( + side + + " of the '%s' trait of %s instance must be %s, but a value of %s was specified." % (self.name, class_of(obj), validator.info(), repr_type(element)) + ) raise TraitError(e) def validate(self, obj, value): - value = super(Dict, self).validate(obj, value) + value = super().validate(obj, value) if value is None: return value value = self.validate_elements(obj, value) @@ -2981,17 +3196,17 @@ class Dict(Instance): if key_trait: try: key = key_trait._validate(obj, key) - except TraitError as error: - self.element_error(obj, key, key_trait, 'Keys') + except TraitError: + self.element_error(obj, key, key_trait, "Keys") active_value_trait = per_key_override.get(key, value_trait) if active_value_trait: try: v = active_value_trait._validate(obj, v) except TraitError: - self.element_error(obj, v, active_value_trait, 'Values') + self.element_error(obj, v, active_value_trait, "Values") validated[key] = v - return self.klass(validated) + return self.klass(validated) # type:ignore def class_init(self, cls, name): if isinstance(self._value_trait, TraitType): @@ -3001,7 +3216,7 @@ class Dict(Instance): if self._per_key_traits is not None: for trait in self._per_key_traits.values(): trait.class_init(cls, None) - super(Dict, self).class_init(cls, name) + super().class_init(cls, name) def instance_init(self, obj): if isinstance(self._value_trait, TraitType): @@ -3011,7 +3226,7 @@ class Dict(Instance): if self._per_key_traits is not None: for trait in self._per_key_traits.values(): trait.instance_init(obj) - super(Dict, self).instance_init(obj) + super().instance_init(obj) def from_string(self, s): """Load value from a single string""" @@ -3036,11 +3251,7 @@ class Dict(Instance): """ if len(s_list) == 1 and s_list[0] == "None" and self.allow_none: return None - if ( - len(s_list) == 1 - and s_list[0].startswith("{") - and s_list[0].endswith("}") - ): + if len(s_list) == 1 and s_list[0].startswith("{") and s_list[0].endswith("}"): warn( "--{0}={1} for dict-traits is deprecated in traitlets 5.0. " "You can pass --{0} <key=value> ... multiple times to add items to a dict.".format( @@ -3068,10 +3279,13 @@ class Dict(Instance): which will be merged in :meth:`.from_string_list`. """ - if '=' not in s: + if "=" not in s: raise TraitError( "'%s' options must have the form 'key=value', got %s" - % (self.__class__.__name__, repr(s),) + % ( + self.__class__.__name__, + repr(s), + ) ) key, value = s.split("=", 1) @@ -3092,8 +3306,8 @@ class TCPAddress(TraitType): This allows for both IPv4 IP addresses as well as hostnames. """ - default_value = ('127.0.0.1', 0) - info_text = 'an (ip, port) tuple' + default_value = ("127.0.0.1", 0) + info_text = "an (ip, port) tuple" def validate(self, obj, value): if isinstance(value, tuple): @@ -3105,11 +3319,11 @@ class TCPAddress(TraitType): self.error(obj, value) def from_string(self, s): - if self.allow_none and s == 'None': + if self.allow_none and s == "None": return None - if ':' not in s: - raise ValueError('Require `ip:port`, got %r' % s) - ip, port = s.split(':', 1) + if ":" not in s: + raise ValueError("Require `ip:port`, got %r" % s) + ip, port = s.split(":", 1) port = int(port) return (ip, port) @@ -3120,7 +3334,7 @@ class CRegExp(TraitType): Accepts both strings and compiled regular expressions. The resulting attribute will be a compiled regular expression.""" - info_text = 'a regular expression' + info_text = "a regular expression" def validate(self, obj, value): try: @@ -3155,16 +3369,16 @@ class UseEnum(TraitType): entity.color = 3 # USE: number (as int) assert entity.color is Color.green """ - default_value = None + + default_value: t.Optional[enum.Enum] = None info_text = "Trait type adapter to a Enum class" def __init__(self, enum_class, default_value=None, **kwargs): - assert issubclass(enum_class, enum.Enum), \ - "REQUIRE: enum.Enum, but was: %r" % enum_class + assert issubclass(enum_class, enum.Enum), "REQUIRE: enum.Enum, but was: %r" % enum_class allow_none = kwargs.get("allow_none", False) if default_value is None and not allow_none: default_value = list(enum_class.__members__.values())[0] - super(UseEnum, self).__init__(default_value=default_value, **kwargs) + super().__init__(default_value=default_value, **kwargs) self.enum_class = enum_class self.name_prefix = enum_class.__name__ + "." @@ -3207,19 +3421,17 @@ class UseEnum(TraitType): self.error(obj, value) def _choices_str(self, as_rst=False): - """ Returns a description of the trait choices (not none).""" + """Returns a description of the trait choices (not none).""" choices = self.enum_class.__members__.keys() if as_rst: - return '|'.join('``%r``' % x for x in choices) + return "|".join("``%r``" % x for x in choices) else: return repr(list(choices)) # Listify because py3.4- prints odict-class def _info(self, as_rst=False): - """ Returns a description of the trait.""" - none = (' or %s' % ('`None`' if as_rst else 'None') - if self.allow_none else - '') - return 'any of %s%s' % (self._choices_str(as_rst), none) + """Returns a description of the trait.""" + none = " or %s" % ("`None`" if as_rst else "None") if self.allow_none else "" + return f"any of {self._choices_str(as_rst)}{none}" def info(self): return self._info(as_rst=False) @@ -3228,7 +3440,6 @@ class UseEnum(TraitType): return self._info(as_rst=True) - class Callable(TraitType): """A trait which is callable. @@ -3237,7 +3448,7 @@ class Callable(TraitType): Classes are callable, as are instances with a __call__() method.""" - info_text = 'a callable' + info_text = "a callable" def validate(self, obj, value): if callable(value): @@ -3252,7 +3463,8 @@ def _add_all(): do in a function to avoid iterating through globals while defining local variables """ for _name, _value in globals().items(): - if not _name.startswith('_') and isinstance(_value, type) and issubclass(_value, TraitType): + if not _name.startswith("_") and isinstance(_value, type) and issubclass(_value, TraitType): __all__.append(_name) + _add_all() diff --git a/contrib/python/traitlets/py3/traitlets/utils/__init__.py b/contrib/python/traitlets/py3/traitlets/utils/__init__.py index 0fbba3d358..1bf11b2e1e 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/__init__.py +++ b/contrib/python/traitlets/py3/traitlets/utils/__init__.py @@ -1,9 +1,10 @@ import os + # vestigal things from IPython_genutils. -def cast_unicode(s, encoding='utf-8'): +def cast_unicode(s, encoding="utf-8"): if isinstance(s, bytes): - return s.decode(encoding, 'replace') + return s.decode(encoding, "replace") return s @@ -58,9 +59,7 @@ def filefind(filename, path_dirs=None): if os.path.isfile(testname): return os.path.abspath(testname) - raise IOError( - "File %r does not exist in any of the search paths: %r" % (filename, path_dirs) - ) + raise OSError(f"File {filename!r} does not exist in any of the search paths: {path_dirs!r}") def expand_path(s): diff --git a/contrib/python/traitlets/py3/traitlets/utils/bunch.py b/contrib/python/traitlets/py3/traitlets/utils/bunch.py index 2edb830ad6..7982bbb93c 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/bunch.py +++ b/contrib/python/traitlets/py3/traitlets/utils/bunch.py @@ -6,20 +6,21 @@ attribute-access of items on a dict. # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -class Bunch(dict): + +class Bunch(dict): # type:ignore[type-arg] """A dict with attribute-access""" + def __getattr__(self, key): try: return self.__getitem__(key) except KeyError: raise AttributeError(key) - + def __setattr__(self, key, value): self.__setitem__(key, value) - + def __dir__(self): # py2-compat: can't use super because dict doesn't have __dir__ names = dir({}) names.extend(self.keys()) return names - diff --git a/contrib/python/traitlets/py3/traitlets/utils/decorators.py b/contrib/python/traitlets/py3/traitlets/utils/decorators.py index 656f968c8d..60b9ab24b3 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/decorators.py +++ b/contrib/python/traitlets/py3/traitlets/utils/decorators.py @@ -1,8 +1,7 @@ """Useful decorators for Traitlets users.""" import copy - -from inspect import Signature, Parameter, signature +from inspect import Parameter, Signature, signature from ..traitlets import Undefined @@ -17,7 +16,7 @@ def signature_has_traits(cls): traits = [ (name, _get_default(value.default_value)) for name, value in cls.class_traits().items() - if not name.startswith('_') + if not name.startswith("_") ] # Taking the __init__ signature, as the cls signature is not initialized yet @@ -33,7 +32,10 @@ def signature_has_traits(cls): # Copy the parameter parameter = copy.copy(old_signature.parameters[parameter_name]) - if parameter.kind is Parameter.POSITIONAL_ONLY or parameter.kind is Parameter.POSITIONAL_OR_KEYWORD: + if ( + parameter.kind is Parameter.POSITIONAL_ONLY + or parameter.kind is Parameter.POSITIONAL_OR_KEYWORD + ): old_positional_parameters.append(parameter) elif parameter.kind is Parameter.VAR_POSITIONAL: @@ -49,8 +51,9 @@ def signature_has_traits(cls): # because it can't accept traits as keyword arguments if old_var_keyword_parameter is None: raise RuntimeError( - 'The {} constructor does not take **kwargs, which means that the signature can not be expanded with trait names' - .format(cls) + "The {} constructor does not take **kwargs, which means that the signature can not be expanded with trait names".format( + cls + ) ) new_parameters = [] diff --git a/contrib/python/traitlets/py3/traitlets/utils/descriptions.py b/contrib/python/traitlets/py3/traitlets/utils/descriptions.py index 7b2996491f..232eb0e728 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/descriptions.py +++ b/contrib/python/traitlets/py3/traitlets/utils/descriptions.py @@ -46,11 +46,11 @@ def describe(article, value, name=None, verbose=False, capital=False): Definite description: >>> describe("the", object()) - "the object at '0x10741f1b0'" + "the object at '...'" >>> describe("the", object) - "the type 'object'" + 'the object object' >>> describe("the", type(object)) - "the type 'type'" + 'the type type' Definitely named description: @@ -71,7 +71,7 @@ def describe(article, value, name=None, verbose=False, capital=False): if article == "the" or (article is None and not inspect.isclass(value)): if name is not None: - result = "{} {}".format(typename, name) + result = f"{typename} {name}" if article is not None: return add_article(result, True, capital) else: @@ -86,7 +86,10 @@ def describe(article, value, name=None, verbose=False, capital=False): elif isinstance(value, types.MethodType): name = value.__func__.__name__ tick_wrap = True - elif type(value).__repr__ in (object.__repr__, type.__repr__): + elif type(value).__repr__ in ( + object.__repr__, + type.__repr__, + ): # type:ignore[comparison-overlap] name = "at '%s'" % hex(id(value)) verbose = False else: @@ -96,24 +99,24 @@ def describe(article, value, name=None, verbose=False, capital=False): name = _prefix(value) + name if tick_wrap: name = name.join("''") - return describe(article, value, name=name, - verbose=verbose, capital=capital) + return describe(article, value, name=name, verbose=verbose, capital=capital) elif article in ("a", "an") or article is None: if article is None: return typename return add_article(typename, False, capital) else: - raise ValueError("The 'article' argument should " - "be 'the', 'a', 'an', or None not %r" % article) + raise ValueError( + "The 'article' argument should be 'the', 'a', 'an', or None not %r" % article + ) + - def _prefix(value): if isinstance(value, types.MethodType): - name = describe(None, value.__self__, verbose=True) + '.' + name = describe(None, value.__self__, verbose=True) + "." else: module = inspect.getmodule(value) if module is not None and module.__name__ != "builtins": - name = module.__name__ + '.' + name = module.__name__ + "." else: name = "" return name @@ -150,11 +153,11 @@ def add_article(name, definite=False, capital=False): if definite: result = "the " + name else: - first_letters = re.compile(r'[\W_]+').sub('', name) - if first_letters[:1].lower() in 'aeiou': - result = 'an ' + name + first_letters = re.compile(r"[\W_]+").sub("", name) + if first_letters[:1].lower() in "aeiou": + result = "an " + name else: - result = 'a ' + name + result = "a " + name if capital: return result[0].upper() + result[1:] else: @@ -167,5 +170,5 @@ def repr_type(obj): error messages. """ the_type = type(obj) - msg = '{!r} {!r}'.format(obj, the_type) + msg = f"{obj!r} {the_type!r}" return msg diff --git a/contrib/python/traitlets/py3/traitlets/utils/getargspec.py b/contrib/python/traitlets/py3/traitlets/utils/getargspec.py index 22511437bd..e2b1f235c8 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/getargspec.py +++ b/contrib/python/traitlets/py3/traitlets/utils/getargspec.py @@ -1,6 +1,6 @@ """ getargspec excerpted from: - + sphinx.util.inspect ~~~~~~~~~~~~~~~~~~~ Helpers for inspecting Python modules. @@ -9,10 +9,10 @@ """ import inspect +from functools import partial # Unmodified from sphinx below this line -from functools import partial def getargspec(func): """Like inspect.getargspec but supports functools.partial as well.""" @@ -26,7 +26,7 @@ def getargspec(func): kwoargs = list(argspec[4]) kwodefs = dict(argspec[5] or {}) if func.args: - args = args[len(func.args):] + args = args[len(func.args) :] for arg in func.keywords or (): try: i = args.index(arg) - len(args) @@ -35,16 +35,15 @@ def getargspec(func): del defaults[i] except IndexError: pass - except ValueError: # must be a kwonly arg + except ValueError: # must be a kwonly arg i = kwoargs.index(arg) del kwoargs[i] del kwodefs[arg] - return inspect.FullArgSpec(args, argspec[1], argspec[2], - tuple(defaults), kwoargs, - kwodefs, argspec[6]) - while hasattr(func, '__wrapped__'): + return inspect.FullArgSpec( + args, argspec[1], argspec[2], tuple(defaults), kwoargs, kwodefs, argspec[6] + ) + while hasattr(func, "__wrapped__"): func = func.__wrapped__ if not inspect.isfunction(func): - raise TypeError('%r is not a Python function' % func) + raise TypeError("%r is not a Python function" % func) return inspect.getfullargspec(func) - diff --git a/contrib/python/traitlets/py3/traitlets/utils/importstring.py b/contrib/python/traitlets/py3/traitlets/utils/importstring.py index defad8f183..7258e20bbe 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/importstring.py +++ b/contrib/python/traitlets/py3/traitlets/utils/importstring.py @@ -23,7 +23,7 @@ def import_item(name): """ if not isinstance(name, str): raise TypeError("import_item accepts strings, not '%s'." % type(name)) - parts = name.rsplit('.', 1) + parts = name.rsplit(".", 1) if len(parts) == 2: # called with 'foo.bar....' package, obj = parts @@ -31,7 +31,7 @@ def import_item(name): try: pak = getattr(module, obj) except AttributeError: - raise ImportError('No module named %s' % obj) + raise ImportError("No module named %s" % obj) return pak else: # called with un-dotted string diff --git a/contrib/python/traitlets/py3/traitlets/utils/nested_update.py b/contrib/python/traitlets/py3/traitlets/utils/nested_update.py new file mode 100644 index 0000000000..7f09e171a3 --- /dev/null +++ b/contrib/python/traitlets/py3/traitlets/utils/nested_update.py @@ -0,0 +1,38 @@ +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + + +def nested_update(this, that): + """Merge two nested dictionaries. + + Effectively a recursive ``dict.update``. + + Examples + -------- + Merge two flat dictionaries: + >>> nested_update( + ... {'a': 1, 'b': 2}, + ... {'b': 3, 'c': 4} + ... ) + {'a': 1, 'b': 3, 'c': 4} + + Merge two nested dictionaries: + >>> nested_update( + ... {'x': {'a': 1, 'b': 2}, 'y': 5, 'z': 6}, + ... {'x': {'b': 3, 'c': 4}, 'z': 7, '0': 8}, + ... ) + {'x': {'a': 1, 'b': 3, 'c': 4}, 'y': 5, 'z': 7, '0': 8} + + """ + for key, value in this.items(): + if isinstance(value, dict): + if key in that and isinstance(that[key], dict): + nested_update(this[key], that[key]) + elif key in that: + this[key] = that[key] + + for key, value in that.items(): + if key not in this: + this[key] = value + + return this diff --git a/contrib/python/traitlets/py3/traitlets/utils/sentinel.py b/contrib/python/traitlets/py3/traitlets/utils/sentinel.py index 0760bec8b5..75e000f81b 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/sentinel.py +++ b/contrib/python/traitlets/py3/traitlets/utils/sentinel.py @@ -4,8 +4,7 @@ # Distributed under the terms of the Modified BSD License. -class Sentinel(object): - +class Sentinel: def __init__(self, name, module, docstring=None): self.name = name self.module = module @@ -13,7 +12,7 @@ class Sentinel(object): self.__doc__ = docstring def __repr__(self): - return str(self.module) + '.' + self.name + return str(self.module) + "." + self.name def __copy__(self): return self diff --git a/contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py b/contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py index 3f71fab957..223124d7d5 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py +++ b/contrib/python/traitlets/py3/traitlets/utils/tests/test_bunch.py @@ -1,14 +1,16 @@ from traitlets.utils.bunch import Bunch + def test_bunch(): b = Bunch(x=5, y=10) - assert 'y' in b - assert 'x' in b + assert "y" in b + assert "x" in b assert b.x == 5 - b['a'] = 'hi' - assert b.a == 'hi' + b["a"] = "hi" + assert b.a == "hi" + def test_bunch_dir(): b = Bunch(x=5, y=10) - assert 'x' in dir(b) - assert 'keys' in dir(b) + assert "x" in dir(b) + assert "keys" in dir(b) diff --git a/contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py b/contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py index aafd372f3c..5410c20137 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py +++ b/contrib/python/traitlets/py3/traitlets/utils/tests/test_decorators.py @@ -1,69 +1,66 @@ +from inspect import Parameter, signature from unittest import TestCase -from inspect import Signature, Parameter, signature - from traitlets.traitlets import HasTraits, Int, Unicode - from traitlets.utils.decorators import signature_has_traits class TestExpandSignature(TestCase): - def test_no_init(self): @signature_has_traits class Foo(HasTraits): number1 = Int() number2 = Int() - value = Unicode('Hello') + value = Unicode("Hello") parameters = signature(Foo).parameters parameter_names = list(parameters) - self.assertIs(parameters['args'].kind, Parameter.VAR_POSITIONAL) - self.assertEqual('args', parameter_names[0]) + self.assertIs(parameters["args"].kind, Parameter.VAR_POSITIONAL) + self.assertEqual("args", parameter_names[0]) - self.assertIs(parameters['number1'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['number2'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['value'].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number1"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number2"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["value"].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['kwargs'].kind, Parameter.VAR_KEYWORD) - self.assertEqual('kwargs', parameter_names[-1]) + self.assertIs(parameters["kwargs"].kind, Parameter.VAR_KEYWORD) + self.assertEqual("kwargs", parameter_names[-1]) - f = Foo(number1=32, value='World') + f = Foo(number1=32, value="World") self.assertEqual(f.number1, 32) self.assertEqual(f.number2, 0) - self.assertEqual(f.value, 'World') + self.assertEqual(f.value, "World") def test_partial_init(self): @signature_has_traits class Foo(HasTraits): number1 = Int() number2 = Int() - value = Unicode('Hello') + value = Unicode("Hello") def __init__(self, arg1, **kwargs): self.arg1 = arg1 - super(Foo, self).__init__(**kwargs) + super().__init__(**kwargs) parameters = signature(Foo).parameters parameter_names = list(parameters) - self.assertIs(parameters['arg1'].kind, Parameter.POSITIONAL_OR_KEYWORD) - self.assertEqual('arg1', parameter_names[0]) + self.assertIs(parameters["arg1"].kind, Parameter.POSITIONAL_OR_KEYWORD) + self.assertEqual("arg1", parameter_names[0]) - self.assertIs(parameters['number1'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['number2'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['value'].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number1"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number2"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["value"].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['kwargs'].kind, Parameter.VAR_KEYWORD) - self.assertEqual('kwargs', parameter_names[-1]) + self.assertIs(parameters["kwargs"].kind, Parameter.VAR_KEYWORD) + self.assertEqual("kwargs", parameter_names[-1]) - f = Foo(1, number1=32, value='World') + f = Foo(1, number1=32, value="World") self.assertEqual(f.arg1, 1) self.assertEqual(f.number1, 32) self.assertEqual(f.number2, 0) - self.assertEqual(f.value, 'World') + self.assertEqual(f.value, "World") def test_duplicate_init(self): @signature_has_traits @@ -74,12 +71,12 @@ class TestExpandSignature(TestCase): def __init__(self, number1, **kwargs): self.test = number1 - super(Foo, self).__init__(number1=number1, **kwargs) + super().__init__(number1=number1, **kwargs) parameters = signature(Foo).parameters parameter_names = list(parameters) - self.assertListEqual(parameter_names, ['number1', 'number2', 'kwargs']) + self.assertListEqual(parameter_names, ["number1", "number2", "kwargs"]) f = Foo(number1=32, number2=36) self.assertEqual(f.test, 32) @@ -91,7 +88,7 @@ class TestExpandSignature(TestCase): class Foo(HasTraits): number1 = Int() number2 = Int() - value = Unicode('Hello') + value = Unicode("Hello") def __init__(self, arg1, arg2=None, *pos_args, **kw_args): self.arg1 = arg1 @@ -99,37 +96,38 @@ class TestExpandSignature(TestCase): self.pos_args = pos_args self.kw_args = kw_args - super(Foo, self).__init__(*pos_args, **kw_args) + super().__init__(*pos_args, **kw_args) parameters = signature(Foo).parameters parameter_names = list(parameters) - self.assertIs(parameters['arg1'].kind, Parameter.POSITIONAL_OR_KEYWORD) - self.assertEqual('arg1', parameter_names[0]) + self.assertIs(parameters["arg1"].kind, Parameter.POSITIONAL_OR_KEYWORD) + self.assertEqual("arg1", parameter_names[0]) - self.assertIs(parameters['arg2'].kind, Parameter.POSITIONAL_OR_KEYWORD) - self.assertEqual('arg2', parameter_names[1]) + self.assertIs(parameters["arg2"].kind, Parameter.POSITIONAL_OR_KEYWORD) + self.assertEqual("arg2", parameter_names[1]) - self.assertIs(parameters['pos_args'].kind, Parameter.VAR_POSITIONAL) - self.assertEqual('pos_args', parameter_names[2]) + self.assertIs(parameters["pos_args"].kind, Parameter.VAR_POSITIONAL) + self.assertEqual("pos_args", parameter_names[2]) - self.assertIs(parameters['number1'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['number2'].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['value'].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number1"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["number2"].kind, Parameter.KEYWORD_ONLY) + self.assertIs(parameters["value"].kind, Parameter.KEYWORD_ONLY) - self.assertIs(parameters['kw_args'].kind, Parameter.VAR_KEYWORD) - self.assertEqual('kw_args', parameter_names[-1]) + self.assertIs(parameters["kw_args"].kind, Parameter.VAR_KEYWORD) + self.assertEqual("kw_args", parameter_names[-1]) - f = Foo(1, 3, 45, 'hey', number1=32, value='World') + f = Foo(1, 3, 45, "hey", number1=32, value="World") self.assertEqual(f.arg1, 1) self.assertEqual(f.arg2, 3) - self.assertTupleEqual(f.pos_args, (45, 'hey')) + self.assertTupleEqual(f.pos_args, (45, "hey")) self.assertEqual(f.number1, 32) self.assertEqual(f.number2, 0) - self.assertEqual(f.value, 'World') + self.assertEqual(f.value, "World") def test_no_kwargs(self): with self.assertRaises(RuntimeError): + @signature_has_traits class Foo(HasTraits): number1 = Int() diff --git a/contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py b/contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py index fb3266942f..a3a74c3214 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py +++ b/contrib/python/traitlets/py3/traitlets/utils/tests/test_importstring.py @@ -12,18 +12,15 @@ from traitlets.utils.importstring import import_item class TestImportItem(TestCase): - def test_import_unicode(self): - self.assertIs(os, import_item('os')) - self.assertIs(os.path, import_item('os.path')) - self.assertIs(os.path.join, import_item('os.path.join')) + self.assertIs(os, import_item("os")) + self.assertIs(os.path, import_item("os.path")) + self.assertIs(os.path.join, import_item("os.path.join")) def test_bad_input(self): - class NotAString(object): + class NotAString: pass - msg = ( - "import_item accepts strings, " - "not '%s'." % NotAString - ) + + msg = "import_item accepts strings, not '%s'." % NotAString with self.assertRaisesRegex(TypeError, msg): import_item(NotAString()) diff --git a/contrib/python/traitlets/py3/traitlets/utils/text.py b/contrib/python/traitlets/py3/traitlets/utils/text.py index 92464a5a6c..c7d49edece 100644 --- a/contrib/python/traitlets/py3/traitlets/utils/text.py +++ b/contrib/python/traitlets/py3/traitlets/utils/text.py @@ -3,8 +3,10 @@ Utilities imported from ipython_genutils """ import re -from textwrap import dedent, indent as _indent import textwrap +from textwrap import dedent +from textwrap import indent as _indent +from typing import List def indent(val): @@ -12,7 +14,7 @@ def indent(val): return res -def wrap_paragraphs(text: str, ncols=80): +def wrap_paragraphs(text: str, ncols: int = 80) -> List[str]: """Wrap multiple paragraphs to fit a specified width. This is equivalent to textwrap.wrap, but with support for multiple |