271 questions
Score of 2
1 answer
102 views
Python abstract methods for Children, but such that don't prohibit instances of the Base?
I'm currently using ABC's to ensure that my child classes implement a specific method (or property, in this particular case). I want to make it impossible to create children of Entity without ...
Score of 1
1 answer
141 views
`NotImplementedError` gets triggered unexpectedly when tring to define an "abstract class property"
This question is related to a question about the implementation of class property in Python which I have asked yesterday. I received a working solution and wrote my classProp and ClassPropMeta as the ...
Score of 0
1 answer
79 views
PyCharm typing warning inconsistency for abstract base classes
In the following code, PyCharm is issuing a typing warning for Base.foo ("Expected to return 'int', got no return") but not for Base.bar, which has exactly the same typing hint and return ...
Score of 0
1 answer
115 views
How to define the method signature in a subclass
I'm implementing a repository pattern as an excercise on typing.
I have a couple of unrelated SQLAlchemy models:
class Base(MappedAsDataclass, DeclarativeBase):
id: Mapped[primary_key] = ...
Score of 0
0 answers
161 views
Raising NotImplementedError in abstract methods
While studying the source code of the numbers.py module from build-in lib, I came across two variants of @abstractmethod (with and without the NotImplementedError raise). Example:
class Complex(ABC):
...
Score of 0
1 answer
84 views
Checking subclass against metaclass type
I have a set of plugins that inherit from a metaclass. the metaclasss is defined like so:
from abc import ABC, abstractmethod
class MetaReader(ABC):
def __init__(self, arg1, arg2, **kwargs):
...
Score of 0
0 answers
22 views
Is it possible to read metadata from a python model before creating an instance? [duplicate]
I have python models which are created using a particular version of my module repository. As updates and changes are made to the repo, the models are not always backward compatible.
I'm wondering if ...
Score of 1
1 answer
88 views
If Python builtins derive from ABCs, then why is their metaclass type instead of ABCMeta?
I was reading PEP-3119, and I discovered that builtins derive from ABCs.
From PEP-3119:
The built-in type set derives from MutableSet. The built-in type frozenset derives from Set and Hashable.
In ...
Score of 1
1 answer
141 views
Confusion about virtual subclasses in static type checking like mypy
I'm trying to figure out in python whether virtual subclass will pass the static type check like mypy. And I read about this in fluent python:
Virtual subclasses do not inherit from their registered ...
Score of 0
1 answer
141 views
How to correctly inherit from an abc interface and another class preserving restriction to create objects missing implementation?
I'm trying to combine a class implementing an abc interface with another class.
Let's start with abc simple example:
import abc
class Interface(abc.ABC):
@abc.abstractmethod
def ...
Score of 0
1 answer
205 views
Coverage expects a test case for an @abstractmethod
As you can see in the picture below, coverage wants me to write a test case for the method inside the abc.
But I dont know how to do it, I mean as far as I can see there is nothing to test.
Code:
...
Score of 0
0 answers
140 views
Python class attributes marked read-only when inheriting from dataclass in IntelliJ
I have a regular class inheriting from a frozen dataclass.
The attributes in my regular class are marked as 'read-only'. Which I understand due to inheriting the characteristics of the frozen ...
Score of -1
1 answer
405 views
getting module 'abc' has no attribute 'update_abstractmethods' error when installing/uninstalling a package
starting from a few days ago every time I try downloading any packages to my environment (or pip installing directly through the terminal) and/or delete/uninstall an existing package in my envirinemt, ...
Score of 0
0 answers
109 views
No TypeError raised for missing abc.abstractmethod when deriving a PySide6 widget
I have an object that inherits both from a QWidget and an abc.ABC interface. See MyWindow in my example below.
import abc
from PySide6 import QtCore, QtWidgets
#from PyQt6 import QtCore, QtWidgets #...
Score of 1
1 answer
1762 views
Python, __init__ method of abstract class implicitly being called?
I'm not too familiar with the use of abstract classes, so I'm trying to understand what is happening in some code I'm using at the moment.
In the code, I have a base dataset class, and some ...