Skip to content

Conversation

@x
Copy link

@x x commented Dec 4, 2023

Currently, this package doesn't support PEP 585. And the stack trace is a little confusing.

For example:

In [1]: from dataclasses import dataclass

In [2]: from dataclass_type_validator import dataclass_validate

In [3]: from typing import List

In [4]: @dataclass_validate
   ...: @dataclass
   ...: class Foo:
   ...:     bar: list[str]
   ...:     baz: List[str]
   ...: 

In [5]: Foo(bar=["bar"], baz=["baz"])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 Foo(bar=["bar"], baz=["baz"])

File ~/.pyenv/versions/3.9.10/envs/science-3.9.10/lib/python3.9/site-packages/dataclass_type_validator/__init__.py:216, in dataclass_validate.<locals>.method_wrapper(self, *args, **kwargs)
    213 @functools.wraps(orig_method)
    214 def method_wrapper(self, *args, **kwargs):
    215     x = orig_method(self, *args, **kwargs)
--> 216     dataclass_type_validator(self, strict=strict)
    217     return x

File ~/.pyenv/versions/3.9.10/envs/science-3.9.10/lib/python3.9/site-packages/dataclass_type_validator/__init__.py:168, in dataclass_type_validator(target, strict)
    165 expected_type = field.type
    166 value = getattr(target, field_name)
--> 168 err = _validate_types(expected_type=expected_type, value=value, strict=strict, globalns=globalns)
    169 if err is not None:
    170     errors[field_name] = err

File ~/.pyenv/versions/3.9.10/envs/science-3.9.10/lib/python3.9/site-packages/dataclass_type_validator/__init__.py:140, in _validate_types(expected_type, value, strict, globalns)
    138 def _validate_types(expected_type: type, value: Any, strict: bool, globalns: GlobalNS_T) -> Optional[str]:
    139     if isinstance(expected_type, type):
--> 140         return _validate_type(expected_type=expected_type, value=value)
    142     if isinstance(expected_type, typing._GenericAlias):
    143         return _validate_sequential_types(expected_type=expected_type, value=value,
    144                                           strict=strict, globalns=globalns)

File ~/.pyenv/versions/3.9.10/envs/science-3.9.10/lib/python3.9/site-packages/dataclass_type_validator/__init__.py:43, in _validate_type(expected_type, value)
     42 def _validate_type(expected_type: type, value: Any) -> Optional[str]:
---> 43     if not isinstance(value, expected_type):
     44         return f'must be an instance of {expected_type}, but received {type(value)}'

TypeError: isinstance() argument 2 cannot be a parameterized generic

This was confusing to me and it seems to other users (see: #20).

This PR adds support for generic type aliases while still maintaining backward compatibility with 3.8 (via some ugly version checks).

@p12tic
Copy link

p12tic commented Jan 15, 2025

@akiray03 just a friendly ping. Would it be possible to merge this PR and make a new release of the library? With PEP 585 type annotations becoming used in many codebases, lack of support for it makes this package less and less useful as time goes on. Which is a pity because it's otherwise great.

hedgehog319 added a commit to hedgehog319/dataclass-type-validator that referenced this pull request Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants