X Tutup
The Wayback Machine - https://web.archive.org/web/20241008024239/https://github.com/python/cpython/issues/118710
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IPv{4,6}Address.version and max_prefixlen available on classes, not just instances #118710

Closed
abacabadabacaba opened this issue May 7, 2024 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@abacabadabacaba
Copy link

abacabadabacaba commented May 7, 2024

Feature or enhancement

Proposal:

Sometimes, when there is a function that can operate both on IPv4 and IPv6 addresses, it is convenient to pass it the corresponding type object (either IPv4Address or IPv6Address) as an argument. However, that function cannot access the properties version and max_prefixlen using those type objects, because they only work on instances. This can be worked around by constructing a dummy instance (e.g. t(0).max_prefixlen), but it would be more convenient if those properties could be used on the type object directly (i.e. t.max_prefixlen), because their value doesn't depend on the specific instance, only on the type.

The proposal is to make IPv4Address.version, IPv4Address.max_prefixlen, IPv6Address.version, and IPv6Address.max_prefixlen evaluate to 4, 32, 6, and 128, respectively. Currently, all those expressions evaluate to property descriptors.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@abacabadabacaba abacabadabacaba added the type-feature A feature request or enhancement label May 7, 2024
@ericvsmith
Copy link
Member

This seems reasonable to me.

@gpshead
Copy link
Member

gpshead commented Jun 18, 2024

While there is a tiny downside of these technically becoming writable even though they're supposed to be read only attributes... this is Python and what you ask for seems practical. Agreed, lets do it.

@abacabadabacaba
Copy link
Author

While there is a tiny downside of these technically becoming writable even though they're supposed to be read only attributes...

They don't become any more writable then they already are. Currently, they can be assigned on the type objects but not on the instances. This will remain. For example, in the current version of Python, it is possible to write IPv4Address.version = 5, and then the version of all IPv4Address instances will be 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants
X Tutup