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

is_of_type does not work as expected with Annotated[Generic[T], ...] #102

Open phubaba opened 3 years ago

phubaba commented 3 years ago

Note this is in python 3.9 where typing.Annotated exists. I'm using pytypes from pip install git+https://github.com/Stewori/pytypes.git

import pytypes
import typing
CT = typing.TypeVar('CT')
class Variable(typing.Generic[CT]):
    pass
pytypes.is_of_type(Variable[int](), Variable[int]) # returns True as expected
pytypes.is_of_type(Variable[int](), typing.Annotated[Variable[int], 'a']) # returns False unexpected

pytypes.is_of_type(3, typing.Annotated[int, 'a']) # returns True as expected

After stepping through the is_of_type function I end up in _issubclass_Generic. I suspect there is some issue in _issubclass_Generic here:

          elif origin is not None and \
                  _issubclass(_origin(subclass), origin, bound_Generic, bound_typevars,
                          bound_typevars_readonly, follow_fwd_refs, _recursion_check):

takes an origin of the subclass so Variable[int] turns into Variable and origin of typing.Annotated is Variable[Int] so clearly _issubclass(Variable, Variable[int]) will not go through later parts of the function will basically all return False, so I think this is the place that needs to work.

Anyways let me know if I can be of any more help in resolving this issue.

Stewori commented 3 years ago

Please note that Python 3.9 is not supported at the moment. Also 3.7 and 3.8 are not fully supported (but mostly), see #40 and #99. If you can make this particular issue work, feel free to do it (PR welcome). However, I suspect a broader 3.9 support would be a larger effort, which I currently cannot afford. (Changes in typing between 3.8 and 3.9 were manifold)