From 7fe6ec9461c47348d54dac6bc1ef7c6da2f32452 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Feb 2023 10:54:27 -0600 Subject: [PATCH] [pre-commit.ci] pre-commit autoupdate (#830)Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/python-jsonschema/check-jsonschema: 0.19.2 → 0.21.0](https://github.com/python-jsonschema/check-jsonschema/compare/0.19.2...0.21.0) - [github.com/psf/black: 22.12.0 → 23.1.0](https://github.com/psf/black/compare/22.12.0...23.1.0) - [github.com/charliermarsh/ruff-pre-commit: v0.0.207 → v0.0.243](https://github.com/charliermarsh/ruff-pre-commit/compare/v0.0.207...v0.0.243) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * sync deps and lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester --- .pre-commit-config.yaml | 6 +- examples/myapp.py | 2 - pyproject.toml | 4 +- traitlets/config/application.py | 4 +- traitlets/config/configurable.py | 5 +- traitlets/config/loader.py | 6 +- traitlets/config/tests/test_application.py | 4 -- traitlets/config/tests/test_loader.py | 1 - traitlets/tests/test_traitlets.py | 66 +--------------------- traitlets/traitlets.py | 20 ++++--- 10 files changed, 26 insertions(+), 92 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 275a49b6..9f160f06 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: - id: trailing-whitespace - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.19.2 + rev: 0.21.0 hooks: - id: check-github-workflows @@ -29,12 +29,12 @@ repos: - id: mdformat - repo: https://github.com/psf/black - rev: 22.12.0 + rev: 23.1.0 hooks: - id: black - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.0.207 + rev: v0.0.243 hooks: - id: ruff args: ["--fix"] diff --git a/examples/myapp.py b/examples/myapp.py index e54c2bf9..bd04be7d 100755 --- a/examples/myapp.py +++ b/examples/myapp.py @@ -46,12 +46,10 @@ class Foo(Configurable): class Bar(Configurable): - enabled = Bool(True, help="Enable bar.").tag(config=True) class MyApp(Application): - name = Unicode("myapp") running = Bool(False, help="Is the app running?").tag(config=True) classes = List([Bar, Foo]) # type:ignore[assignment] diff --git a/pyproject.toml b/pyproject.toml index 15ee8a93..3aa321ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,7 +57,7 @@ dependencies = ["mypy>=0.990"] test = "mypy --install-types --non-interactive {args:.}" [tool.hatch.envs.lint] -dependencies = ["black==22.12.0", "mdformat>0.7", "ruff==0.0.206"] +dependencies = ["black==23.1.0", "mdformat>0.7", "ruff==0.0.243"] detached = true [tool.hatch.envs.lint.scripts] style = [ @@ -182,6 +182,8 @@ ignore = [ "C408", # N801 Class name `directional_link` should use CapWords convention "N801", + # S110 `try`-`except`-`pass` detected + "S110", ] unfixable = [ # Don't touch print statements diff --git a/traitlets/config/application.py b/traitlets/config/application.py index 3cffa6b0..156016a7 100644 --- a/traitlets/config/application.py +++ b/traitlets/config/application.py @@ -446,7 +446,7 @@ def __init__(self, **kwargs): if cls not in self.classes: if self.classes is cls.classes: # class attr, assign instead of insert - self.classes = [cls] + self.classes + self.classes = [cls, *self.classes] else: self.classes.insert(0, self.__class__) @@ -947,7 +947,7 @@ def load_config_file(self, filename, path=None): """Load config files by filename and path.""" filename, ext = os.path.splitext(filename) new_config = Config() - for (config, fname) in self._load_config_files( + for config, fname in self._load_config_files( filename, path=path, log=self.log, diff --git a/traitlets/config/configurable.py b/traitlets/config/configurable.py index effa8e42..e85638eb 100644 --- a/traitlets/config/configurable.py +++ b/traitlets/config/configurable.py @@ -43,7 +43,6 @@ class MultipleInstanceError(ConfigurableError): class Configurable(HasTraits): - config = Instance(Config, (), {}) parent = Instance("traitlets.config.configurable.Configurable", allow_none=True) @@ -558,8 +557,8 @@ def instance(cls, *args, **kwargs): return cls._instance else: raise MultipleInstanceError( - "An incompatible sibling of '%s' is already instantiated" - " as singleton: %s" % (cls.__name__, type(cls._instance).__name__) + "An incompatible sibling of '{}' is already instantiated" + " as singleton: {}".format(cls.__name__, type(cls._instance).__name__) ) @classmethod diff --git a/traitlets/config/loader.py b/traitlets/config/loader.py index c1834a9b..4d3eac51 100644 --- a/traitlets/config/loader.py +++ b/traitlets/config/loader.py @@ -349,7 +349,7 @@ def __setitem__(self, key, value): if not isinstance(value, Config): raise ValueError( "values whose keys begin with an uppercase " - "char must be Config instances: %r, %r" % (key, value) + "char must be Config instances: {!r}, {!r}".format(key, value) ) dict.__setitem__(self, key, value) @@ -1037,8 +1037,8 @@ def _add_arguments(self, aliases, flags, classes): # 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) + "The alias `{}` for the 'append' sequence " + "config-trait `{}` cannot be also a flag!'".format(key, traitname) ) # For argcomplete, check if any either an argcompleter metadata tag or method # is available. If so, it should be a callable which takes the command-line key diff --git a/traitlets/config/tests/test_application.py b/traitlets/config/tests/test_application.py index 40da8e46..7c3644d5 100644 --- a/traitlets/config/tests/test_application.py +++ b/traitlets/config/tests/test_application.py @@ -34,7 +34,6 @@ class Foo(Configurable): - i = Integer( 0, help=""" @@ -51,7 +50,6 @@ class Foo(Configurable): 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="*") @@ -62,7 +60,6 @@ class Bar(Configurable): class MyApp(Application): - name = Unicode("myapp") running = Bool(False, help="Is the app running?").tag(config=True) classes = List([Bar, Foo]) # type:ignore @@ -475,7 +472,6 @@ def test_flatten_aliases(self): self.assertEqual(app.config.MyApp.log_level, "CRITICAL") def test_extra_args(self): - app = MyApp() app.parse_command_line(["--Bar.b=5", "extra", "args", "--disable"]) app.init_bar() diff --git a/traitlets/config/tests/test_loader.py b/traitlets/config/tests/test_loader.py index 7355544c..3a1f9612 100644 --- a/traitlets/config/tests/test_loader.py +++ b/traitlets/config/tests/test_loader.py @@ -96,7 +96,6 @@ def test_json(self): self._check_conf(config) def test_context_manager(self): - fd, fname = mkstemp(".json", prefix="μnïcø∂e") f = os.fdopen(fd, "w") f.write("{}") diff --git a/traitlets/tests/test_traitlets.py b/traitlets/tests/test_traitlets.py index b69fad44..3b41fe4b 100644 --- a/traitlets/tests/test_traitlets.py +++ b/traitlets/tests/test_traitlets.py @@ -193,7 +193,6 @@ def _x_default(self): self.assertEqual(a._trait_values, {"x": 11}) def test_deprecated_method_warnings(self): - with expected_warnings([]): class ShouldntWarn(HasTraits): @@ -415,7 +414,6 @@ def instance_init(self, inst): foo = inst.foo # instance should have the attr class HasFooDescriptors(HasDescriptors): - fd = FooDescriptor() def setup_instance(self, *args, **kwargs): @@ -597,7 +595,6 @@ def listener1(self, name, old, new): self.b += 1 class B(A): - c = 0 d = 0 @@ -788,7 +785,6 @@ def listener1(self, change): self.b += 1 class B(A): - c = 0 d = 0 @@ -1291,12 +1287,10 @@ def tearDown(self): class AnyTrait(HasTraits): - value = Any() class AnyTraitTest(TraitTestBase): - obj = AnyTrait() _default_value = None @@ -1305,48 +1299,40 @@ class AnyTraitTest(TraitTestBase): 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, ""] class OrTrait(HasTraits): - value = Bool() | Unicode() class OrTraitTest(TraitTestBase): - obj = OrTrait() _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] @@ -1397,12 +1383,10 @@ class TestMinBoundCInt(TestCInt): class LongTrait(HasTraits): - value = Long(99) class TestLong(TraitTestBase): - obj = LongTrait() _default_value = 99 @@ -1513,12 +1497,10 @@ class TestMaxBoundInteger(TraitTestBase): class FloatTrait(HasTraits): - value = Float(99.0, max=200.0) class TestFloat(TraitTestBase): - obj = FloatTrait() _default_value = 99.0 @@ -1541,12 +1523,10 @@ class TestFloat(TraitTestBase): class CFloatTrait(HasTraits): - value = CFloat("99.0", max=200.0) class TestCFloat(TraitTestBase): - obj = CFloatTrait() _default_value = 99.0 @@ -1558,12 +1538,10 @@ def coerce(self, v): class ComplexTrait(HasTraits): - value = Complex(99.0 - 99.0j) class TestComplex(TraitTestBase): - obj = ComplexTrait() _default_value = 99.0 - 99.0j @@ -1583,12 +1561,10 @@ class TestComplex(TraitTestBase): class BytesTrait(HasTraits): - value = Bytes(b"string") class TestBytes(TraitTestBase): - obj = BytesTrait() _default_value = b"string" @@ -1597,12 +1573,10 @@ class TestBytes(TraitTestBase): class UnicodeTrait(HasTraits): - value = Unicode("unicode") class TestUnicode(TraitTestBase): - obj = UnicodeTrait() _default_value = "unicode" @@ -1659,7 +1633,6 @@ class TCPAddressTrait(HasTraits): class TestTCPAddress(TraitTestBase): - obj = TCPAddressTrait() _default_value = ("127.0.0.1", 0) @@ -1668,12 +1641,10 @@ class TestTCPAddress(TraitTestBase): class ListTrait(HasTraits): - value = List(Int()) class TestList(TraitTestBase): - obj = ListTrait() _default_value: t.List[t.Any] = [] @@ -1691,12 +1662,10 @@ class Foo: class NoneInstanceListTrait(HasTraits): - value = List(Instance(Foo)) class TestNoneInstanceList(TraitTestBase): - obj = NoneInstanceListTrait() _default_value: t.List[t.Any] = [] @@ -1705,12 +1674,10 @@ class TestNoneInstanceList(TraitTestBase): class InstanceListTrait(HasTraits): - value = List(Instance(__name__ + ".Foo")) class TestInstanceList(TraitTestBase): - obj = InstanceListTrait() def test_klass(self): @@ -1731,12 +1698,10 @@ def test_klass(self): class UnionListTrait(HasTraits): - value = List(Int() | Bool()) class TestUnionListTrait(TraitTestBase): - obj = UnionListTrait() _default_value: t.List[t.Any] = [] @@ -1745,12 +1710,10 @@ class TestUnionListTrait(TraitTestBase): class LenListTrait(HasTraits): - value = List(Int(), [0], minlen=1, maxlen=2) class TestLenList(TraitTestBase): - obj = LenListTrait() _default_value = [0] @@ -1764,12 +1727,10 @@ def coerce(self, value): class TupleTrait(HasTraits): - value = Tuple(Int(allow_none=True), default_value=(1,)) class TestTupleTrait(TraitTestBase): - obj = TupleTrait() _default_value = (1,) @@ -1788,12 +1749,10 @@ def test_invalid_args(self): class LooseTupleTrait(HasTraits): - value = Tuple((1, 2, 3)) class TestLooseTupleTrait(TraitTestBase): - obj = LooseTupleTrait() _default_value = (1, 2, 3) @@ -1812,12 +1771,10 @@ def test_invalid_args(self): class MultiTupleTrait(HasTraits): - value = Tuple(Int(), Bytes(), default_value=[99, b"bottles"]) class TestMultiTuple(TraitTestBase): - obj = MultiTupleTrait() _default_value = (99, b"bottles") @@ -1884,7 +1841,6 @@ class C(HasTraits): class CRegExpTrait(HasTraits): - value = CRegExp(r"") @@ -1913,12 +1869,10 @@ def test_dict_assignment(): class UniformlyValueValidatedDictTrait(HasTraits): - value = Dict(value_trait=Unicode(), default_value={"foo": "1"}) class TestInstanceUniformlyValueValidatedDict(TraitTestBase): - obj = UniformlyValueValidatedDictTrait() _default_value = {"foo": "1"} @@ -1927,12 +1881,10 @@ class TestInstanceUniformlyValueValidatedDict(TraitTestBase): class NonuniformlyValueValidatedDictTrait(HasTraits): - value = Dict(per_key_traits={"foo": Int()}, default_value={"foo": 1}) class TestInstanceNonuniformlyValueValidatedDict(TraitTestBase): - obj = NonuniformlyValueValidatedDictTrait() _default_value = {"foo": 1} @@ -1941,12 +1893,10 @@ class TestInstanceNonuniformlyValueValidatedDict(TraitTestBase): class KeyValidatedDictTrait(HasTraits): - value = Dict(key_trait=Unicode(), default_value={"foo": "1"}) class TestInstanceKeyValidatedDict(TraitTestBase): - obj = KeyValidatedDictTrait() _default_value = {"foo": "1"} @@ -1955,7 +1905,6 @@ class TestInstanceKeyValidatedDict(TraitTestBase): class FullyValidatedDictTrait(HasTraits): - value = Dict( value_trait=Unicode(), key_trait=Unicode(), @@ -1965,7 +1914,6 @@ class FullyValidatedDictTrait(HasTraits): class TestInstanceFullyValidatedDict(TraitTestBase): - obj = FullyValidatedDictTrait() _default_value = {"foo": 1} @@ -1992,7 +1940,6 @@ 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") @@ -2018,7 +1965,6 @@ def test_multiple_validate(self): """Verify that we can register the same validator to multiple names""" class OddEven(HasTraits): - odd = Int(1) even = Int(0) @@ -2319,7 +2265,6 @@ class A(HasTraits): class Pickleable(HasTraits): - i = Int() @observe("i") @@ -2478,22 +2423,18 @@ def test_notification_order(): # Traits for Forward Declaration Tests ### class ForwardDeclaredInstanceTrait(HasTraits): - value = ForwardDeclaredInstance("ForwardDeclaredBar", allow_none=True) class ForwardDeclaredTypeTrait(HasTraits): - value = ForwardDeclaredType("ForwardDeclaredBar", allow_none=True) class ForwardDeclaredInstanceListTrait(HasTraits): - value = List(ForwardDeclaredInstance("ForwardDeclaredBar")) class ForwardDeclaredTypeListTrait(HasTraits): - value = List(ForwardDeclaredType("ForwardDeclaredBar")) @@ -2501,6 +2442,7 @@ class ForwardDeclaredTypeListTrait(HasTraits): # End Traits for Forward Declaration Tests ### + ### # Classes for Forward Declaration Tests ### @@ -2516,11 +2458,11 @@ class ForwardDeclaredBarSub(ForwardDeclaredBar): # End Classes for Forward Declaration Tests ### + ### # Forward Declaration Tests ### class TestForwardDeclaredInstanceTrait(TraitTestBase): - obj = ForwardDeclaredInstanceTrait() _default_value = None _good_values = [None, ForwardDeclaredBar(), ForwardDeclaredBarSub()] @@ -2528,7 +2470,6 @@ class TestForwardDeclaredInstanceTrait(TraitTestBase): class TestForwardDeclaredTypeTrait(TraitTestBase): - obj = ForwardDeclaredTypeTrait() _default_value = None _good_values = [None, ForwardDeclaredBar, ForwardDeclaredBarSub] @@ -2536,7 +2477,6 @@ class TestForwardDeclaredTypeTrait(TraitTestBase): class TestForwardDeclaredInstanceList(TraitTestBase): - obj = ForwardDeclaredInstanceListTrait() def test_klass(self): @@ -2560,7 +2500,6 @@ def test_klass(self): class TestForwardDeclaredTypeList(TraitTestBase): - obj = ForwardDeclaredTypeListTrait() def test_klass(self): @@ -2673,7 +2612,6 @@ class C(HasTraits): class TransitionalClass(HasTraits): - d = Any() @default("d") diff --git a/traitlets/traitlets.py b/traitlets/traitlets.py index 7cf69168..1783e263 100644 --- a/traitlets/traitlets.py +++ b/traitlets/traitlets.py @@ -573,12 +573,12 @@ def __init__( assert f is not None mod = f.f_globals.get("__name__") or "" pkg = mod.split(".", 1)[0] - key = tuple(["metadata-tag", pkg] + sorted(kwargs)) + key = ("metadata-tag", pkg, *sorted(kwargs)) if _should_warn(key): warn( - "metadata %s was set from the constructor. " + "metadata {} 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,), + "e.g., Int().tag(key1='value1', key2='value2')".format(kwargs), DeprecationWarning, stacklevel=stacklevel, ) @@ -758,7 +758,7 @@ def _cross_validate(self, obj, value): def __or__(self, other): if isinstance(other, Union): - return Union([self] + other.trait_types) + return Union([self, *other.trait_types]) else: return Union([self, other]) @@ -2044,8 +2044,8 @@ def validate(self, obj, value): value = self._resolve_string(value) except ImportError as e: raise TraitError( - "The '%s' trait of %s instance must be a type, but " - "%r could not be imported" % (self.name, obj, value) + "The '{}' trait of {} instance must be a type, but " + "{!r} could not be imported".format(self.name, obj, value) ) from e try: if issubclass(value, self.klass): # type:ignore[arg-type] @@ -2309,7 +2309,7 @@ def __or__(self, other): if isinstance(other, Union): return Union(self.trait_types + other.trait_types) else: - return Union(self.trait_types + [other]) + return Union([*self.trait_types, other]) def from_string(self, s): for trait_type in self.trait_types: @@ -2498,7 +2498,7 @@ def from_string(self, s): s = s[2:-1] warn( "Supporting extra quotes around Bytes is deprecated in traitlets 5.0. " - "Use %r instead of %r." % (s, old_s), + "Use {!r} instead of {!r}.".format(s, old_s), FutureWarning, ) break @@ -2547,7 +2547,9 @@ def from_string(self, s): s = s[1:-1] 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), + "You can use {!r} instead of {!r} if you require traitlets >=5.".format( + s, old_s + ), FutureWarning, ) return s