Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions pymodbus/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
This is the single location for storing default
values for the servers and clients.
"""
import enum

INTERNAL_ERROR = "Pymodbus internal error"


class ModbusStatus: # pylint: disable=too-few-public-methods
class ModbusStatus(int, enum.Enum):
"""These represent various status codes in the modbus protocol.

.. attribute:: Waiting
Expand Down Expand Up @@ -44,12 +45,8 @@ class ModbusStatus: # pylint: disable=too-few-public-methods
SlaveOn = 0xFF
SlaveOff = 0x00

def __init__(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we permit objects ? I do not see the need in contrary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum members are singletons by default, we don't need any special logic to guard them, Python handles that for us.

Copy link
Collaborator

@janiversen janiversen Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not prohibit that I can assign the class to a variable and make changes, with unexpected results.

And as far as I know, if you instanciate the class you get a new set of class variables (elements of enum).

Copy link
Contributor Author

@Booplicate Booplicate Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And as far as I know, if you instanciate the class you get a new set of class variables (elements of enum).

Actually, I believe no, class variables are static. You can change them, but restricting __init__ won't prevent that anyway.

I think I should clarify, __init__ is used to initialise enum members, not the enum itself.

Enums are using metaclasses underneath. Say, if somebody does ModbusStatus(0xFFFF), they will get ModbusStatus.WAITING, because enum members are singletons.

"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)


class Endian: # pylint: disable=too-few-public-methods
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced by the system constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understood, do you mean sys.byteorder?

class Endian(str, enum.Enum):
"""An enumeration representing the various byte endianness.

.. attribute:: Auto
Expand All @@ -73,12 +70,8 @@ class Endian: # pylint: disable=too-few-public-methods
Big = ">"
Little = "<"

def __init__(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not permit to instantiate any constant class.

"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)


class ModbusPlusOperation: # pylint: disable=too-few-public-methods
class ModbusPlusOperation(int, enum.Enum):
"""Represents the type of modbus plus request.

.. attribute:: GetStatistics
Expand All @@ -95,12 +88,8 @@ class ModbusPlusOperation: # pylint: disable=too-few-public-methods
GetStatistics = 0x0003
ClearStatistics = 0x0004

def __init__(self):
"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)


class DeviceInformation: # pylint: disable=too-few-public-methods
class DeviceInformation(int, enum.Enum):
"""Represents what type of device information to read.

.. attribute:: Basic
Expand Down Expand Up @@ -132,12 +121,8 @@ class DeviceInformation: # pylint: disable=too-few-public-methods
Extended = 0x03
Specific = 0x04

def __init__(self):
"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)


class MoreData: # pylint: disable=too-few-public-methods
class MoreData(int, enum.Enum):
"""Represents the more follows condition.

.. attribute:: Nothing
Expand All @@ -151,7 +136,3 @@ class MoreData: # pylint: disable=too-few-public-methods

Nothing = 0x00
KeepReading = 0xFF

def __init__(self):
"""Prohibit objects."""
raise RuntimeError(INTERNAL_ERROR)
2 changes: 0 additions & 2 deletions test/sub_messages/test_mei_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def test_read_device_information_encode(self):
)
result = handle.encode()
assert result == message
assert str(handle) == "ReadDeviceInformationResponse(1)"

dataset = {
0x00: "Company",
Expand Down Expand Up @@ -133,7 +132,6 @@ def test_read_device_information_encode_long(self):
)
result = handle.encode()
assert result == message
assert str(handle) == "ReadDeviceInformationResponse(1)"

def test_read_device_information_decode(self):
"""Test that the read device information response can decode"""
Expand Down