From c4c301a491eb04cbd31409daafba1b025bdeb0fb Mon Sep 17 00:00:00 2001 From: Kazantcev Andrey <45011689+heckad@users.noreply.github.com> Date: Mon, 20 Apr 2020 14:50:07 +0300 Subject: [PATCH 1/5] Improve typehint --- marshmallow_dataclass/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index 67e7d77..8f9c775 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -62,6 +62,8 @@ class User: NoneType = type(None) _U = TypeVar("_U") +T_SchemaType = TypeVar('T_SchemaType', bound=Type[marshmallow.Schema]) + # Whitelist of dataclass members that will be copied to generated schema. MEMBERS_WHITELIST: Set[str] = {"Meta"} @@ -162,8 +164,8 @@ def decorator(clazz: Type[_U]) -> Type[_U]: def class_schema( - clazz: type, base_schema: Optional[Type[marshmallow.Schema]] = None -) -> Type[marshmallow.Schema]: + clazz: type, base_schema: Optional[T_SchemaType] = None +) -> T_SchemaType: """ Convert a class to a marshmallow schema From d66aced5389feecd4d891c4055241ba3b9210550 Mon Sep 17 00:00:00 2001 From: Kazantcev Andrey <45011689+heckad@users.noreply.github.com> Date: Mon, 20 Apr 2020 14:58:56 +0300 Subject: [PATCH 2/5] More improves --- marshmallow_dataclass/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index 8f9c775..bd17bec 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -163,9 +163,17 @@ def decorator(clazz: Type[_U]) -> Type[_U]: return decorator(_cls) if _cls else decorator -def class_schema( - clazz: type, base_schema: Optional[T_SchemaType] = None -) -> T_SchemaType: +@typing.overload +def class_schema(clazz: type, base_schema: None) -> Type[marshmallow.Schema]: + ... + + +@typing.overload +def class_schema(clazz: type, base_schema: T_SchemaType) -> T_SchemaType: + ... + + +def class_schema(clazz, base_schema=None): """ Convert a class to a marshmallow schema From 6105dbc44da325a6a346d585bff0f1ab678b16d7 Mon Sep 17 00:00:00 2001 From: Kazantcev Andrey <45011689+heckad@users.noreply.github.com> Date: Mon, 20 Apr 2020 16:35:47 +0300 Subject: [PATCH 3/5] More correct type --- marshmallow_dataclass/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index bd17bec..6355afd 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -62,7 +62,8 @@ class User: NoneType = type(None) _U = TypeVar("_U") -T_SchemaType = TypeVar('T_SchemaType', bound=Type[marshmallow.Schema]) +T_Schema = TypeVar('T_Schema', bound=Type[marshmallow.Schema]) +T_SchemaType = Type[T_Schema] # Whitelist of dataclass members that will be copied to generated schema. MEMBERS_WHITELIST: Set[str] = {"Meta"} From f2a219c9d440083b8d9ff900680e9d121d1c1d5c Mon Sep 17 00:00:00 2001 From: Kazantcev Andrey <45011689+heckad@users.noreply.github.com> Date: Tue, 21 Apr 2020 00:01:11 +0300 Subject: [PATCH 4/5] Fixes --- marshmallow_dataclass/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index 6355afd..84e0751 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -36,6 +36,7 @@ class User: """ import dataclasses import inspect +import typing from functools import lru_cache from enum import EnumMeta from typing import ( @@ -62,7 +63,7 @@ class User: NoneType = type(None) _U = TypeVar("_U") -T_Schema = TypeVar('T_Schema', bound=Type[marshmallow.Schema]) +T_Schema = TypeVar("T_Schema", bound=marshmallow.Schema) T_SchemaType = Type[T_Schema] # Whitelist of dataclass members that will be copied to generated schema. @@ -165,7 +166,7 @@ def decorator(clazz: Type[_U]) -> Type[_U]: @typing.overload -def class_schema(clazz: type, base_schema: None) -> Type[marshmallow.Schema]: +def class_schema(clazz: type, base_schema: None = None) -> Type[marshmallow.Schema]: ... From a63fde1241bda2e16d85dae5c09efd2f5ad4717d Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Tue, 21 Apr 2020 15:58:43 +0200 Subject: [PATCH 5/5] Add (currently failing) tests for #77 See https://github.com/python/mypy/issues/4300 --- tests/test_schema_type.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_schema_type.py diff --git a/tests/test_schema_type.py b/tests/test_schema_type.py new file mode 100644 index 0000000..0d5ec26 --- /dev/null +++ b/tests/test_schema_type.py @@ -0,0 +1,30 @@ +import unittest + +import marshmallow + +import marshmallow_dataclass + + +class MyBaseSchema(marshmallow.Schema): + pass + + +class A: + a: int + + +# Top-level so that this is checked by mypy +schema_with_base: MyBaseSchema = marshmallow_dataclass.class_schema( + A, base_schema=MyBaseSchema +)() + +schema_without_base: marshmallow.Schema = marshmallow_dataclass.class_schema(A)() + + +# Regression test for https://github.com/lovasoa/marshmallow_dataclass/pull/77 +class TestSchemaType(unittest.TestCase): + def test_custom_basechema_type(self): + self.assertIsInstance(schema_with_base, MyBaseSchema) + + def test_no_basechema_type(self): + self.assertIsInstance(schema_without_base, marshmallow.Schema)