mkapi icon indicating copy to clipboard operation
mkapi copied to clipboard

Python 3.9 : Accessing __args__ raises attribute error

Open tirkarthi opened this issue 3 years ago • 0 comments

__args__ was removed in https://bugs.python.org/issue40397 and https://bugs.python.org/issue40398 . It seems a lot of places use it that causes test failures in Python 3.9. A quick grep of args shows many places and can cause failures like this. python 3.8 added typing.get_args as a helper.

rg __args__
mkapi/core/signature.py
164:        if hasattr(annotation, "__args__") and annotation.__args__:
165:            if len(annotation.__args__) == 1:
166:                return to_string(annotation.__args__[0], obj=obj)
187:        args = [to_string(x, obj=obj) for x in annotation.__args__]
193:        if type(annotation.__args__[0]) == TypeVar:
195:        args = [to_string(x, obj=obj) for x in annotation.__args__]
200:    if not hasattr(annotation, "__args__"):
202:    if len(annotation.__args__) == 0:
204:    if len(annotation.__args__) == 1:
231:    if type(annotation.__args__[0]) == TypeVar:
233:    type_ = f"{name} of " + to_string(annotation.__args__[0], obj=obj)
256:    args = annotation.__args__
304:    args = annotation.__args__

Sample failure after trying to fix #36

==================================== FAILURES =====================================
______________________________ test_class_attribute _______________________________

    def test_class_attribute():
        attrs = get_attributes(A)
        assert attrs
        for k, (name, (type, markdown)) in enumerate(attrs.items()):
            assert name == ["x", "y", "a", "z"][k]
            assert markdown.startswith(["Doc ", "list of", "", "Docstring *after*"][k])
            assert markdown.endswith(["attribute.", "specified.", "", "supported."][k])
            if k == 0:
                assert type is int
            elif k == 1:
                assert type is None
            elif k == 2:
                assert not markdown
            elif k == 3:
>               x = signature.to_string(type)

tests/core/test_core_attribute.py:39: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
mkapi/core/signature.py:187: in to_string
    args = [to_string(x, obj=obj) for x in annotation.__args__]
mkapi/core/signature.py:187: in <listcomp>
    args = [to_string(x, obj=obj) for x in annotation.__args__]
mkapi/core/signature.py:187: in to_string
    args = [to_string(x, obj=obj) for x in annotation.__args__]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = typing.Tuple, attr = '__args__'

    def __getattr__(self, attr):
        # We are careful for copy and pickle.
        # Also for simplicity we just don't relay all dunder names
        if '__origin__' in self.__dict__ and not _is_dunder(attr):
            return getattr(self.__origin__, attr)
>       raise AttributeError(attr)
E       AttributeError: __args__

/usr/lib/python3.9/typing.py:648: AttributeError

tirkarthi avatar Oct 18 '20 07:10 tirkarthi