lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.
https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html
MIT License
456 stars 78 forks source link

Union without typeguard fails without explicit message #262

Closed jasle closed 3 months ago

jasle commented 3 months ago

Currently marshmallow_dataclass fails without an explicit error message when trying to use Unions but missing the typeguard dependency.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../marshmallow_dataclass/lazy_class_attribute.py:36: in __get__
    setattr(cls, self.name, self.func())
../marshmallow_dataclass/__init__.py:462: in class_schema
    return _internal_class_schema(clazz, base_schema)
../marshmallow_dataclass/__init__.py:552: in _internal_class_schema
    attributes.update(
../marshmallow_dataclass/__init__.py:555: in <genexpr>
    _field_for_schema(
../marshmallow_dataclass/__init__.py:855: in _field_for_schema
    generic_field = _field_for_generic_type(typ, base_schema, **metadata)
../marshmallow_dataclass/__init__.py:711: in _field_for_generic_type
    from . import union_field
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    import copy
    import inspect
    from typing import List, Tuple, Any, Optional

    import typeguard
    from marshmallow import fields, Schema, ValidationError

    try:
        from typeguard import TypeCheckError  # type: ignore[attr-defined]
    except ImportError:
        # typeguard < 3
        TypeCheckError = TypeError  # type: ignore[misc, assignment]

>   if "argname" not in inspect.signature(typeguard.check_type).parameters:
E   AttributeError: module 'typeguard' has no attribute 'check_type'

../marshmallow_dataclass/union_field.py:14: AttributeError
jasle commented 3 months ago

I see two options to fix this:

  1. Obviously adding a explicit message when trying to use Unions but typeguard is not available.
  2. Make typeguard a non-extra dependency. If marshmallow_dataclass drops support for older python versions (as suggested in PR #260) and the minimum version of marshmallow is raised from 3.13 to 3.18 this would allow to completely refrain from using extra dependencies.

What do you think @lovasoa?

lovasoa commented 3 months ago

Yes, i think we should go with option 2.

jasle commented 3 months ago

Closed by #263