Skip to content
Advertisement

Tag: abc

Detect incomplete subclass of abstract class, python

In Python, how can I differentiate between a concrete subclass and a subclass which is still abstract (i.e. not all abstract methods have been implemented)? Consider the following: What is the implementation of is_concrete_class? I could attempt to instantiate each subclass given by __subclasses__() and catch TypeError: Can’t instantiate abstract class <class_name> with abstract methods <…>, but TypeError seems too

Distinguishing between Pydantic Models with same fields

I’m using Pydantic to define hierarchical data in which there are models with identical attributes. However, when I save and load these models, Pydantic can no longer distinguish which model was used and picks the first one in the field type annotation. I understand that this is expected behavior based on the documentation. However, the class type information is important

Abstract dataclass without abstract methods in Python: prohibit instantiation

Even if a class is inherited from ABC, it can still be instantiated unless it contains abstract methods. Having the code below, what is the best way to prevent an Identifier object from being created: Identifier([‘get’, ‘Name’])? Answer You can create a AbstractDataclass class which guarantees this behaviour, and you can use this every time you have a situation like

Advertisement