'"Expected type 'Type', got 'Type[Type]' instead"
So I'm pretty new to python, and 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*
.
This is kinda confusing to explain, but I would appreciate some help.
*I am required to complete the assignment using this format, and am only asking about the syntax, not how to format it
Solution 1:[1]
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).
Solution 2:[2]
If your formatter understands sphinx type docstrings (pep257), then if you have code like this:
newTable = Table(TableEntry)
Then you'll need docstrings like
class TableEntry:
def __init__(self):
pass
class Table:
def __init__(self, table_entry):
"""Descrption.
:param table_entry:
:type table_entry: type[TableEntry]
"""
pass
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Roman |