OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to make Pylance and Pydantic understand each other when instantiating BaseModel class from external data?

  • Thread starter Thread starter Vlad Ankudinov
  • Start date Start date
V

Vlad Ankudinov

Guest
I am trying to instantiate user = User(**external_data), where User is Pydantic BaseModel, but I am getting error from Pylance, which don't like my external_data dictionary and unable to figure out that data in the dict is actually correct (see first screenshot).

Type error

I found a workaround by creating TypedDict with the same declaration as for User(BaseModel). Now Pylance is happy, but I am not, because I need to repeat myself (see second screenshot).

Repetition

Any ideas on how to make Pylance and Pydantic understand each other without repetition?

Code:
from datetime import datetime
from pydantic import BaseModel
from typing import TypedDict

class UserDict(TypedDict, total=False):
    id: int
    name: str
    signup_ts: datetime
    friends: list[int]


class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: datetime | None = None
    friends: list[int] = []


external_data: UserDict = {
    'id': 123,
    'name': 'Vlad',
    'signup_ts': datetime.now(),
    'friends': [1, 2, 3],
}

user = User(**external_data)
print(user)
print(user.id)

Pylance error for the case with no UserDict:

Code:
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "id" of type "int" in function "__init__"
  Type "int | str | datetime | list[int]" cannot be assigned to type "int"
    "datetime" is incompatible with "int"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "name" of type "str" in function "__init__"
  Type "int | str | datetime | list[int]" cannot be assigned to type "str"
    "datetime" is incompatible with "str"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "signup_ts" of type "datetime | None" in function "__init__"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "friends" of type "list[int]" in function "__init__"
  Type "int | str | datetime | list[int]" cannot be assigned to type "list[int]"
    "datetime" is incompatible with "list[int]"PylancereportGeneralTypeIssues
(variable) external_data: dict[str, int | str | datetime | list[int]]
<p>I am trying to instantiate <code>user = User(**external_data)</code>, where <code>User</code> is Pydantic <code>BaseModel</code>, but I am getting error from Pylance, which don't like my <code>external_data</code> dictionary and unable to figure out that data in the dict is actually correct (see first screenshot).</p>
<p><a href="https://i.sstatic.net/kd0wE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kd0wE.png" alt="Type error" /></a></p>
<p>I found a workaround by creating <code>TypedDict</code> with the same declaration as for <code>User(BaseModel)</code>. Now Pylance is happy, but I am not, because I need to repeat myself (see second screenshot).</p>
<p><a href="https://i.sstatic.net/0nbHi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0nbHi.png" alt="Repetition" /></a></p>
<p>Any ideas on how to make Pylance and Pydantic understand each other without repetition?</p>
<pre class="lang-py prettyprint-override"><code>from datetime import datetime
from pydantic import BaseModel
from typing import TypedDict

class UserDict(TypedDict, total=False):
id: int
name: str
signup_ts: datetime
friends: list[int]


class User(BaseModel):
id: int
name: str = "John Doe"
signup_ts: datetime | None = None
friends: list[int] = []


external_data: UserDict = {
'id': 123,
'name': 'Vlad',
'signup_ts': datetime.now(),
'friends': [1, 2, 3],
}

user = User(**external_data)
print(user)
print(user.id)
</code></pre>
<p>Pylance error for the case with no <code>UserDict</code>:</p>
<pre><code>Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "id" of type "int" in function "__init__"
Type "int | str | datetime | list[int]" cannot be assigned to type "int"
"datetime" is incompatible with "int"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "name" of type "str" in function "__init__"
Type "int | str | datetime | list[int]" cannot be assigned to type "str"
"datetime" is incompatible with "str"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "signup_ts" of type "datetime | None" in function "__init__"PylancereportGeneralTypeIssues
Argument of type "int | str | datetime | list[int]" cannot be assigned to parameter "friends" of type "list[int]" in function "__init__"
Type "int | str | datetime | list[int]" cannot be assigned to type "list[int]"
"datetime" is incompatible with "list[int]"PylancereportGeneralTypeIssues
(variable) external_data: dict[str, int | str | datetime | list[int]]
</code></pre>
 

Latest posts

Top