Kitten hints: Add new IP (matches v4 and v6) type#3009
Merged
kovidgoyal merged 2 commits intokovidgoyal:masterfrom Oct 6, 2020
Sighery:master
Merged
Kitten hints: Add new IP (matches v4 and v6) type#3009kovidgoyal merged 2 commits intokovidgoyal:masterfrom Sighery:master
kovidgoyal merged 2 commits intokovidgoyal:masterfrom
Sighery:master
Conversation
It selects v4 and v6 IPs with a simple regex that doesn't actually check for the validity of the IPs.
On the initial commit of this feature, IPs were just matched with a very simple regex that prioritised simplicity/readability over accuracy. This commit adds a postprocessor for ip matches that makes use of Python's `ipaddress` in the standard library to validate all the IP matches. This way we don't need huge and complex regex patterns to match _and_ validate the IPs, and we can just use `ipaddress` to abstract us from implementing all the validation logic into the regex pattern.
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.
This PR adds a new type for the hints kitten: IP.
This type matches both v4 and v6 IPs. Subnet mask notation (
ip/32) is not supported, but could easily be added.This PR contains two commits:
The first commit adds a very simple regex to match IPv4/IPv6 with no validation and tests that cover matching valid and invalid IPs.
The second commit adds validation by adding a postprocessor for the
iptype, and a new exception calledInvalidMatch, which can be raised by the postprocessor to ignore a given match. This postprocessor foripmakes use of the Python standard library'sipaddressmodule to validate the matched IPs. This commit also improves the previous test by adding a few testcases of invalid matches being ignored.The bodies of the commit contain this information as well, but as for the reasoning behind doing the validation in Python: Validating IPs with regex (especially IPv6 with its multiple notations) is hard. There are some regex patterns you can find online for matching IPv6 and all its different notations, but they all have a thing in common: they're very long (the smallest I found was well over 400 characters), and complex.
For this PR, I decided on readability/simplicity, and using existing and tested Python code (
ipaddress) over some complex regex to do the validation as well.That being said, since the commits are separated, if you don't agree with this approach (or allowing postprocessors to "ignore" a match by raising
InvalidMatch), it can simply be dropped, and just re-use the first commit which only adds the simply regex pattern (which can be replaced by the more complex ones that also perform validation) and a testcase for this new hints type.As an aside, if you end up merging this, I would appreciate it if you add the
hacktoberfest-acceptedlabel to the PR (following new restrictions to this year's Hacktoberfest) so this PR counts towards that, but it's fine if you don't 👍