-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Abstract methods in non-abstract imporlib.metadata.Distribution #419
Comments
I can't remember if there was a reason why I chose not to configure the metaclass. Perhaps I thought it was optional. Thanks for the reference to the documentation, as it clarifies that ABCMeta should be used unconditionally if abstract methods are present. Also, thanks for the PR, which illustrates that simply configuring the meta class isn't viable, because downstream consumers are depending on the non-enforcement of the interface, making introduction of the enforcement a backward-incompatible change. I'll have to think about it more and determine if there's a path forward here and if so how. I'd like the test suite to include a test capturing the enhanced expectation here. It's also likely that this issue should be addressed first in But before we embark on a solution, what is the importance of this issue? Is the goal mainly to align best practices or do you have a more concrete goal in mind? |
I have encountered the issue when I was studying importlib.metatdata. So for me personally, I was just little bit confused by methods having abstractmethod, but the class not having metaclass=abc.ABCMeta. So only goal was to clarify the confusion, why there are abstract decorated methods where the decorater has no effect. |
This can be useful for type-checking purposes. While at runtime, the decorator only has an effect if the metaclass is Decorating methods with |
Thank @AlexWaygood for the information about mypy checking If this use of the decorator is a good practice, then I am happy with the resolution. |
In pypa/pip#11684, I reported the issue to pip so the usage can be corrected there, paving the way to potentially introduce the ABCMeta. In the meantime, it's probably safe to add the change to importlib_metadata to see if there are potentially other uses out there (seems unlikely). I'll transfer this issue to that project. |
See #422 where I'm pondering how to handle the backward compatibility concerns this change revealed. |
The empty methods read_text and locatate_file of Distribution class are decorated with abc.abstractmethod. But Distribution class doesn't have metaclass=abc.ABCMeta.
PEP 3119, Introducing Abstract Base Classes, states.:
Considering the facts that methods read_text and locate_file are used in other methods of Distribution class and both are empty, only things containing are docstrings, I think the author of the class intended to use the abstractmethod functionality to enforce an implementation of the aforementioned methods. So, in my opinion, the Distribution class should have metaclass=abc.ABCMeta.
(Other possibility is that the aforementioned methods are intended as stubs. Then the abstractmethod decorator may be deleted.)
I've created the PR in case the issue is right, i.e. the class should contain metaclass=abc.ABCMeta.
Linked PRs
The text was updated successfully, but these errors were encountered: