Skip to content

Commit e06a19d

Browse files
authored
Make Parity a string enum (#72)
* Make Parity a string enum * Python 3.10 compat * Pin mypy to 3.10 * Drop unused pre-commit config * Add a test
1 parent 0d770f6 commit e06a19d

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

.pre-commit-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
ci:
2-
autofix_commit_msg: "Apply pre-commit auto fixes"
3-
autoupdate_commit_msg: "Auto-update pre-commit hooks"
4-
skip: [mypy]
5-
61
repos:
72
- repo: https://github.com/codespell-project/codespell
83
rev: v2.4.1

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ asyncio_mode = "auto"
7171
asyncio_default_fixture_loop_scope = "function"
7272

7373
[tool.mypy]
74+
python_version = "3.10"
7475
strict = true
7576
check_untyped_defs = true
7677
show_error_codes = true

serialx/common.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ class StopBits(Enum):
184184
TWO = 2
185185

186186

187-
class Parity(Enum):
187+
class Parity(str, Enum):
188188
"""Parity configuration."""
189189

190-
NONE = None
191-
ODD = 1
192-
EVEN = 2
193-
MARK = 3
194-
SPACE = 4
190+
NONE = "N"
191+
ODD = "O"
192+
EVEN = "E"
193+
MARK = "M"
194+
SPACE = "S"
195195

196196

197197
class PinState(Enum):
@@ -300,7 +300,7 @@ def __init__(
300300
path: str | Path | None = None,
301301
baudrate: int = 9600,
302302
*,
303-
parity: Parity | None = Parity.NONE,
303+
parity: Parity | str | None = Parity.NONE,
304304
stopbits: StopBits | int | float = StopBits.ONE,
305305
xonxoff: bool = False,
306306
rtscts: bool = False,
@@ -327,7 +327,9 @@ def __init__(
327327
if not isinstance(stopbits, StopBits):
328328
stopbits = StopBits(stopbits)
329329

330-
if not isinstance(parity, Parity):
330+
if parity is None:
331+
parity = Parity.NONE
332+
elif not isinstance(parity, Parity):
331333
parity = Parity(parity)
332334

333335
self._path = path

tests/test_pyserial_compat.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
allow_module_level=True,
1212
)
1313

14-
from serialx import Serial, SerialPortInfo
14+
from serialx import Parity, Serial, SerialPortInfo
1515
from serialx.tools.list_ports import comports, grep
1616
from serialx.tools.list_ports_common import ListPortInfo
1717
from tests.common import SerialPair
@@ -82,6 +82,12 @@ def test_compat_timeout_setter(serial_pair: SerialPair) -> None:
8282
assert s.read_timeout == 0.5
8383

8484

85+
def test_compat_parity_none(serial_pair: SerialPair) -> None:
86+
"""Test that `parity=None` is accepted and maps to `Parity.NONE`."""
87+
with Serial.from_url(serial_pair.left, baudrate=115200, parity=None) as s:
88+
assert s.parity is Parity.NONE
89+
90+
8591
def test_compat_baudrate_setter(serial_pair: SerialPair) -> None:
8692
"""Test that the deprecated .baudrate setter reconfigures the port."""
8793
with Serial.from_url(serial_pair.left, baudrate=9600) as s:

0 commit comments

Comments
 (0)