Skip to content

Commit 3b796c9

Browse files
committed
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
1 parent 1b61994 commit 3b796c9

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

leakix/field.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def __init__(self, ip: str, operator: Operator | None = None) -> None:
5959

6060
class PortField(CustomField):
6161
def __init__(self, port: int, operator: Operator | None = None) -> None:
62-
assert 0 <= port < 65536
62+
if not (0 <= port < 65536):
63+
raise ValueError(f"Port must be between 0 and 65535, got {port}")
6364
super().__init__(v=str(port), operator=operator, field_name="port")
6465

6566

tests/test_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ def test_serialize_with_max_port(self) -> None:
161161
assert field.serialize() == "port:65535"
162162

163163
def test_invalid_port_negative(self) -> None:
164-
with pytest.raises(AssertionError):
164+
with pytest.raises(ValueError):
165165
PortField(-1)
166166

167167
def test_invalid_port_too_large(self) -> None:
168-
with pytest.raises(AssertionError):
168+
with pytest.raises(ValueError):
169169
PortField(65536)
170170

171171
def test_serialize_with_greater_operator(self) -> None:

0 commit comments

Comments
 (0)