dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

Pylance says Cannot access member "schema" for type "Type[...]"

Open davetapley opened this issue 4 years ago • 3 comments
trafficstars

I have this snippet in VSCode with Python 3.9.6 and Pylance (v2021.8.1105858891):

@dataclass_json
@dataclass
class Folder:
    Id: int
    Name: str

# mode code

        return Folder.schema().load(res.json()['Folders'], many=True)

And Pylance is underlining schema in red, saying:

Cannot access member "schema" for type "Type[Folder]"
  Member "schema" is unknownPylancereportGeneralTypeIssues

I'm opening here instead of against Pylance to see if others are seeing this, and just in case something it's something with ⬇️ which is confusing/breaking Pylance 🤔

https://github.com/lidatong/dataclasses-json/blob/3dc59e01ccdfec619ee4e4c3502b9759b67c3fa8/dataclasses_json/api.py#L89

davetapley avatar Aug 19 '21 20:08 davetapley

It appears to me that pylance is struggling with certain uses methods on @dataclass classes.

For instance, in some circumstances:

from my_module import MyBaseClass

@dataclass
class` Foo(MyBaseClass):
    a: int
    def foo(self):
        print("...")

def bar() -> Foo:
    return Foo(a=0)

bar().foo()   # <-- pylance error

pylance will flag foo() call, "Cannot access member "foo" for type "Dataclass"

I haven't had time to verify if this is related to

  • the type hints (error mentions Dataclass NOT the udt),
  • or the MRO or other inheritence issue (if I remove the inheritance from MyBaseClass, pylance accepts the code)
    • note, perhaps NOT similar to @davetapley 's reproduction, as @dataclass_json is not altering MRO but rather just adds cls attrs

bentheadset avatar Jan 31 '22 03:01 bentheadset

this is probably a general problem of visibility of the class decorator to static type checkers -- i've begun looking into various solutions for this

lidatong avatar Jan 31 '22 03:01 lidatong

Should be fixed as a result of #442 work - with a breaking change most likely

george-zubrienko avatar Jul 20 '23 22:07 george-zubrienko