Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

Error in @typechecked with List[Dict[str, str]] #92

Open leo-deprecated opened 4 years ago

leo-deprecated commented 4 years ago

environment

function definition

from pytypes import typechecked
from typing import List, Dict

@typechecked
def func(x:List[Dict[str, str]]):
    do sth else

test with unittest

x = [
  {
    'a':'a'
  }
]
func(x=x)

raise error

ERROR: test (__main__.test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File 
    func(x=x)
  File "c:\python\py36\lib\site-packages\pytypes\typechecker.py", line 858, in checker_tp
    prop_getter or auto_prop_getter, specs, bound_typevars=bound_typevars)
  File "c:\python\py36\lib\site-packages\pytypes\typechecker.py", line 685, in _checkfunctype
    _recursion_check, True, func)
  File "c:\python\py36\lib\site-packages\pytypes\typechecker.py", line 585, in _checkinstance
    is_args, func)
  File "c:\python\py36\lib\site-packages\pytypes\typechecker.py", line 671, in _checkinstance
    bound_typevars_readonly, follow_fwd_refs, _recursion_check), obj
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1904, in _isinstance
    bound_typevars_readonly, follow_fwd_refs, _recursion_check)
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1769, in _issubclass
    bound_typevars_readonly, follow_fwd_refs, _recursion_check)
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1799, in _issubclass_2
    bound_typevars_readonly, follow_fwd_refs, _recursion_check)
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1341, in _issubclass_Generic
    _recursion_check):
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1769, in _issubclass
    bound_typevars_readonly, follow_fwd_refs, _recursion_check)
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1796, in _issubclass_2
    _recursion_check)
  File "c:\python\py36\lib\site-packages\pytypes\type_util.py", line 1140, in _issubclass_Mapping_covariant
    return issubclass(subclass, superclass)
  File "C:\Program Files\Python36\lib\typing.py", line 1148, in __subclasscheck__
    raise TypeError("Parameterized generics cannot be used with class "
TypeError: Parameterized generics cannot be used with class or instance checks

why this error raised?? i'm hard to find the reason

Stewori commented 4 years ago

Sorry, but I cannot reproduce this issue. However, I did not test with Python 3.6.8 specifically, but before I undertake the hassle to setup this specific version on my system I would like to exclude other possible causes (because I somewhat doubt that Python 3.6.8 is the cause). If I run

from pytypes import typechecked
from typing import List, Dict

@typechecked
def func(x: List[Dict[str, str]]):
    pass

x = [
  {
    'a': 'a'
  }
]

func(x=7)

that works as expected. What do you mean by "unittest"? What tests do you refer to? Anyway, please post an entire runnable code snippet you think that fails, e.g. similar to that I posted above. If there's more to it than just running python file.py, please describe how to run it such that it fails. What version of pytypes are you using?