Skip to content

Commit

Permalink
stricter type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechromancer committed May 1, 2024
1 parent 4a2bd1a commit 6349bd4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "radixtarget"
version = "0.0.9"
version = "1.1.0"
description = "Check whether an IP address belongs to a cloud provider"
authors = ["TheTechromancer"]
license = "GPL-3.0"
Expand All @@ -23,7 +23,7 @@ pytest-cov = "^5.0.0"
[tool.poetry-dynamic-versioning]
enable = true
metadata = false
format-jinja = 'v1.0.0.{{ distance }}'
format-jinja = 'v1.1.0.{{ distance }}'

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"]
Expand Down
9 changes: 7 additions & 2 deletions radixtarget/radixtarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ def search(self, host):
return self.dns_tree.search(host)

def make_ip(self, host):
if not isinstance(host, str):
if not self.is_ip(host):
raise ValueError(
f'Host "{host}" must be of str or ipaddress type, not "{type(host)}"'
)
try:
return ipaddress.ip_network(host)
return ipaddress.ip_network(host, strict=False)
except Exception:
return str(host).lower()
return host.lower()

def is_ip(self, host):
return ipaddress._IPAddressBase in host.__class__.__mro__
7 changes: 7 additions & 0 deletions radixtarget/test/test_pyradix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import pytest
import random
import logging
import ipaddress
Expand Down Expand Up @@ -66,6 +67,12 @@ def test_radixtarget():
rt.insert("evilcorp.co.uk", "custom_data")
assert rt.search("www.evilcorp.co.uk") == "custom_data"

with pytest.raises(ValueError, match=".*must be of str or ipaddress type.*"):
rt.insert(b"asdf")

with pytest.raises(ValueError, match=".*must be of str or ipaddress type.*"):
rt.search(b"asdf")

assert "net" in rt.dns_tree.root.children
assert "com" in rt.dns_tree.root.children

Expand Down

0 comments on commit 6349bd4

Please sign in to comment.