5

As I understand it, Python (2.5.2) does not have real support for abstract classes. Why is pylint complaining about this class being an "Abstract class not reference?" Will it do this for any class that has NotImplementedError thrown?

I have each class in its own file so if this is the case I guess I have no choice but to suppress this message but I am hoping there is maybe another way around it.

"""Package Repository interface."""


class PackageRepository(object):
    """Package Repository interface."""

    def __init__(self):
        self.hello = "world"

    def get_package(self, package_id):
        """
        Get a package by ID.
        """
        raise NotImplementedError( \
                "get_package() method has not been implemented")

    def get_packages(self):
        """
        Get all packages.
        """
        raise NotImplementedError( \
                "get_packages() method has not been implemented")

    def commit(self):
        """
        Commit all changes.
        """
        raise NotImplementedError( \
                "commit() method has not been implemented")

    def do_something(self):
        """
        Doing something.
        """
        return self.hello

EDIT

Perhaps I should clarify. I realize this is an abstract class and I would love to use the abstract keyword but as I understand it none of that matters in Python (at least in the version I am currently using) so I didn't bother doing any funny abstract tricks (like those found here) and simply left it out.

I was surprised to see that pylint picks up on the fact that this is an abstract class on its own. What makes pylint determine this is an abstract class? Is it simply looking for NotImplementedError being thrown somewhere?

Beau Simensen
  • 4,558
  • 3
  • 38
  • 55
  • 6
    What makes you say that it's **not** abstract? It sure can't be used as it is. It throws exceptions for each method function. What do you mean by "abstract" if this is not an example? – S.Lott Dec 30 '09 at 19:34
  • It is confusing to me that Python does not support abstract classes but pylint will complain if one is defined but not referenced within the same file. It makes me wonder if I am doing something wrong that is triggering this warning. I prefer to have my classes all in their own files so I am bound to run into this often and would like to know if there is something I should be doing more Pythonic that would help me get around this warning. – Beau Simensen Dec 30 '09 at 19:43
  • 1
    @Beau Simensen: Why are you focused on one-class-per-file. That's very non-Pythonic. Indeed, that's the root cause of your problem. No one (especially not pylint) expects one-class-per-file. Why are you doing it this way? Please update your question with some explanation for why you're doing this. – S.Lott Dec 30 '09 at 19:49
  • 5
    FWIW, raising NotImplementedError is enough to make pylint think this is an abstract class (which is absolutely correct). from http://www.logilab.org/card/pylintfeatures: W0223: Method %r is abstract in class %r but is not overridden Used when an abstract method (ie raise NotImplementedError) is not overridden in concrete class. – Tobiesque Dec 30 '09 at 20:01
  • 2
    Tobiesque has the right answer...now if only he'd post it as an answer :P – Pace Dec 30 '09 at 20:17
  • @Tobiesque Thanks for finding that. I only looked up R0921 which didn't explain it exactly. Would you mind adding this information as an answer and I will accept it as answering my question? – Beau Simensen Dec 30 '09 at 20:17
  • @S.Lott I am still trying to get a good feel for organizing my code in Python. The more I learn, the easier it is to understand why things are done in a certain way. Knowing when to put classes into the same module vs. putting them into separate modules is not something I have mastered yet as it feels very foreign to me. I have been reading over http://docs.python.org/tutorial/modules.html#packages to help me figure this out. – Beau Simensen Dec 30 '09 at 20:27
  • http://stackoverflow.com/questions/1801878/the-pythonic-way-of-organizing-modules-and-packages, http://stackoverflow.com/questions/106896/how-many-python-classes-should-i-put-in-one-file. – S.Lott Dec 30 '09 at 22:09
  • 1
    Tobiesque has the right answer in so far as the Pylint docs are concerned, but in what bizarre way is a class which has a method which raises NotImplemented an abstract class? You can still instantiate the class (which is why you'd put `raise NotImplementedError` instead of just `pass` for the body of the method), so it seems an awful stretch to call any such class "abstract". This just makes no sense at all, especially when now (in 2.6+) Python in fact *does* support [abstract base classes](http://docs.python.org/library/abc.html). – Adam Parkin Jan 12 '12 at 21:54

2 Answers2

12

FWIW, raising NotImplementedError is enough to make pylint think this is an abstract class (which is absolutely correct). from logilab.org/card/pylintfeatures: W0223: Method %r is abstract in class %r but is not overridden Used when an abstract method (ie raise NotImplementedError) is not overridden in concrete class. – Tobiesque 2 hours ago

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    Hint to @Tobiesque: Post your answer and get the reputation. That way I can delete this answer in favor of yours. – S.Lott Dec 30 '09 at 22:10
  • This might be a bit too much. I use the nice stuff in `abc` module to define abstract classes and methods, and I use `NotImplementedError` for stuff that is not implemented yet, for instance. – Jérôme Oct 25 '17 at 15:54
1

In my experience, pylint is a bit over-zealous, and isn't useful until you've turned off a number of the warnings.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662