Skip to content

Conversation

@xsedla1o
Copy link
Contributor

Fixes incorrect behavior of UnirecIPAddrRange.__contains__ due to incomplete type checking:

Previous behavior

from pytrap import UnirecIPAddrRange, UnirecIPAddr
r = UnirecIPAddrRange("192.168.42.0/24")
a = UnirecIPAddr("192.168.42.42")
print(a in r)       # True, OK
print(str(a) in r)  # False, should TypeError
print(42 in r)      # False, should TypeError

New behavior

from pytrap import UnirecIPAddrRange, UnirecIPAddr
r = UnirecIPAddrRange("192.168.42.0/24")
a = UnirecIPAddr("192.168.42.42")
print(a in r)       # True, OK
print(str(a) in r)  # TypeError: pytrap.UnirecIPAddrRange object expected, got 'str'.
print(42 in r)      # TypeError: pytrap.UnirecIPAddrRange object expected, got 'int'.

Also fixes adjacent behavior of UnirecIPAddrRange.isOverlap and UnirecIPAddrRange.isIn.

Previously:

from pytrap import UnirecIPAddrRange, UnirecIPAddr
r = UnirecIPAddrRange("192.168.42.0/24")
r2 = UnirecIPAddrRange("192.168.42.128/25")
a = UnirecIPAddr("192.168.42.42")

print(r.isOverlap(r2))        # True, OK
print(r.isOverlap(r2.start))  # NotImplemented, not great because bool(NotImplemented) is True
print(r.isOverlap(42))        # NotImplemented again.

print(r.isIn(a))       # 0, OK
print(r.isIn(str(a)))  # 1, should TypeError
print(r.isIn(0))       # 1, should TypeError

New behavior

from pytrap import UnirecIPAddrRange, UnirecIPAddr
r = UnirecIPAddrRange("192.168.42.0/24")
r2 = UnirecIPAddrRange("192.168.42.128/25")
a = UnirecIPAddr("192.168.42.42")

print(r.isOverlap(r2))        # True, OK
print(r.isOverlap(r2.start))  # TypeError: UnirecIPAddrRange object expected, got 'pytrap.UnirecIPAddr'.
print(r.isOverlap(42))        # TypeError: UnirecIPAddrRange object expected, got 'int'.

print(r.isIn(a))       # 0, OK
print(r.isIn(str(a)))  # TypeError: UnirecIPAddr object expected, got 'str'.
print(r.isIn(0))       # TypeError: UnirecIPAddr object expected, got 'int'.

Previous behavior was to return Py_NotImplemented,
which ended up as always `False` in python usage.
@xsedla1o xsedla1o self-assigned this Jan 29, 2025
@xsedla1o xsedla1o requested a review from cejkato2 January 29, 2025 13:48
@codecov-commenter
Copy link

codecov-commenter commented Jan 29, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.36%. Comparing base (cbd92bb) to head (b74b316).
Report is 2 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #235      +/-   ##
==========================================
+ Coverage   85.00%   86.36%   +1.36%     
==========================================
  Files           2        2              
  Lines          20       22       +2     
  Branches        2        2              
==========================================
+ Hits           17       19       +2     
  Misses          2        2              
  Partials        1        1              
Flag Coverage Δ
tests 86.36% <ø> (+1.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cejkato2 cejkato2 merged commit 7031b1c into master Feb 5, 2025
2 checks passed
@cejkato2 cejkato2 deleted the pytrap_fix_typeerror branch February 5, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants