From 0a34100c2412322559b249f3092a86bb34f1f844 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 17 Oct 2023 15:22:15 -0500 Subject: [PATCH 1/4] Bump ruff to 0.1.0 --- .pre-commit-config.yaml | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9212c31fb..d2f1b03b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: - id: check-added-large-files # run ruff with --fix before black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: 'v0.0.287' + rev: 'v0.1.0' hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/pyproject.toml b/pyproject.toml index 9dda8bc82..74da7bee8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,7 +78,7 @@ development = [ "pytest-cov>=4.1.0", "pytest-timeout>=2.2.0", "pytest-xdist>=3.3.1", - "ruff>=0.0.287", + "ruff>=0.1.0", "twine>=4.0.2", "types-Pygments", "types-pyserial" From 31d68cca73ecf7f7c7fbde1af4c3b0c9d1a2810e Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 17 Oct 2023 15:23:19 -0500 Subject: [PATCH 2/4] Replace black with ruff --- .pre-commit-config.yaml | 12 +++++------- pyproject.toml | 2 +- ruff.toml | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d2f1b03b1..64975d98e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,15 +10,13 @@ repos: - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files -# run ruff with --fix before black + +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: 'v0.1.0' + hooks: + - id: ruff-format - repo: https://github.com/astral-sh/ruff-pre-commit rev: 'v0.1.0' hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] -- repo: https://github.com/psf/black - rev: 23.3.0 - hooks: - - id: black - args: [--safe, --quiet] - files: (examples|pymodbus|test)/ diff --git a/pyproject.toml b/pyproject.toml index 74da7bee8..9b5f12a07 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -143,7 +143,7 @@ enable = "all" disable = [ "duplicate-code", # TBD "file-ignored", # ONLY to be used with extreme care. - "format", # NOT wanted, handled by black + "format", # NOT wanted, handled by ruff "locally-disabled", # NOT wanted "suppressed-message" # NOT wanted""" ] diff --git a/ruff.toml b/ruff.toml index e8125c395..d0cd468f7 100644 --- a/ruff.toml +++ b/ruff.toml @@ -19,7 +19,7 @@ ignore = [ "RUF013", # implicit Optional "RUF015" # next(iter(list)) instead of list[0] ] -line-length = 120 +line-length = 88 select = [ "B007", # Loop control variable {name} not used within loop body "B014", # Exception handler with duplicate exception From f25e30d3db386b26cdbeeb2972e0456b5baa66ae Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 17 Oct 2023 15:39:24 -0500 Subject: [PATCH 3/4] Update exclude paths --- ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruff.toml b/ruff.toml index d0cd468f7..4ac34005c 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,10 +1,10 @@ target-version="py38" exclude = [ - "pymodbus/transport/serial_asyncio", "venv", ".venv", ".git", "build", + "doc" ] ignore = [ "D202", # No blank lines allowed after function docstring (to work with black) From e669cdf1cb71dfc617c9a4b9796a84246eb76d40 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Tue, 17 Oct 2023 15:39:38 -0500 Subject: [PATCH 4/4] pre-commit run --all-files --- pymodbus/device.py | 48 ++++++------------- pymodbus/utilities.py | 22 ++++----- .../test_register_read_messages.py | 3 +- test/sub_server/test_server_asyncio.py | 8 ++-- test/test_transaction.py | 6 +-- 5 files changed, 30 insertions(+), 57 deletions(-) diff --git a/pymodbus/device.py b/pymodbus/device.py index ff262b03d..0060f5540 100644 --- a/pymodbus/device.py +++ b/pymodbus/device.py @@ -220,23 +220,15 @@ def __str__(self): # -------------------------------------------------------------------------# # Properties # -------------------------------------------------------------------------# - VendorName = dict_property( - lambda s: s.__data, 0 # pylint: disable=protected-access - ) - ProductCode = dict_property( - lambda s: s.__data, 1 # pylint: disable=protected-access - ) - MajorMinorRevision = dict_property( - lambda s: s.__data, 2 # pylint: disable=protected-access - ) + # fmt: off + VendorName = dict_property(lambda s: s.__data, 0) # pylint: disable=protected-access + ProductCode = dict_property(lambda s: s.__data, 1) # pylint: disable=protected-access + MajorMinorRevision = dict_property(lambda s: s.__data, 2) # pylint: disable=protected-access VendorUrl = dict_property(lambda s: s.__data, 3) # pylint: disable=protected-access - ProductName = dict_property( - lambda s: s.__data, 4 # pylint: disable=protected-access - ) + ProductName = dict_property(lambda s: s.__data, 4) # pylint: disable=protected-access ModelName = dict_property(lambda s: s.__data, 5) # pylint: disable=protected-access - UserApplicationName = dict_property( - lambda s: s.__data, 6 # pylint: disable=protected-access - ) + UserApplicationName = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access + # fmt: on class DeviceInformationFactory: # pylint: disable=too-few-public-methods @@ -428,27 +420,17 @@ def summary(self): # -------------------------------------------------------------------------# # Properties # -------------------------------------------------------------------------# - BusMessage = dict_property( - lambda s: s.__data, 0 # pylint: disable=protected-access - ) - BusCommunicationError = dict_property( - lambda s: s.__data, 1 # pylint: disable=protected-access - ) - BusExceptionError = dict_property( - lambda s: s.__data, 2 # pylint: disable=protected-access - ) - SlaveMessage = dict_property( - lambda s: s.__data, 3 # pylint: disable=protected-access - ) - SlaveNoResponse = dict_property( - lambda s: s.__data, 4 # pylint: disable=protected-access - ) + # fmt: off + BusMessage = dict_property(lambda s: s.__data, 0) # pylint: disable=protected-access + BusCommunicationError = dict_property(lambda s: s.__data, 1) # pylint: disable=protected-access + BusExceptionError = dict_property(lambda s: s.__data, 2) # pylint: disable=protected-access + SlaveMessage = dict_property(lambda s: s.__data, 3) # pylint: disable=protected-access + SlaveNoResponse = dict_property(lambda s: s.__data, 4) # pylint: disable=protected-access SlaveNAK = dict_property(lambda s: s.__data, 5) # pylint: disable=protected-access SlaveBusy = dict_property(lambda s: s.__data, 6) # pylint: disable=protected-access - BusCharacterOverrun = dict_property( - lambda s: s.__data, 7 # pylint: disable=protected-access - ) + BusCharacterOverrun = dict_property(lambda s: s.__data, 7) # pylint: disable=protected-access Event = dict_property(lambda s: s.__data, 8) # pylint: disable=protected-access + # fmt: on # ---------------------------------------------------------------------------# diff --git a/pymodbus/utilities.py b/pymodbus/utilities.py index 37cc7069c..bedc81006 100644 --- a/pymodbus/utilities.py +++ b/pymodbus/utilities.py @@ -77,26 +77,20 @@ def dict_property(store, index): getter = lambda self: store( # pylint: disable=unnecessary-lambda-assignment self )[index] - setter = ( - lambda self, value: store( # pylint: disable=unnecessary-lambda-assignment - self - ).__setitem__(index, value) - ) + setter = lambda self, value: store( # pylint: disable=unnecessary-lambda-assignment + self + ).__setitem__(index, value) elif isinstance(store, str): getter = lambda self: self.__getattribute__( # pylint: disable=unnecessary-dunder-call,unnecessary-lambda-assignment store - )[ - index - ] + )[index] setter = lambda self, value: self.__getattribute__( # pylint: disable=unnecessary-dunder-call,unnecessary-lambda-assignment store - ).__setitem__( - index, value - ) + ).__setitem__(index, value) else: - getter = lambda self: store[ # pylint: disable=unnecessary-lambda-assignment - index - ] + getter = ( + lambda self: store[index] # pylint: disable=unnecessary-lambda-assignment + ) setter = lambda self, value: store.__setitem__( # pylint: disable=unnecessary-lambda-assignment index, value ) diff --git a/test/sub_messages/test_register_read_messages.py b/test/sub_messages/test_register_read_messages.py index b9e19967a..dcb1ef6c5 100644 --- a/test/sub_messages/test_register_read_messages.py +++ b/test/sub_messages/test_register_read_messages.py @@ -58,8 +58,7 @@ def setup_method(self): ReadWriteMultipleRegistersRequest( write_registers=0xAB, **arguments, - ): b"\x00\x01\x00\x05\x00\x01\x00" - b"\x01\x02\x00\xAB", + ): b"\x00\x01\x00\x05\x00\x01\x00" b"\x01\x02\x00\xAB", } self.response_read = { ReadRegistersResponseBase(self.values): TEST_MESSAGE, diff --git a/test/sub_server/test_server_asyncio.py b/test/sub_server/test_server_asyncio.py index e0ba7ccbc..42a5d2848 100755 --- a/test/sub_server/test_server_asyncio.py +++ b/test/sub_server/test_server_asyncio.py @@ -323,13 +323,13 @@ async def test_async_udp_server_serve_forever_twice(self): async def test_async_udp_server_roundtrip(self): """Test sending and receiving data on udp socket""" - expected_response = b"\x01\x00\x00\x00\x00\x05\x01\x03\x02\x00\x11" # value of 17 as per context + expected_response = ( + b"\x01\x00\x00\x00\x00\x05\x01\x03\x02\x00\x11" + ) # value of 17 as per context BasicClient.dataTo = TEST_DATA # slave 1, read register BasicClient.done = asyncio.Future() await self.start_server(do_udp=True) - random_port = self.server.transport._sock.getsockname()[ # pylint: disable=protected-access - 1 - ] + random_port = self.server.transport._sock.getsockname()[1] # pylint: disable=protected-access transport, _ = await self.loop.create_datagram_endpoint( BasicClient, remote_addr=("127.0.0.1", random_port) ) diff --git a/test/test_transaction.py b/test/test_transaction.py index fb236f2d5..0a05d1c1b 100755 --- a/test/test_transaction.py +++ b/test/test_transaction.py @@ -65,10 +65,8 @@ def test_calculate_expected_response_length(self): self._tm.client = mock.MagicMock() self._tm.client.framer = mock.MagicMock() self._tm._set_adu_size() # pylint: disable=protected-access - assert ( - not self._tm._calculate_response_length( # pylint: disable=protected-access - 0 - ) + assert not self._tm._calculate_response_length( # pylint: disable=protected-access + 0 ) self._tm.base_adu_size = 10 assert (