Skip to content

remove unnecessary quantifier in regex in __format__ for ip_address.py #16362

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

s-sanjay
Copy link
Contributor

@s-sanjay s-sanjay commented Sep 24, 2019

This is very small re factor and a very straight forward change. There is no need to create an issue for this or add an entry in the news since no behavior change is happening.

Copy link
Contributor

@aeros aeros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @s-sanjay.

@@ -639,7 +639,7 @@ def __format__(self, fmt):
# From here on down, support for 'bnXx'

import re
fmt_re = '^(?P<alternate>#?)(?P<grouping>_?)(?P<fmt_base>[xbnX]){1}$'
fmt_re = '^(?P<alternate>#?)(?P<grouping>_?)(?P<fmt_base>[xbnX])$'
Copy link
Contributor

@aeros aeros Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The {1} at the end of the regex specifically makes it match only one instance, removing it will make this match all instances. I'm not sure if that's at all important within this context though, so it would make sense to check with those who added it in the first place.

From looking at the git blame, this was added in the commit f9c95a4, authored by @ewosborne and merged by @zware 13 days ago. Let's wait on feedback from @zware before proceeding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even without the {1} it will match only one instance.
for example we can check here https://regex101.com/r/xtB5yX/1 ( in fact in the explanation it says {1} is not needed )
removing the {1} does not have any effect on what will match.

Copy link
Contributor

@aeros aeros Sep 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even without the {1} it will match only one instance.

I believe you're correct, but it's always better to be on the safe side and double check with the core dev who added/approved of the previous version when making any code revisions, no matter how minor the changes are. Especially if the commit was made fairly recently and the core dev is currently active.

Either way though, a core developer still needs to approve and merge the PR before it can be added. IMO, the most well suited person for doing so would be @zware since he merged the PR that added this section. Alternatively, @serhiy-storchaka is the most active expert of the re module and has made several changes within ipaddress.

Edit: Also, thanks for the introduction to regex101. I've mostly used regexr in the past, but it's useful to know of a visual tool that supports the Python specific flavor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I was remembering incorrectly when I initially said:

The {1} at the end of the regex specifically makes it match only one instance, removing it will make this match all instances.

For re.match(), it only matches at the beginning of the string, not each line. For matching all occurences, re.findall() is used instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that makes sense. let us wait for others to review this PR.
yeah I use regex101 to understand the regex visually than parsing it in my mind. it is an amazing tool.

@aeros aeros requested a review from zware September 25, 2019 02:00
@aeros aeros added the type-feature A feature request or enhancement label Sep 25, 2019
@serhiy-storchaka
Copy link
Member

Thank you @s-sanjay. I agree that {1} is not needed here.

I would remove also ^ and $ and use re.fullmatch() instead of re.match().

And I noticed many other opportunities for simplification and optimization, so I have created a separate #16378 which includes your change.

@aeros
Copy link
Contributor

aeros commented Sep 25, 2019

Thanks for the verification @serhiy-storchaka.

@s-sanjay
Copy link
Contributor Author

s-sanjay commented Sep 25, 2019

@serhiy-storchaka that looks even better ! thanks 👍

@s-sanjay
Copy link
Contributor Author

I will close this PR since the other PR has subsumed the change @serhiy-storchaka @aeros167 ?

@s-sanjay s-sanjay closed this Sep 25, 2019
@aeros
Copy link
Contributor

aeros commented Sep 25, 2019

I will close this PR since the other PR has subsumed the change

Sounds good, @serhiy-storchaka should be able to add a "co-authored-by" note in the PR he opened that includes you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants