X Tutup
The Wayback Machine - https://web.archive.org/web/20260321200233/https://github.com/python/cpython/pull/93985
Skip to content

GH-93880: Improved conciseness of ipaddress factory functions (GH-93917)#93985

Closed
Nougat-Waffle wants to merge 3 commits intopython:mainfrom
Nougat-Waffle:93880-ipaddress-factory-improvement
Closed

GH-93880: Improved conciseness of ipaddress factory functions (GH-93917)#93985
Nougat-Waffle wants to merge 3 commits intopython:mainfrom
Nougat-Waffle:93880-ipaddress-factory-improvement

Conversation

@Nougat-Waffle
Copy link
Contributor

gh-93880: Improved conciseness of ipaddress factory functions

@ghost
Copy link

ghost commented Jun 18, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@AlexWaygood
Copy link
Member

The issue number needs to be in the title of the PR, not the description of the PR

@Nougat-Waffle Nougat-Waffle changed the title Improved conciseness of ipaddress factory functions GH-93880: Improved conciseness of ipaddress factory functions (GH-93917) Jun 18, 2022
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

It doesn't seem to me that this PR fixes any bugs, and the extra import might make importing the module slower. Is there a strong rationale for this change?

You should also delete the news entry, since the changes this PR makes wouldn't be visible to end users in any way. I'll add the "skip news" label, which will mean the CI will still pass if you delete the news entry.

@Nougat-Waffle
Copy link
Contributor Author

Nougat-Waffle commented Jun 19, 2022

It doesn't seem to me that this PR fixes any bugs, and the extra import might make importing the module slower. Is there a strong rationale for this change?

The rationale for this change is purely to increase the readability and elegancy of the code.

You should also delete the news entry, since the changes this PR makes wouldn't be visible to end users in any way. I'll add the "skip news" label, which will mean the CI will still pass if you delete the news entry.

Done

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 22, 2022

The rationale for this change is purely to increase the readability and elegancy of the code.

This is a worthy goal, but, in this case, I'm not sure it's worth the extra import. But the final decision needs to be made by the ipaddress module maintainers; I'm just a triager, I'm afraid, and don't have merge privileges :)

@AlexWaygood
Copy link
Member

But the final decision needs to be made by the ipaddress module maintainers; I'm just a triager, I'm afraid, and don't have merge privileges :)

Sadly there are no active maintainers listed for ipaddress on the experts index. @ezio-melotti, would you mind taking a look?

@AlexWaygood AlexWaygood removed their request for review June 22, 2022 11:32
Copy link
Member

@ezio-melotti ezio-melotti left a comment

Choose a reason for hiding this comment

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

The changes seem reasonable, but I'm also not sure if it's worth merging it.

In theory this could be made even more concise with a function that does something like this:

def try_classes(address, classes, type, *args, **kwargs):
    for cls in classes:
        with contextlib.suppress(AddressValueError, NetmaskValueError):
            return cls(address, *args, **kwargs)
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 {type}')

or like this (if we want to get rid of the extra import):

def try_classes(address, classes, type, *args, **kwargs):
    for cls in classes:
        try:
            return cls(address, *args, **kwargs)
        except (AddressValueError, NetmaskValueError):
            pass
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 {type}')

but again I'm not sure if it's worth the extra complexity just to save a few lines

@ezio-melotti ezio-melotti requested a review from ncoghlan June 23, 2022 13:59
@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 8, 2023

Closing on the basis that:

  • The readability improvement here is relatively small.
  • The extra import comes with a performance cost.
  • Using contextlib.suppress is slower than using try/except.
  • There's no obvious bug being fixed to justify the code churn.
  • No other maintainers have come forward to defend this PR in the last 6 months.

Thanks for the PR, but we'll decline this one :)

@AlexWaygood AlexWaygood closed this Jan 8, 2023
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.

4 participants

X Tutup