From 8262ad6874adcafdd3019a0936ea12303d8df488 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Sun, 8 Feb 2026 00:12:13 -0300 Subject: [PATCH] Replace assert with ValueError in PortField validation Assertions can be stripped with python -O, silently disabling port range validation. Use ValueError with a descriptive message instead. Closes #34 --- leakix/field.py | 3 ++- tests/test_query.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/leakix/field.py b/leakix/field.py index 2ccf522..d63b2bc 100644 --- a/leakix/field.py +++ b/leakix/field.py @@ -59,7 +59,8 @@ def __init__(self, ip: str, operator: Operator | None = None) -> None: class PortField(CustomField): def __init__(self, port: int, operator: Operator | None = None) -> None: - assert 0 <= port < 65536 + if not (0 <= port < 65536): + raise ValueError(f"Port must be between 0 and 65535, got {port}") super().__init__(v=str(port), operator=operator, field_name="port") diff --git a/tests/test_query.py b/tests/test_query.py index 059ebae..2222f4f 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -161,11 +161,11 @@ def test_serialize_with_max_port(self) -> None: assert field.serialize() == "port:65535" def test_invalid_port_negative(self) -> None: - with pytest.raises(AssertionError): + with pytest.raises(ValueError): PortField(-1) def test_invalid_port_too_large(self) -> None: - with pytest.raises(AssertionError): + with pytest.raises(ValueError): PortField(65536) def test_serialize_with_greater_operator(self) -> None: