Search code examples
pythonfastapipython-datetime

RuntimeError: error checking inheritance of module 'datetime'


I am getting following error when trying to run my python app:

RuntimeError: error checking inheritance of <module 'datetime' from '/usr/local/lib/python3.9/datetime.py'> (type: module)

This is my code:

import datetime
from pydantic.types import Optional
from sqlmodel import SQLModel, Field

class BlogBase(SQLModel): 
    title: str
    published_at: datetime
    # author:str = Field(default=None, foreign_key="author.id")
    body: str
    updated_at: Optional[str]

class Blog(BlogBase, table=True): 
    id: int = Field(default=True, primary_key=True)
    published_at: datetime = Field(default=datetime.utcnow(), nullable=False)


class BlogCreate(BlogBase): 
    pass

Can someone help me in understanding the problem and how I can fix it?


Solution

  • The datetime module supplies classes/objects for manipulating dates. One of them is datetime.datetime. The datetime class also includes other class methods such as utcnow(), which you seem to be using in the example you provided.

    Hence, you should instead use, for instance:

    from datetime import datetime
    
    now = datetime.utcnow()
    

    Note how the datetime should be imported, that is:

    from datetime import datetime
    

    datetime is basically a class inside a module that is also named datetime.

    or, you could also do:

    import datetime
    
    now = datetime.datetime.utcnow()