From 3e2d7a20946742384461206d7786c0cf3234374e Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 10 Feb 2025 22:14:05 +0900 Subject: [PATCH] Run pyupgrade to clean up Python 2 syntaxes Update all .py source files by $ pyupgrade --py3-only $(git ls-files | grep ".py$") to modernize the code according to Python 3 syntaxes. pep8 errors are fixed by $ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \ --in-place automaton Also add the pyupgrade hook to pre-commit to avoid merging additional Python 2 syntaxes. Change-Id: Ic81c8d6f1270895437132bfccd676c79e6dfce30 --- .pre-commit-config.yaml | 5 +++++ automaton/exceptions.py | 2 +- automaton/machines.py | 18 +++++++++--------- automaton/runners.py | 4 ++-- automaton/tests/test_fsm.py | 2 +- doc/source/conf.py | 1 - releasenotes/source/conf.py | 1 - 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 07917d3..edd4b53 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,3 +23,8 @@ repos: hooks: - id: hacking additional_dependencies: [] + - repo: https://github.com/asottile/pyupgrade + rev: v3.18.0 + hooks: + - id: pyupgrade + args: [--py3-only] diff --git a/automaton/exceptions.py b/automaton/exceptions.py index 069b98b..30aadae 100644 --- a/automaton/exceptions.py +++ b/automaton/exceptions.py @@ -37,4 +37,4 @@ class FrozenMachine(AutomatonException): """Exception raised when a frozen machine is modified.""" def __init__(self): - super(FrozenMachine, self).__init__("Frozen machine can't be modified") + super().__init__("Frozen machine can't be modified") diff --git a/automaton/machines.py b/automaton/machines.py index 896c4ce..23c431b 100644 --- a/automaton/machines.py +++ b/automaton/machines.py @@ -20,7 +20,7 @@ from automaton import _utils as utils from automaton import exceptions as excp -class State(object): +class State: """Container that defines needed components of a single state. Usage of this and the :meth:`~.FiniteMachine.build` make creating finite @@ -58,7 +58,7 @@ def _orderedkeys(data, sort=True): return list(data) -class _Jump(object): +class _Jump: """A FSM transition tracks this data while jumping.""" def __init__(self, name, on_enter, on_exit): self.name = name @@ -66,7 +66,7 @@ class _Jump(object): self.on_exit = on_exit -class FiniteMachine(object): +class FiniteMachine: """A finite state machine. This state machine can be used to automatically run a given set of @@ -413,7 +413,7 @@ class FiniteMachine(object): postfix_markings.append("^") if self._states[state]['terminal']: postfix_markings.append("$") - pretty_state = "%s%s" % ("".join(prefix_markings), state) + pretty_state = "{}{}".format("".join(prefix_markings), state) if postfix_markings: pretty_state += "[%s]" % "".join(postfix_markings) if self._transitions[state]: @@ -453,7 +453,7 @@ class HierarchicalFiniteMachine(FiniteMachine): 'reaction,terminal,machine') def __init__(self): - super(HierarchicalFiniteMachine, self).__init__() + super().__init__() self._nested_machines = {} @classmethod @@ -475,15 +475,15 @@ class HierarchicalFiniteMachine(FiniteMachine): if machine is not None and not isinstance(machine, FiniteMachine): raise ValueError( "Nested state machines must themselves be state machines") - super(HierarchicalFiniteMachine, self).add_state( + super().add_state( state, terminal=terminal, on_enter=on_enter, on_exit=on_exit) if machine is not None: self._states[state]['machine'] = machine self._nested_machines[state] = machine def copy(self, shallow=False, unfreeze=False): - c = super(HierarchicalFiniteMachine, self).copy(shallow=shallow, - unfreeze=unfreeze) + c = super().copy(shallow=shallow, + unfreeze=unfreeze) if shallow: c._nested_machines = self._nested_machines else: @@ -512,7 +512,7 @@ class HierarchicalFiniteMachine(FiniteMachine): also be used to initialize any state machines they contain (recursively). """ - super(HierarchicalFiniteMachine, self).initialize( + super().initialize( start_state=start_state) for data in self._states.values(): if 'machine' in data: diff --git a/automaton/runners.py b/automaton/runners.py index 485d00c..1c660d5 100644 --- a/automaton/runners.py +++ b/automaton/runners.py @@ -61,7 +61,7 @@ class FiniteRunner(Runner): """Create a runner for the given machine.""" if not isinstance(machine, (machines.FiniteMachine,)): raise TypeError("FiniteRunner only works with FiniteMachine(s)") - super(FiniteRunner, self).__init__(machine) + super().__init__(machine) def run(self, event, initialize=True): for transition in self.run_iter(event, initialize=initialize): @@ -104,7 +104,7 @@ class HierarchicalRunner(Runner): if not isinstance(machine, (machines.HierarchicalFiniteMachine,)): raise TypeError("HierarchicalRunner only works with" " HierarchicalFiniteMachine(s)") - super(HierarchicalRunner, self).__init__(machine) + super().__init__(machine) def run(self, event, initialize=True): for transition in self.run_iter(event, initialize=initialize): diff --git a/automaton/tests/test_fsm.py b/automaton/tests/test_fsm.py index fe79271..7f0ac97 100644 --- a/automaton/tests/test_fsm.py +++ b/automaton/tests/test_fsm.py @@ -39,7 +39,7 @@ class FSMTest(testcase.TestCase): return m def setUp(self): - super(FSMTest, self).setUp() + super().setUp() # NOTE(harlowja): this state machine will never stop if run() is used. self.jumper = self._create_fsm("down", add_states=['up', 'down']) self.jumper.add_transition('down', 'up', 'jump') diff --git a/doc/source/conf.py b/doc/source/conf.py index 9b36537..b314351 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/releasenotes/source/conf.py b/releasenotes/source/conf.py index e8d02e3..e6afd5b 100644 --- a/releasenotes/source/conf.py +++ b/releasenotes/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at