Describe the bug
__Incorrect__ Unnecessary isinstance call when working with Union containing mongoengine Document.
To Reproduce
Steps to reproduce the behavior.
Small code snippet below to reproduce the bug.
import random
from typing import Union
from mongoengine import Document
from mongoengine import StringField
class DbModel(Document):
bar = StringField()
def foo() -> Union[int, DbModel]:
p = random.random() < 0.5
return DbModel() if p else 42
isinstance(foo(), int)
error report:
Unnecessary isinstance call; "int | DbModel" is always instance of "int"Pyright (reportUnnecessaryIsInstance)
Expected behavior
isinstance is not unnecessary. I believe that this bug comes from the missing stub files in __mongoengine__. However, it shouldn't be reported as unnecessary.
Screenshots or Code
code snippet above
Additional context
Platform:
Pyright settings in settings.json:
{
...
"pyright.useLibraryCodeForTypes": true,
"pyright.typeCheckingMode": "strict",
...
}
Thanks for the report.
A couple of follow-up questions for you:
I think what's happening here is that the type for Document is unknown, and the logic for determining whether isinstance is unnecessary relies on having complete types. Normally, reportUnneccessaryIsInstance is used in conjunction with "strict" mode where all types are known.
Thank you for reaching out so quickly!
Stub file not found for "mongoengine")Document, it displays the type properly I guess:class Document
The base class used for defining the structure and properties of collections of documents stored in MongoDB.... and so on
I guess, that the lack of a stub file is causing the problem here.
Thanks for the bug report. This will be fixed in the next version of pyright.
This is now fixed in Pyright 1.1.40, which I just published.