python-iptools icon indicating copy to clipboard operation
python-iptools copied to clipboard

fix: raise clear TypeError for non-int in long2ip()

Open koteshyelamati opened this issue 5 months ago • 1 comments

Bug Fix

In Python 3, long2ip(None) raises:

TypeError: '<' not supported between instances of 'int' and 'NoneType'

This comes from the comparison MAX_IP < l failing when l is None. The doctest (which was written for Python 2) expects:

TypeError: unsupported operand type(s) for >>: 'NoneType' and 'int'

In Python 2, None < int was valid (always False), so execution reached the >> bit-shift operator. In Python 3, the comparison itself raises TypeError.

Fix

Added an explicit isinstance check before the comparison so that None and other non-integer inputs raise a consistent, clear TypeError("expected int between 0 and 4294967295 inclusive") — the same message already raised for out-of-range integers.

koteshyelamati avatar Jul 03 '26 20:07 koteshyelamati

I’m not sure I see the point of replacing a type error with a type error that has a different description after extra steps. What is your use case for this?

bd808 avatar Jul 03 '26 21:07 bd808

Fair question @bd808. The motivation: the module's own doctest documents the Py2-era message (unsupported operand type(s) for >>), which Python 3 can no longer produce — so the documented and actual behavior have drifted. Rather than update the doctest to assert on an incidental comparison error, I made non-int inputs raise the same explicit message the function already uses for out-of-range ints. If you'd rather just fix the doctest and keep the organic TypeError, happy to close this.

koteshyelamati avatar Jul 13 '26 20:07 koteshyelamati