question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lovasoacommented, 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: https://github.com/python/mypy/issues/7901

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

1reaction
lovasoacommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

mypy "is not valid as a type" for types constructed with `type()`
mypy complains error: Variable "packagename.
Read more >
dynamic base class "error: Variable ... is not valid as a type ...
dynamic base class "error: Variable ... is not valid as a type" when it is the parameter to a function and typed as...
Read more >
Common issues and solutions - mypy 0.991 documentation
There are several common reasons why obviously wrong code is not flagged as an error. The function containing the error is not annotated....
Read more >
python/typing - Gitter
Hi all, I'm trying to use a mypy plugin to essentially remove an attribute from a class such that when that attribute is...
Read more >
Mypy Documentation - Read the Docs
Python interpreter to run your code, even if mypy reports errors. ... mypy will use the provided type hints to detect incorrect use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found