Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Integration with mypy #119

Open
radix opened this issue Nov 23, 2019 · 1 comment
Open

Integration with mypy #119

radix opened this issue Nov 23, 2019 · 1 comment
Labels
feature New feature or request

Comments

@radix
Copy link

radix commented Nov 23, 2019

I'm not sure if the serde field type annotations are compatible with MyPy. For example, with this code:

from serde import Model, fields
class Artist(Model):
    name: fields.Str()

Running mypy on the code gives the following result (in addition to an error about the fact that serde doesn't have a type definition module):

serdetest.py:7: error: Invalid type comment or annotation
serdetest.py:7: note: Suggestion: use fields.Str[...] instead of fields.Str(...)

So, on the surface, it seems that this library is incompatible with MyPy. Is this true, or is there a way to get them to work together?

@rossmacarthur rossmacarthur added the feature New feature or request label Nov 24, 2019
@rossmacarthur rossmacarthur changed the title Can this library be used with MyPy? Support Python >=3.5 type annotations Nov 24, 2019
@rossmacarthur
Copy link
Owner

Hi @radix at the moment typing annotations are not supported. However, there is support for plain types which is supported by MyPy. Any of these types will be automatically converted to serde field objects.

import datetime
import uuid
from serde import Model
class Artist(Model):
    id: uuid.UUID
    name: str
    birthday: datetime.date

See the following for a list of types that are supported like this:

serde/src/serde/fields.py

Lines 1308 to 1331 in ad6d24b

FIELD_CLASS_MAP = {
# Built-in types
bool: Bool,
bytes: Bytes,
complex: Complex,
dict: Dict,
float: Float,
int: Int,
list: List,
set: Set,
str: Str,
tuple: Tuple,
# Collections
collections.OrderedDict: OrderedDict,
# Datetimes
datetime.datetime: DateTime,
datetime.date: Date,
datetime.time: Time,
# Others
uuid.UUID: Uuid,
}

In the future I want to make this library automatically convert typing objects to serde field objects. So that you could do something like:

from serde import Model
from typing import List, Tuple

class Example(Model):
    things: List[Tuple[str, int, str]]

@rossmacarthur rossmacarthur changed the title Support Python >=3.5 type annotations Support Python >=3.5 typing module / integrate with mypy Nov 25, 2019
@rossmacarthur rossmacarthur changed the title Support Python >=3.5 typing module / integrate with mypy Integration with mypy Feb 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants