How to use with ABC / ABCMeta
See original GitHub issueI was trying to define a pydantic class with abstract methods, which was harder than I thought due to metaclass conflicts. I finally managed to do it like this:
from abc import ABCMeta, abstractmethod
from pydantic import BaseModel
from pydantic.main import MetaModel
class PydanticWithABCMeta(ABCMeta, MetaModel):
pass
class MyPydanticClass(BaseModel, metaclass=PydanticWithABCMeta):
some_field: str
@abstractmethod
def my_abstract_method(self):
pass
Not sure it’s the best approach, but if it is, would be worth documenting it somewhere?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
abc — Abstract Base Classes — Python 3.11.1 documentation
A decorator indicating abstract methods. Using this decorator requires that the class's metaclass is ABCMeta or is derived from it. A class that...
Read more >Abstract Base Class (abc) in Python - GeeksforGeeks
ABCMeta metaclass provides a method called register method that can be invoked by its instance. By using this register method, any abstract base ......
Read more >How to Write Cleaner Python Code Using Abstract Classes
To define an abstract method in the abstract class, we have to use a decorator: @abstractmethod . The built-in abc module contains both...
Read more >What is the difference between abstractclass(metaclass ...
A helper class that has ABCMeta as its metaclass. ... be created by simply deriving from ABC avoiding sometimes confusing metaclass usage.
Read more >18. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
thanks for your work on this.
Addressed in #123