Questions tagged [abc]

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

280 questions
7
votes
2 answers

Find all the abstract base classes that a class is registered with

How can I find all the abstract base classes that a given class is a "virtual subclass" of? In other words, I'm looking for a magic function virtual_base_classes() that does something like this: >>> for cls in virtual_base_classes(list): >>> …
max
  • 49,282
  • 56
  • 208
  • 355
6
votes
2 answers

ABC for String?

I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldn't find an ABC for strings. There is UserString, but UserDict was…
Gere
  • 12,075
  • 18
  • 62
  • 94
6
votes
1 answer

How to create an abstract cached property in Python?

In order to create an abstract property in Python one can use the following code: from abc import ABC, abstractmethod class AbstractClassName(ABC): @cached_property @abstractmethod def property_name(self) -> str: pass class…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
6
votes
1 answer

NamedTuple Class with ABC mixin

My problem is as follows: I want to create a class that inherits from typing.NamedTuple and another mixin which in an abstract class. Ideally I want to do something like this: from typing import * from abc import ABC, abstractmethod class M(ABC): …
hchw
  • 1,388
  • 8
  • 14
6
votes
1 answer

How to use abc abstract base class as mock spec?

I have an abstract base class: import abc import six @six.add_metaclass(abc.ABCMeta) class A(object): @abc.abstractmethod def f(self, arg1): pass I'd like to use this class as a spec for a mock. import mock mock_a =…
Cirdec
  • 24,019
  • 2
  • 50
  • 100
6
votes
1 answer

Python multiple inheritance of __new__ and __init__ with a string and second class

I'm trying to create a derived class that inherits from both a str type and a second class. It's problematic since the str type doesn't simply call __init__, but the __new__ method due to its immutability. I know that for __init__ and super to work…
Hooked
  • 84,485
  • 43
  • 192
  • 261
6
votes
1 answer

Actual difference in implementing/overriding using @abstractproperty and @abstractmethod

Consider an abstract base class with a function which you want each subsequent subclass to override. Using the abc module and ABCMeta; does decorating with @abstractproperty or @abstractmethod actually force the subclass/developer implementing to…
Parham
  • 3,157
  • 4
  • 31
  • 44
5
votes
1 answer

Solving inheritance contradictions among abc.Sequence, abc.Hashable and list in Python

I'm using version 3.6.3 I'm studying Python collections.abc's inheritance relationships among classes. And I found some contradictory inheritances among list, Sequence and Hashable As you already know, 1. Sequence inherits Hashable class and 2. list…
Stonehead
  • 81
  • 5
5
votes
1 answer

mypy issues with abstract classes and dictionaries

Greatings, consider the following code. from abc import ABC, abstractmethod class Interface(ABC): @abstractmethod def method(self) -> None: pass class A(Interface): def method(self) -> None: pass class…
Daniel Severo
  • 1,768
  • 2
  • 15
  • 22
5
votes
2 answers

Python abstract setters and getters

I want to write abstract class that will force inheriting classes to implement all methods AND properties in my abstract class. Additionally I want to use of setters and getters for my abstract property to make my code uncluttered and looking…
Jakub Pastuszuk
  • 928
  • 4
  • 12
  • 31
5
votes
1 answer

Documenting attributes of abstract base classes?

I have an abstract base class, and every descendant is expected to have certain attributes (of type str, int, etc), hence it makes sense to document those attributes in the base class. What is the recommended format for documenting these attributes…
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
5
votes
3 answers

Delegation design pattern with abstract methods in python

I have the following classes implementing a "Delegation Design Pattern" with an additional DelegatorParent class: class DelegatorParent(): def __init__(self): self.a = 'whatever' class ConcreteDelegatee(): def myMethod(self): …
caspillaga
  • 573
  • 4
  • 16
5
votes
2 answers

Is it possible to be a virtual subclass of a built in type?

Is it possible to make a user defined type be a virtual subclass of a built in type in python? I would like my class to be considered a subclass of int, however I don't want to inherit directly like this: class MyInt(int): '''Do some stuff kind…
eestrada
  • 1,575
  • 14
  • 24
5
votes
1 answer

Abstract method inheritance in Python

Let's assume that we have a Python class that makes use of the abc module to define an abstract attribute: import abc class A(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def test_attribute(self): raise…
Andrea
  • 3,627
  • 4
  • 24
  • 36
5
votes
1 answer

Python collections.MappingView

I was checking out the very nice collections library and more specific the Abstract Base Classes (ABC). One I could not get my head around: the MappingView. What is its use? What is its advantage over Sized? An example perhaps? Documentation says…
Lense
  • 61
  • 4