-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
bpo-42403: Use @staticmethod in importlib #23395
Conversation
What's the point? Are there any benefits in using staticmethod? I personally consider staticmethod a misfeature that should be avoided. |
See https://julien.danjou.info/guide-python-static-class-abstract-methods/ I like |
Hum, test_importlib fails with this change. Example:
|
It was a stupid mistake in my PR. It's now fixed ;-) |
I agree that using staticmethod+hardcoding the class name is a bad idea, but it's not the case here: semantically those are staticmethods, not classmethods as they don't need to know in which class they are. Marking them as so is an information to the reader. |
To be clear, this change doesn't prevent subclasses to access self or cls (if using |
Yeah, that's the main purpose of this change. |
Use @staticmethod on methods using @classmethod but don't use their cls parameter, of the following classes: * BuiltinImporter * FrozenImporter * WindowsRegistryFinder * PathFinder
PR rebased after I merged my other importlib PR #23397. |
@tiran: So are you ok with this change? |
I don't have a hard opinion here. If you like to change it, go ahead. I personally don't like staticmethod because I don't see a valid use case for it. I see them as sign of bad design. Either a method is bound to an instance, bound to a class, or it should be a function in the first place. |
The importer API requires create_module(sep) to be a method, to be able to call my_impoter.create_module(sep). See importlib.abc.Loader ABC. The implementation is free to use a static method (no extra parameter), a class method (cls) or a regular method (self).
I'm a little bit confused between "loader" and "importer", but it doesn't matter much to explain my point ;-) Marking a method with |
Use @staticmethod on methods using @classmethod but don't use their cls parameter on the following classes: * BuiltinImporter * FrozenImporter * WindowsRegistryFinder * PathFinder Leave methods using @_requires_builtin or @_requires_frozen unchanged, since this decorator requires the wrapped method to have an extra parameter (cls or self).
Use @staticmethod on methods using @classmethod but don't use their
cls parameter, of the following classes:
https://bugs.python.org/issue42403