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
This commit is contained in:
Takashi Kajinami 2025-02-10 22:14:05 +09:00
parent 68317f5bd7
commit 3e2d7a2094
7 changed files with 18 additions and 15 deletions

View File

@ -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]

View File

@ -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")

View File

@ -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:

View File

@ -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):

View File

@ -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')

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -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