Skip to content

Provide typehints for payload classes and methods #2624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
campbellmbrown opened this issue Apr 3, 2025 · 1 comment
Closed

Provide typehints for payload classes and methods #2624

campbellmbrown opened this issue Apr 3, 2025 · 1 comment

Comments

@campbellmbrown
Copy link

Issue

It would be nice if the payload.py module had consistent typehinting to pass static linting tools (e.g. MyPy):

For example,

def decode_8bit_int(self):
def decode_16bit_int(self):
def decode_32bit_int(self):
def decode_64bit_int(self):

should be:

def decode_8bit_int(self) -> int:
def decode_16bit_int(self) -> int:
def decode_32bit_int(self) -> int:
def decode_64bit_int(self) -> int:

Note half of this module is already typehinted.

Reproduce

Set up environment:

$ python --version
Python 3.13.2

$ python -m venv .venv

$ .venv\Scripts\activate

$ pip install pymodbus mypy

$ pip list
Package           Version
----------------- -------
mypy              1.15.0
mypy-extensions   1.0.0
pip               24.3.1
pymodbus          3.8.6
typing_extensions 4.13.1

Basic example:

from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian

decoder = BinaryPayloadDecoder(b"\x01\x02\x03\x04\x05\x06\x07\x08", byteorder=Endian.BIG)
print(decoder.decode_16bit_int())
print(decoder.decode_32bit_int())
print(decoder.decode_8bit_int())

Output:

$ mypy mypy_test.py --strict
mypy_test.py:4: error: Call to untyped function "BinaryPayloadDecoder" in typed context  [no-untyped-call]
mypy_test.py:5: error: Call to untyped function "decode_16bit_int" in typed context  [no-untyped-call]
mypy_test.py:6: error: Call to untyped function "decode_32bit_int" in typed context  [no-untyped-call]
mypy_test.py:7: error: Call to untyped function "decode_8bit_int" in typed context  [no-untyped-call]
Found 4 errors in 1 file (checked 1 source file)

Workaround

A MyPy specific workaround is to add # type: ignore[no-untyped-call] to each line, but this isn't ideal.

@janiversen
Copy link
Collaborator

payload.py will be removed in the next release, and is already gone on dev so it's hard to type hint.

@janiversen janiversen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants