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

Wrong error in Metric.__iter__ #1536

Closed
ValerianRey opened this issue Feb 21, 2023 · 1 comment · Fixed by #1538
Closed

Wrong error in Metric.__iter__ #1536

ValerianRey opened this issue Feb 21, 2023 · 1 comment · Fixed by #1538
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Milestone

Comments

@ValerianRey
Copy link
Contributor

ValerianRey commented Feb 21, 2023

🐛 Bug

In the class Metric, the __iter__ method is defined as follows:

def __iter__(self):
    """Iteration over metrics are not allowed. Use metric collections for nesting metrics."""
    raise NotImplementedError("Metrics does not support iteration.")

In python's docs, the following note is written about NotImplementedError (https://docs.python.org/3/library/exceptions.html#NotImplementedError):

It should not be used to indicate that an operator or method is not meant to be supported at all – in that case either leave the operator / method undefined or, if a subclass, set it to None.

In fact, the use cases for NotImplementedError are:

In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added.

In PyCharm (and maybe other python code checkers), this leads to a warning for every sub-class of Metric, saying that all abstract methods should be implemented (PyCharm understands a method that raises a NotImplementedError as abstract, even if there is no @abstractmethod decorator on this method).

Was there a good reason to define __iter__ like that? Otherwise, could we remove it?

@ValerianRey ValerianRey added bug / fix Something isn't working help wanted Extra attention is needed labels Feb 21, 2023
@ValerianRey
Copy link
Contributor Author

ValerianRey commented Feb 21, 2023

Since __getitem__ is defined, we need to specifically disable __iter__, so I understand why it was defined like that. I think the correct way to define it is to simply set:

__iter__ = None

in Metric.

Apparently it's possible to do that since python 3.6: https://docs.python.org/3/whatsnew/3.6.html#other-language-changes
They specifically take __iter__ as an example:

It is now possible to set a special method to None to indicate that the corresponding operation is not available. For example, if a class sets __iter__() to None, the class is not iterable. (Contributed by Andrew Barnert and Ivan Levkivskyi in bpo-25958.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants