question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ignore ABC abstract methods

See original GitHub issue

Is your feature request related to a problem? Please describe. Sometimes you want to create an abstract class with abstract methods that will be overridden by other subclasses. For example:

import abc

class Foo(abc.ABC):
    @abc.abstractmethod
    def a():
        ...

class Bar(Foo):
    def a():
        return 1

Pay attention that while Bar.a content should be covered by test, the content of Foo.a should not since there is no actual gain in covering it with a test.

Describe the solution you’d like Coverage should ignore abstract methods by default and should not require coverage for them.

Describe alternatives you’ve considered If you think that sometimes abstract methods should be covered, maybe we can make this ability toggleable by a flag (--ignore-abstract) for example.

Additional context This API is available only in Python 3, therefore we should make sure we disable it in Python 2.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
nedbatcommented, Jul 22, 2021

There’s something people are missing in this discussion: since coverage.py v4.1, if you exclude a decorator, it excludes the entire decorated function. So the best exclude line here would be:

[report]
exclude_lines =
    @abstractmethod
    @abc.abstractmethod

Once you do that, the entire abstract method is excluded from coverage, no matter what statements it contains.

BTW, this is also being discussed in pytest-cov: https://github.com/pytest-dev/pytest-cov/issues/428

1reaction
nedbatcommented, Mar 2, 2021

You don’t want to add “…”, that’s a regex that matches almost every line of your code (se #1042). Try the setting I suggested, I think it will work for you.

I’m reluctant to add hard-coded options for language features, because different people approach them differently, and the language changes over time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Passing" the implementation of an abstract method through to ...
Your better bet is to define the ABC without the abstract method, and also define a mixin class that contains the method.
Read more >
abstract-class-instantiated / E0110 - Pylint 2.16.0-dev ...
Used when an abstract class with `abc.ABCMeta` as metaclass has abstract methods and is instantiated. Problematic code: import abc class Animal(abc.
Read more >
How to Fix Python Error 'Can't Instantiate Abstract Class'
Here's the fix for can't instantiate abstract class error. ... One is to pass 'ABC' to a class, and it should have an...
Read more >
Class Abstraction - Manual - PHP
Methods defined as abstract simply declare the method's signature; they cannot define the implementation. When inheriting from an abstract class, all methods ...
Read more >
Abstract Classes in Python - GeeksforGeeks
By default, Python does not provide abstract classes. ... ABC works by decorating methods of the base class as abstract and then registering ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found