Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
2
votes
1 answer
Pylint cannot handle abstract subclasses of abstract base classes
I have a concrete class MyConcreteClass which subclasses an abstract
class MyABC2, which, in turn, subclasses another abstract class MyABC1:
import abc
class MyABC1 (object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def…

sds
- 58,617
- 29
- 161
- 278
2
votes
1 answer
Python format and validate class variable without instantiating it
I'm trying to validate and format a class variable. The class extends a class with ABCMeta as its __metaclass__ and I can't yet instantiate my child class. So when I run this below code it prints property object and not
the output I want, which I…

Anshu Kumar
- 606
- 6
- 18
2
votes
1 answer
Python Abstract Proper Way of Calling Library Imported in Base Class
What is the proper way of utilizing functions that are imported in the base class of an abstract class? For example: in base.py I have the following:
import abc
import functions
class BasePizza(object):
__metaclass__ = abc.ABCMeta
…

user2694306
- 3,832
- 10
- 47
- 95
2
votes
1 answer
pylint and abc - abstractmethod
import abc
class Human(object):
__metaclass__ = abc.ABCMeta
config = {
'num_ears': 2,
'num_hands': 2,
}
def __init__(self):
self.config = dict(self.config.items() + self.derived_config.items())
…

yangmillstheory
- 1,055
- 13
- 31
2
votes
1 answer
How do I combine wxPython, abc, and a metaclass mixin?
I have a base class from which other classes should inherit:
class AppToolbar(wx.ToolBar):
''' Base class for the Canary toolbars '''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# ... a few common…

Chris Krycho
- 3,125
- 1
- 23
- 35
2
votes
4 answers
What is a "nearly-empty" class?
Compile the following class
class Interface
{
virtual void doIt() = 0;
virtual ~Interface() = 0;
};
inline Interface::~Interface() {}
using gcc -fdump-class-hierarchy.
gcc emits
Class Interface
size=4 align=4
base size=4 base…

Tobias
- 6,388
- 4
- 39
- 64
1
vote
0 answers
Python 3.10+ deconstructing an over engineered solution to better understand how metaclasses work with properties, static methods, and classmethods
TL;DR
This question examines an over-engineered example of python metaclasses and dataclasses to create a LiteralEnum (for validating a stringly-typed keyword argument) like LinkageMethod and a KeywordsArgumentBaseClass for making wrappers around…

SumNeuron
- 4,850
- 5
- 39
- 107
1
vote
1 answer
What the purpose of creating Python class inherited from `abc.ABC` but without `abstractmethod`?
I've read TorchServe's default handlers' sources and found that the BaseHandler is inherited from abc.ABC and doesn't have any abstract method. The VisionHandler is the same.
What could be the reason and when I should use abc.ABC without…

feeeper
- 2,865
- 4
- 28
- 42
1
vote
2 answers
Abstract class for thread-related class without multiple inheritance
I want to create an abstract class for thread-related class. Class that want to enable threading must inherit threading.Thread class, however at the same time, class that want to enable the @abstractmethod must inherit abc.ABC class. Because…

LLL
- 229
- 2
- 13
1
vote
0 answers
See references of classes that implement python protocol in PyCharm
I was just wondering if you know a way of getting a list of all classes that follow the structure of a given python protocol. When using normal inheritance like ABCs, PyCharm is able to get a list of all subclasses (implementation) of this…

Mattia Peiretti
- 21
- 1
- 6
1
vote
2 answers
Is it necessary to use abc.ABC for each base class in multiple inheritance?
Consider the following code snippet:
import abc
class Base(abc.ABC):
@abc.abstractmethod
def foo(self):
pass
class WithAbstract(Base, abc.ABC):
@abc.abstractmethod
def bar(self):
pass
class…

Woltan
- 13,723
- 15
- 78
- 104
1
vote
1 answer
mypy throws error for abstractmethod created with decorator
I have a decorator that creates an abstractmethod from a simple method. It works as I'd expect, however if I run mypy, it tells me this:
mypy_try.py:20: error: Missing return statement [empty-body]
mypy_try.py:20: note: If the method is meant to be…

waszil
- 390
- 2
- 5
- 15
1
vote
1 answer
ABC vs typing: is it possible to make a parameter truly optional?
With the following code, type checkers like pylance or mypy will complain about argument count mismatch (base request method has 3, but override only 2):
from abc import ABC, abstractmethod
from typing import TypedDict, Optional
class…

barciewicz
- 3,511
- 6
- 32
- 72
1
vote
1 answer
Alternative that doesn't violate the Liskov substitution principle
Considering the following structure of classes:
from abc import ABC, abstractmethod
class ModelSettings(ABC):
...
class BlueModelSettings(ModelSettings):
...
class RedModelSettings(ModelSettings):
...
class Model(ABC):
…

Jivan
- 21,522
- 15
- 80
- 131
1
vote
0 answers
TypeError: 'ABCMeta' object is not subscriptable when running PageRank of PySpark
I'm trying to run this implementation of Page Rank. This is a part of the code:
import re
import sys
from operator import add
from typing import Iterable, Tuple
from pyspark.resultiterable import ResultIterable
from pyspark.sql import…

Mehdi Abbassi
- 627
- 1
- 7
- 24