You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from pymodm import MongoModel
from pymodm.fields import CharField
class Thing(MongoModel):
name = CharField()
Pyright sees this as Unknown | Charfield. Pymodm isn't typed, so as an ORM I assume it's confusing Pyright with what it does under the hood. I tried to fix this with:
class Thing(MongoModel):
name: CharField = CharField()
This remains Unknown | Charfield and I'm not sure why. Shouldn't my explicit annotation definitively tell Pyright what name is?
VS Code extension or command-line
VS Code extension, Pylance 20.10.2
When you say that "Pyright sees this as Unknown | Charfield", what expression are you referring to? Is that what you see when you hover over the name symbol? Or is this coming from some other source such as an error message?
I'm not able to repro this with the latest version of Pylance or Pyright. Perhaps I'm using a different version of pymodm than you? I installed the latest version, which appears to be 0.4.3.
I've reconfirmed that this is happening as described in a fresh file. I'm running:
VS Code 1.50.1
Pylance 2020.10.2
Pymodm 0.4.3
Python 3.8.5 (virtualenv interpreter selected)
from pymodm import MongoModel
from pymodm.fields import CharField
class Thing(MongoModel):
name: CharField = CharField()
thing = Thing() # same message for Thing(name='abc')
thing.name
Pylance reports the issue under Problems, and when hovering thing.name.
Thanks for the additional information. I'm able to repro the problem now that I have the full code snippet.
First, I noticed that you have "strict" type checking enabled. It's difficult to use strict type checking in Python today because it requires that all imported packages have complete and accurate type information — or that you have found or created type stubs yourself that provide this type information. The 'pymodm' is not a typed library, and I presume that you don't have any type stubs for it. As such, I recommend that you switch from 'strict' to 'basic' typeCheckingMode. You can individually enable some of the other diagnostic checks that are normally off in basic mode, but practically speaking, you won't be able to enable them all.
As for the specific problem you reported, it's due to the fact that the CharField class has a __get__ method, so when you access the field name, this __get__ method is implicitly called. The __get__ method in this case does not have any type annotations, so pyright does its best to infer the return type. If you look at the implementation, it returns either self or a value that is dynamically fetched, and is therefore unknown. That's why the resulting type is Unknown | CharField.
Thank you for the quick, thorough, and helpful reply!
You're right about not having custom types for Pymodm. In fact, I encountered this problem while trying to write some initial stubs for it and move my org's ORM models away from # type: ignore. For both Pyright and Mypy (run in CI/CD), I've started from strict and am backing rules off when they're untenable.
This is certainly not correct, but it looks like I can create the following stubs as a rough draft:
# typings/pymodm/base/fields.pyi
from typing import Any, Generic, Optional, TypeVar
T = TypeVar("T")
class MongoBaseField(Generic[T]):
def __getattr__(self, name: str) -> Any:
... # incomplete
def __get__(self, inst: Optional[T], owner) -> T:
...
# typings/pymodm/fields.pyi
from typing import Any
from .base.fields import MongoBaseField
class CharField(MongoBaseField[CharField]):
...
After adding these, Pylance is throwing:
Type of parameter "owner" is unknown
Typeshed says that unannotated arguments are preferred over Any, however Pylance throws reportUnknownParameterType. This is technically correct but is it working as intended? Should Pylance skip that check for type-only .pyi files?
Pylance is also throwing reportUnknownMemberType for typings/pymodm/__init__.pyi with __gettattr__. Similar to the above, this is technically correct but failing when following typeshed's guidelines for partially typed modules.
Please let me know if you prefer a new issue for this.
owner is a parameter. Because you've specified that you want "strict" type checking, all parameters must have annotations.
PEP 484 indicates that parameters that have no annotation should be treated as though they are "Any". Pyright goes a step further and differentiates between an explicit "Any" and an implicit "Any". It refers to the latter as "Unknown". When in strict mode, Pyright emits errors if there are any Unknowns.
As I mentioned above, "strict" mode is difficult to achieve, especially in an existing code base. I recommend that you start with "basic" mode and then enable more diagnostic rules as you work your way up to "strict". Here is some documentation that you might find useful.
Activity
erictraut commentedon Oct 23, 2020
When you say that "Pyright sees this as
Unknown | Charfield", what expression are you referring to? Is that what you see when you hover over thenamesymbol? Or is this coming from some other source such as an error message?I'm not able to repro this with the latest version of Pylance or Pyright. Perhaps I'm using a different version of pymodm than you? I installed the latest version, which appears to be 0.4.3.
pikeas commentedon Oct 23, 2020
I've reconfirmed that this is happening as described in a fresh file. I'm running:
Pylance reports the issue under Problems, and when hovering
thing.name.Here are my Pylance-relevant VS Code settings:
erictraut commentedon Oct 23, 2020
Thanks for the additional information. I'm able to repro the problem now that I have the full code snippet.
First, I noticed that you have "strict" type checking enabled. It's difficult to use strict type checking in Python today because it requires that all imported packages have complete and accurate type information — or that you have found or created type stubs yourself that provide this type information. The 'pymodm' is not a typed library, and I presume that you don't have any type stubs for it. As such, I recommend that you switch from 'strict' to 'basic' typeCheckingMode. You can individually enable some of the other diagnostic checks that are normally off in basic mode, but practically speaking, you won't be able to enable them all.
As for the specific problem you reported, it's due to the fact that the
CharFieldclass has a__get__method, so when you access the fieldname, this__get__method is implicitly called. The__get__method in this case does not have any type annotations, so pyright does its best to infer the return type. If you look at the implementation, it returns eitherselfor a value that is dynamically fetched, and is therefore unknown. That's why the resulting type isUnknown | CharField.So Pyright is doing the right thing in this case.
pikeas commentedon Oct 23, 2020
Thank you for the quick, thorough, and helpful reply!
You're right about not having custom types for Pymodm. In fact, I encountered this problem while trying to write some initial stubs for it and move my org's ORM models away from
# type: ignore. For both Pyright and Mypy (run in CI/CD), I've started fromstrictand am backing rules off when they're untenable.This is certainly not correct, but it looks like I can create the following stubs as a rough draft:
After adding these, Pylance is throwing:
Typeshed says that unannotated arguments are preferred over Any, however Pylance throws
reportUnknownParameterType. This is technically correct but is it working as intended? Should Pylance skip that check for type-only .pyi files?Pylance is also throwing
reportUnknownMemberTypefortypings/pymodm/__init__.pyiwith__gettattr__. Similar to the above, this is technically correct but failing when following typeshed's guidelines for partially typed modules.Please let me know if you prefer a new issue for this.
erictraut commentedon Oct 23, 2020
owneris a parameter. Because you've specified that you want "strict" type checking, all parameters must have annotations.PEP 484 indicates that parameters that have no annotation should be treated as though they are "Any". Pyright goes a step further and differentiates between an explicit "Any" and an implicit "Any". It refers to the latter as "Unknown". When in strict mode, Pyright emits errors if there are any Unknowns.
As I mentioned above, "strict" mode is difficult to achieve, especially in an existing code base. I recommend that you start with "basic" mode and then enable more diagnostic rules as you work your way up to "strict". Here is some documentation that you might find useful.
Disable general type issues as we could only use typed libraries othe…
Disable general type issues as we could only use typed libraries othe…
Use newest version of pyright, ignored a lot of errors, identified so…