Skip to content

Commit c68abe6

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 50cf840 commit c68abe6

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
@@ -57,7 +57,8 @@ def __init__(self, ip: str, operator: Operator | None = None):
5757

5858
class PortField(CustomField):
5959
def __init__(self, port: int, operator: Operator | None = None):
60-
assert 0 <= port < 65536
60+
if not (0 <= port < 65536):
61+
raise ValueError(f"Port must be between 0 and 65535, got {port}")
6162
super().__init__(v=str(port), operator=operator, field_name="port")
6263

6364

tests/test_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def test_serialize_with_max_port(self):
163163
assert field.serialize() == "port:65535"
164164

165165
def test_invalid_port_negative(self):
166-
with pytest.raises(AssertionError):
166+
with pytest.raises(ValueError):
167167
PortField(-1)
168168

169169
def test_invalid_port_too_large(self):
170-
with pytest.raises(AssertionError):
170+
with pytest.raises(ValueError):
171171
PortField(65536)
172172

173173
def test_serialize_with_greater_operator(self):

0 commit comments

Comments
 (0)