Adding functions to check the validation of IP address in lib `ipaddress`

Hi everyone!

In python, if we need to decide whether a IP address is valid, we need to do like that:

import ipaddress

def is_valid_ip(text):
    try:
        ipaddress.ip_address(text)
        return True
    except ValueError:
        return False

if is_valid_ip('ip_addr_here'):
    the_ip_is_valid()
else:
    the_ip_is_not_valid()

The helper function of is_valid_ip is very common in scripts. If I want to check if a IP address is valid, I need to write this every time, and it actually creates useless IP object here. Well, in my opinion, we can have something like ipaddress.is_ip():

import ipaddress

if ipaddress.is_ip('ip_addr_here'):
    the_ip_is_valid()
else:
    the_ip_is_not_valid()

which will be very convenient here.

Additionally, I think we can also have ipaddress.is_ipv4() and ipaddress.is_ipv6() in the same reason.

Thank you for your time!

Is it? Do you have evidence from e.g. examples on github?

Are all these implementations always identical? Are there design decisions that need to be made?

Not every 3-line function needs to be in the stdlib. Is this tricky to implement is in some way?

Is there a benefit of doing exception handling instead of a validate function? That is normally considered better style for python.

Is it? Do you have evidence from e.g. examples on github?

Sure, for instance, see here

Well, I think the function of checking validation is kind of important. The ipaddress.ip_address is supposed to be used in returning ip objects, not checking validation here IMO.

That is one example, in CPython’s stdlib[1], by you. That is not the same as “The helper function of is_valid_ip is very common in scripts”.

Also, notably, the IPv6 version in that PR is different to what I would expect based on your OP, indicating that there is some design room to play with.


  1. which often, but not always has different useage patterns than most code ↩︎

That is one example

I think certainly no here. I quote this from An introduction to the ipaddress module — Python 3.13.5 documentation:


This is a kind of suggested usage.

The point I’m making is, ipaddress.ip_address() is supposed to be used as returning an useful IP address object, but not checking the validation tho. I think we need some func returning True/False to check if the ip addr is legit. I don’t think exception handling is a good method here.

the IPv6 version in that PR is different

that is because it’s a special case in checking ipv6 like hostnames. We need to remove the [ and ]. In this case we are not actually detecting IP address.

1 Like

I think the problem is the definition of the term “valid”. What does valid mean? Accessible (generally or in our network), theoretically useable IP address, and so on and so on.

1 Like

The cost of creating an object really isn’t that much unless validating millions of IP-addresses is all that your application does. What kind of application are you developing where creating a useless IP object poses a non-insignificant impact?

FWIW the currently recommended way of validating a string as an integer is to call the int constructor with an exception handler.

Well, I think it is kind of common to validate a IP address and it would be good to have a helper func in stdlib. But your point of validating a int is correct here I think. Just to reduce the indention maybe and make the code looks more pythonic. I am really tired of writing these exception handling everytime I want to validate a IP

FWIW the currently recommended way of validating a string as an integer is to call the int constructor with an exception handler.

I don’t know if it’s a recommended way as I would instead suggest using isdigit() (the int constructor accepts numerical-like strings, so those that are categorized as digits, including the weird ones).

think it is kind of common to validate a IP address and it would be good to have a helper func in stdlib

I personally think it’s common in our case, not in the wild. I also didn’t know (and still don’t know) if it’s useful for someone else.


Now, the helper can serve when one wants to distinguish between an IPv4 and an IPv6 because currently, this means we need to actually either catch the ValueError (which may be raised by a custom address) or the exact exceptions (namely AddressValueError and NetmaskValueError).

If I want to check if a IP address is valid, I need to write this every time, and it actually creates useless IP object here. Well, in my opinion, we can have something like ipaddress.is_ip():

Just to be clear, the is_valid_ipaddress() would still create useless objects because it would be easiest way to implement it. I don’t think I want to extract the logic of _check_int_address and _check_packed_address as static methods so we would just have an except catching the exact exception types that the constructor may raise during validation (this is essentially the benefit of having an helper, namely we don’t need to catch a broad ValueError, just the exceptions that the validation is meant to raise).

1 Like