Questions tagged [abc]

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

280 questions
2
votes
0 answers

Flask-Login current_user changing to the most recent User class query result

My implementation of Flask-Login is working well across my site, with the exception of contexts where I need to make additional queries on the User class. For example, when I try to generate a list of User instances representing other users with…
2
votes
1 answer

Abstract method allowing for additional arguments

Is it possible to have an abstract class require some specific arguments for a method of a Python class, while still leaving the possibility for concrete classes to add more arguments? from abc import ABC, abstractmethod class FooBase(ABC): …
Jivan
  • 21,522
  • 15
  • 80
  • 131
2
votes
2 answers

Use __init_subclass__ to patch abstract methods while preserving the abc check

I would like to wrap an abstractmethod for all subclasses of an abc. I tried doing that by implementing __init_subclass__ as below: import abc class Base(abc.ABC): @abc.abstractmethod def foo(self) -> str: pass def…
tepsijash
  • 370
  • 2
  • 18
2
votes
1 answer

How to create an abstract attribute for an abstract class in Python?

I already found some similar question where they try to create abstract attributes and they all suggest using @property and @abc.abstractmethod to achieve that like here. There are many similar topics but I cant find a way to do it without using the…
KZiovas
  • 3,491
  • 3
  • 26
  • 47
2
votes
1 answer

Is it OK to define optional methods on abstract base class?

Is it a good practice to have non-abstract methods on abstract base classes? I mean, methods that can, but don't have to, be present on subclasses of particular ABC? Technically it is possible, as seen in the below example (ConcreteDataProvider does…
barciewicz
  • 3,511
  • 6
  • 32
  • 72
2
votes
1 answer

`__subclasshook__` on inherited abstract classes in Python?

I'm wondering how best I can define __subclasshook__ on a hierarchy of abstract classes in Python. Let's say I have an interface, and an ABC that inherits from that interface. Is something like this okay? import abc from typing import IO class…
piglet
  • 83
  • 1
  • 6
2
votes
1 answer

How to implement MutableSequence on custom class

I want to use a class like a list. I found out that inheriting and implementing collections.abc.MutableSequence makes the class list-like, but I don't know how to overload methods such as __getitem__, __setitem__ for integer and slice…
usan
  • 81
  • 1
  • 8
2
votes
1 answer

In python, Can an abstract class be instantiated?

from abc import ABC, abstractmethod class Example(ABC): def smile(self): print("smile") My understanding is that Example becomes an AbstractClass when it inherits the ABC class. An AbstractClass can't be instantiated but the following…
Kun.tito
  • 165
  • 1
  • 7
2
votes
1 answer

How to implement a constructor for an abstract base class?

I am trying to write a base abstract class that has some properties that will be initialized using a constructor. So far I have this: from abc import ABC, abstractmethod class A(ABC): def __init__(self, n, *params): self.n = n …
Farhood ET
  • 1,432
  • 15
  • 32
2
votes
3 answers

ImportError: cannot import name 'my_function' from 'abc'

I'm trying to load python script from computer to google colab but is show me importError
Sandeep Agrawal
  • 175
  • 1
  • 8
2
votes
0 answers

How do I tell pytype that an instance is a subclass of an abstract base, but not the abstract base itself?

The issue arises when code instantiates an instance of the supertype. Here's an example. T = TypeVar("T") class A(Generic[T], metaclass=abc.ABCMeta): @abc.abstractmethod def hello(self, arg: T): raise NotImplementedError class…
Carson McNeil
  • 790
  • 9
  • 22
2
votes
1 answer

Is there some mechanism to enforce nested interfaces are implemented in abstract classes in Python?

I want to create a class that has some nested class that defines some contract in Python. A tenable example is a typed config object. My attempt at this is below: from typing import Mapping from abc import ABCMeta, abstractmethod class…
erip
  • 16,374
  • 11
  • 66
  • 121
2
votes
0 answers

Python abstract base classes and multiple inheritance

I am trying to create a python (3.5) class structure where I use abstract methods to indicate which methods should be implemented. The following works as expected: from abc import ABC, abstractmethod class Base(ABC): @abstractmethod def…
Aaron
  • 321
  • 4
  • 9
2
votes
0 answers

Register subclass to an ABC class inside __init_subclass__ does not fully work

What I want to achieve is to register one type as subtype of all other types. For some other reason I cannot use metaclass, so __init_subclass__ seems like a reasonable choice. I have code like this from abc import ABC class AnyData(ABC): …
damantou
  • 53
  • 6
2
votes
0 answers

Python abstractmethod property - enforce concrete decorator correctly

How can you ensure a concrete class will correctly use @property decorator when overriding an @abstractmethod? Consider the following abstract class: class Vehicle(ABC): def __init__(self): print(self.gas * 2) @property …
Voy
  • 5,286
  • 1
  • 49
  • 59