Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kitten hints: Add new IP (matches v4 and v6) type #3009

Merged
merged 2 commits into from Oct 6, 2020
Merged

Kitten hints: Add new IP (matches v4 and v6) type #3009

merged 2 commits into from Oct 6, 2020

Conversation

Sighery
Copy link
Contributor

@Sighery Sighery commented Oct 5, 2020

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 ip type, and a new exception called InvalidMatch, which can be raised by the postprocessor to ignore a given match. This postprocessor for ip makes use of the Python standard library's ipaddress module 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-accepted label 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 馃憤

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.
@kovidgoyal kovidgoyal merged commit fe97bbc into kovidgoyal:master Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants