Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Type[xxx]" has no attribute "from_json" #23

Closed
gjcarneiro opened this issue Oct 19, 2018 · 2 comments
Closed

"Type[xxx]" has no attribute "from_json" #23

gjcarneiro opened this issue Oct 19, 2018 · 2 comments

Comments

@gjcarneiro
Copy link

One of the downsides of @dataclass_json is that mypy doesn't know about the extra from_json and to_json methods. Any chance this can be supported, somehow?

The docs seem to suggest there is a plugin system in mypy, albeit experimental: https://mypy.readthedocs.io/en/latest/extending_mypy.html#extending-mypy-using-plugins

@dataclass_json
@dataclass
class BulkApmm:
    id: str
    frequency_ms: int
    pmms: List[Apmm]

...

    bulk_apmm = BulkApmm.from_json(await request.text())

betslipdisp/views.py:70: error: "Type[BulkApmm]" has no attribute "from_json"
@lidatong
Copy link
Owner

lidatong commented Oct 28, 2018

Hi, yes: I'd like this package to have full support for typing in the future. The "official" way to do that would be to follow PEP 561 or provide a stub (.pyi file) containing type information, so there definitely is a way. It does require introducing typing to all packages and sub-packages, so it's a fair amount of work.

Without the above, mypy treats the package as "untyped" and will output a message along the lines of Cannot find module dataclasses_json. Basically, mypy treats a package as missing if there are no associated typing stub files.

But this is somewhat tangential to your code example / use case:
In your example, you're using the dataclass_json class decorator approach rather than the the mixin, which is absolutely fine (I personally find stacking decorators syntactically nicer), except mypy currently has particularly weak support for decorators (see this issue). So even with a typing stub, it won't be able to typecheck using this library's dataclass_json class decorator.

Something like the following should be valid:

from typing import Type, TypeVar

A = TypeVar('A')
DC = TypeVar('DC', bound=DataClassJsonMixin)

def dataclass_json(f: Type[A]) -> Type[DC]:
    ...

The gist of the above is dataclass_json's type signature says "it's a function that takes in a type and returns a type that implements DataClassJsonMixin" (i.e. a class decorator). And the type variable DC captures the concrete implementation of the DataClassJsonMixin (BulkApmm in your example).

But mypy actually still won't recognize .to_json() as a method, even with the above. It'll only type-check if you inherit the mixin directly.

Indeed, the state of typing in Python is not yet where I wish it were.

@lidatong
Copy link
Owner

lidatong commented Nov 4, 2018

Tracking in parent issue #31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants