question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use with ABC / ABCMeta

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelcolvincommented, Feb 6, 2018

thanks for your work on this.

0reactions
jcugatcommented, Feb 6, 2018

Addressed in #123

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found