Skip to content

Mypy error: Variable "Email" is not valid as a type mypy(error) #50

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

Closed
chespinoza opened this issue Nov 6, 2019 · 6 comments
Closed

Comments

@chespinoza
Copy link

Taking the example from the readme:

import marshmallow
from marshmallow_dataclass import NewType

Email = NewType("Email", str, field=marshmallow.fields.Email)

When using Email in a schema, for instance:

@marshmallow_dataclass.dataclass
class MySchema:
    id: str = field(metadata={"validate": marshmallow.validate.uuid})
    email: Email
    Schema: ClassVar[Type[marshmallow.Schema]] = marshmallow.Schema

I'm getting this error from mypy:

Mypy error: Variable "Email" is not valid as a type mypy(error)

versions:

marshmallow==3.2.1
marshmallow-dataclass==7.0.0

Thanks in advance.

@lovasoa
Copy link
Owner

lovasoa commented Nov 7, 2019

Correct me if I'm wrong, but don't think this is related to marshmallow_dataclass. mypy cannot handle any dynamically created types.

Your code can be reduced to the following, that mypy also rejects:

from typing import Type

Email: Type[str] = str

class MySchema:
    email: Email

This also gives: error: Variable "Email" is not valid as a type

@lovasoa
Copy link
Owner

lovasoa commented Nov 8, 2019

I opened an issue at mypy, and they do not seem to be keen on implementing anything that would allow us make NewType work with mypy:
python/mypy#7901

I am going to leave this issue open, and if anyone is interested in writing a mypy plugin, we'll track progress here.

@tomdottom
Copy link

tomdottom commented Nov 19, 2019

Correct me if I'm wrong, but don't think this is related to marshmallow_dataclass. mypy cannot handle any dynamically created types.

Your code can be reduced to the following, that mypy also rejects:

from typing import Type

Email: Type[str] = str

class MySchema:
    email: Email

This also gives: error: Variable "Email" is not valid as a type

mypy is able to recognize the following

from typing import NewType

Email = NewType("Email", str)

class MySchema:
    email: Email

Instead of returning a function maybe marshmallow_dataclass.NewType could do one of the following:

A) Return a typing.NewType and internally map this to construction functions which can be looked as needed.

B) Construct and return a new Field Class.

@lovasoa
Copy link
Owner

lovasoa commented Nov 19, 2019

A) Return a typing.NewType and internally map this to construction functions which can be looked as needed.

This is not possible. The logic for typing.NewType is hardcoded in mypy and does not support dynamic arguments. See the mypy issue I linked to above.

B) Construct and return a new Field Class.

This wouldn't work with the rest of marshmallow-dataclass. In particular, this wouldn't allow:

class MySchema:
    email: Email

@lovasoa
Copy link
Owner

lovasoa commented Nov 19, 2019

@tomdottom : Would you be interested in writing a mypy plugin ?

@selimb selimb mentioned this issue Dec 8, 2019
1 task
@selimb
Copy link
Contributor

selimb commented Dec 9, 2019

I'd like for this to get fixed as well.
Wrote mypy plugin in #55

@sloria sloria closed this as completed Dec 11, 2019
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

5 participants