Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Expected type 'Type', got 'Type[Type]' instead"

Tags:

python

My class has a project where we have to build a database. I keep running into this error where python asks expected a type of the same kind that I gave it (see image )

It says Expected type 'TableEntry', got 'Type[TableEntry]' instead

TableEntry is a dataclass instance (as per my assignment). I am only calling it for it's creation newTable = Table(id, TableEntry)
Where Table is another dataclass with an id and data with type TableEntry*.

*I am required to complete the assignment using this format, and am only asking about the syntax, not how to format it

like image 678
daxti Avatar asked Mar 27 '26 19:03

daxti


2 Answers

When you see an error like this:

Expected type 'TableEntry', got 'Type[TableEntry]' instead

it generally means that in the body of your code you said TableEntry (the name of the type) rather than TableEntry() (an expression that constructs an actual object of that type).

like image 199
Samwise Avatar answered Mar 29 '26 08:03

Samwise


The problem occurs because you are passing in a Type (TableEntry) when an Instance (TableEntry()) of the class is expected. Notice the trailing parenthesis in the latter one.

I've reproduced the error with a basic demo:

from dataclasses import dataclass


@dataclass
class TableEntry:
    name: str
    value: int


@dataclass
class Table:
    id: int
    TableEntry: TableEntry


id = 3
newTable = Table(id, TableEntry)

Here, you were passing the Type TableEntry; not an instance of TableEntry.

To solve the issue:

1- You can either create the TableEntry instance in place:

newTable = Table(id, TableEntry('daxti', 23))

or

2- You can create an instance and then pass it into the Table object:

newTableEntry = TableEntry("daxti", 23)
newTable = Table(id, newTableEntry)
like image 27
Susamate Avatar answered Mar 29 '26 07:03

Susamate