gh-153769: Clarify the TypeError from ipaddress subnet_of()/supernet_of()#154147
Open
fedonman wants to merge 1 commit into
Open
gh-153769: Clarify the TypeError from ipaddress subnet_of()/supernet_of()#154147fedonman wants to merge 1 commit into
fedonman wants to merge 1 commit into
Conversation
…ernet_of() Passing something that is not a network object, such as an address, to IPv4Network.subnet_of() or supernet_of() made _is_subnet_of() reach for the network_address attribute, fail with an AttributeError, and turn that into a TypeError with a message that read like a real containment result and hid the actual cause. Check that both arguments are network objects up front and raise a clear TypeError explaining what is required, so nothing masks an internal AttributeError anymore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calling
subnet_of()orsupernet_of()on a network and passing something that is not a network object, such as an address, gives a confusing error today:An address has a
versionattribute, so the version check passes, and the code then reaches fornetwork_address, which the address does not have. TheAttributeErroris caught and re-raised as aTypeErrorwhose message reads like a real containment result and hides what actually went wrong.This checks that both arguments are network objects before touching any attributes, and raises a clear
TypeErrorthat says a network object is required. Nothing masks an internalAttributeErroranymore:The existing behaviour for valid networks and for the mixed IPv4/IPv6 case is unchanged.
ipaddress:subnet_of()andsupernet_of()raise confusingTypeErrorwrapping anAttributeErrorwhen passed an Address object #153769