Questions tagged [abc]

Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.

280 questions
10
votes
1 answer

Python inheritance, metaclasses and type() function

I can't understand why the following code behaves a particular way, which is described below: from abc import ABCMeta class PackageClass(object): __metaclass__ = ABCMeta class MyClass1(PackageClass): pass MyClass2 =…
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
9
votes
1 answer

Why does @abstractmethod need to be used in a class whose metaclass is derived from ABCMeta?

PEP 3119 states that: The @abstractmethod decorator should only be used inside a class body, and only for classes whose metaclass is (derived from) ABCMeta. Dynamically adding abstract methods to a class, or attempting to modify the abstraction…
8
votes
4 answers

Abstract base classes and Exceptions

Question Why do virtual subclasses of an abstract Exception created using the ABCMeta.register not match under the except clause? Background I'd like to ensure that exceptions that get thrown by a package that I'm using are converted to MyException,…
Mike McCoy
  • 263
  • 1
  • 11
8
votes
2 answers

Two-level abstract class hierarchy without 'consistent method resolution' error

I need two levels of abstract classes and a third level concrete class: from abc import ABC class Shape(ABC): ... class Quad(ABC, Shape): ... class Square(Quadrilateral) ... This code generates TypeError: Cannot create a consistent method…
Keith Wiley
  • 683
  • 2
  • 6
  • 14
8
votes
1 answer

Python Abstract Method With It's own __init__ function

How can I define a __init__ function in both the base and derived abstract classes and have all self.* be available in the abstract method? For example:  What is the proper way of utilizing functions that are imported in the base class of an…
user2694306
  • 3,832
  • 10
  • 47
  • 95
8
votes
1 answer

How can I combine abc.abstractproperty with a classmethod to make an "abstract class property"?

I'd like to create a "class property" that is declared in an abstract base class, and then overridden in a concrete implementation class, while keeping the lovely assertion that the implementation must override the abstract base class' class…
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
8
votes
1 answer

Why can `__subclasshook__` be monkeypatched onto the metaclass but `__instancecheck__` cannot?

Here is a toy example of trying to create a decorator that allows declaration of attribute names which should be required parts of "interface checking" along the standard __subclasshook__ and __instancecheck__ patterns. It seems to work as expected…
ely
  • 74,674
  • 34
  • 147
  • 228
8
votes
2 answers

How to annotate a member as abstract in Sphinx documentation?

The following two property definitions show up exactly the same in Sphinx autodoc HTML output: @property def concrete(self): """This is the concrete docstring""" pass @abstractproperty def abstract(self): """This is the abstract…
cdwilson
  • 4,310
  • 4
  • 26
  • 32
7
votes
1 answer

Can I mark a class abstract even if all its methods are implemented?

Is there a way to mark a python class as abstract or un-instantiable even if all its abstract methods have been implemented? class Component(ABC): @abstractmethod def operation(self) -> None: pass class Decorator(Component): # I…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
7
votes
4 answers

Python : subclass `type` to create specialized types (e.g. a "list of int")

I am trying to subclass type in order to create a class allowing to build specialized types. e.g. a ListType : >>> ListOfInt = ListType(list, value_type=int) >>> issubclass(ListOfInt, list) True >>> issubclass(list, ListOfInt) False >>> # And so on…
sebpiq
  • 7,540
  • 9
  • 52
  • 69
7
votes
1 answer

PyCharm "must implement all abstract methods" on a subclass that's intentionally abstract

I have an abstract base class, Animal: class Animal(metaclass=abc.ABCMeta): @abc.abstractmethod def move(self): raise NotImplementedError() @abc.abstractmethod def eat(self): raise NotImplementedError() Now I have…
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
7
votes
1 answer

Python abc.abstractproperty compatibility

The Python 3 documentation mentions that abc.abstractproperty is deprecated since 3.3 in favour of @property and @abstractmethod. Is there an alternative way to implement an abstract property (without abc.abstractproperty) that is compatible with…
sfinkens
  • 1,210
  • 12
  • 15
7
votes
1 answer

Python Abstract Base Classes: Why doesn't abc prevent instantiation?

As far as I understood, the Python module abc should prevent instantiation of classes which have not all @abstractmethod marked methods of the base class implemented (provided that the base class has __metaclass__ = ABCMeta set) However, this seems…
Andreas Duering
  • 1,560
  • 2
  • 12
  • 21
7
votes
2 answers

Python abc module: Extending both an abstract base class and an exception-derived class leads to surprising behavior

Extending both an abstract base class and a class derived from "object" works as you would expect: if you you haven't implemented all abstract methods and properties, you get an error. Strangely, replacing the object-derived class with an class…
7
votes
1 answer

Abstract methods with specific arguments in Python

I implement abstract class with abc package. The program below shows no problems. Is there any way to make it fail because abstract MyMethod did have an argument a but the implementation of 'MyMethod' in class Derivative didn't? So I would like…
Konstantin
  • 2,937
  • 10
  • 41
  • 58
1 2
3
18 19