Repository: yukinarit/pyserde (python)

Issue:
from_yaml will return None with empty string
I assume this is a bug and not by design:

```py
from dataclasses import dataclass
from serde.yaml import from_yaml

@dataclass
class Foo:
  bar: str

good = '''
  bar: hello
'''

bad = '''
  no: hello
'''

from_yaml(Foo, good) # Foo(bar='hello') ✅
from_yaml(Foo, bad)  # raises SerdeError ✅
from_yaml(Foo, '')   # None 😱
```


Pull request:
Fix deserializing from incompatible value e.g. None
Closes #369 

Patch:
diff --git a/serde/de.py b/serde/de.py
index b66e24c..633c879 100644
--- a/serde/de.py
+++ b/serde/de.py
@@ -362,8 +362,6 @@ def from_obj(c: Type[T], o: Any, named: bool, reuse_instances: bool) -> T:
         maybe_generic = c
     try:
         thisfunc = functools.partial(from_obj, named=named, reuse_instances=reuse_instances)
-        if o is None:
-            return None
         if is_dataclass_without_de(c):
             deserialize(c)
             return deserializable_to_obj(c)
@@ -951,9 +949,6 @@ def {{func}}(cls=cls, maybe_generic=None, maybe_generic_type_vars=None, data=Non
   if reuse_instances is Ellipsis:
     reuse_instances = {{serde_scope.reuse_instances_default}}
 
-  if data is None:
-    return None
-
   maybe_generic_type_vars = maybe_generic_type_vars or {{cls_type_vars}}
 
   {% for f in fields %}
@@ -1000,9 +995,6 @@ def {{func}}(cls=cls, maybe_generic=None, maybe_generic_type_vars=None, data=Non
   if reuse_instances is Ellipsis:
     reuse_instances = {{serde_scope.reuse_instances_default}}
 
-  if data is None:
-    return None
-
   maybe_generic_type_vars = maybe_generic_type_vars or {{cls_type_vars}}
 
   {% for f in fields %}


Test patch:
diff --git a/tests/test_basics.py b/tests/test_basics.py
index 971a7e2..30e7196 100644
--- a/tests/test_basics.py
+++ b/tests/test_basics.py
@@ -1233,3 +1233,17 @@ def test_nested_dataclass_ignore_wrapper_options() -> None:
     serialized = serde.to_dict(a)
     assert {"foo": {"myLongName": 1}} == serialized
     assert a == serde.from_dict(Wrapper, serialized)
+
+
+def test_deserialize_from_incompatible_value() -> None:
+    @serde.deserialize
+    @dataclasses.dataclass
+    class Foo:
+        v: int
+
+    with pytest.raises(serde.SerdeError):
+        assert serde.from_dict(Foo, None)
+    with pytest.raises(serde.SerdeError):
+        assert serde.from_dict(Foo, "")
+    with pytest.raises(serde.SerdeError):
+        assert serde.from_dict(Foo, 10)
diff --git a/tests/test_de.py b/tests/test_de.py
index 3868393..54ca516 100644
--- a/tests/test_de.py
+++ b/tests/test_de.py
@@ -5,9 +5,6 @@ from serde.de import from_obj
 
 
 def test_from_obj():
-    # None will be None
-    assert from_obj(int, None, False, False) is None
-
     # Primitive
     assert 10 == from_obj(int, 10, False, False)