Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytype.tools.annotate_ast.annotate_source crashes with RecursionError: maximum recursion depth exceeded in __instancecheck__ #1616

Open
YingboFu opened this issue Apr 16, 2024 · 0 comments

Comments

@YingboFu
Copy link
Contributor

Hi,

Thanks a lot for the amazing PyType you developed.

When I run the annotate_source on the following file (simplified)


from abc import ABC
from typing import Any, Callable, Generic, Iterator, List, Literal, Optional, Set, TypeVar, Union, cast

T = TypeVar("T")
V = TypeVar("V")

Factory = Optional[Callable[[object], T]]  # note the argument is anything, due e.g. memory loader can inject anything


class Convert(ABC, Generic[T]):
    """A class that converts a raw type to a given tox (python) type."""

    def to(self, raw: T, of_type: type[V], factory: Factory[V]) -> V:  # noqa: PLR0911
        """
        Convert given raw type to python type.

        :param raw: the raw type
        :param of_type: python type
        :param factory: factory method to build the object
        :return: the converted type
        """
        from_module = getattr(of_type, "__module__", None)
        if from_module in {"typing", "typing_extensions"}:
            return self._to_typing(raw, of_type, factory)

    def _to_typing(self, raw: T, of_type: type[V], factory: Factory[V]) -> V:  # noqa: C901
        origin = getattr(of_type, "__origin__", of_type.__class__)
        if origin in {list, List}:
            entry_type = of_type.__args__[0]  # type: ignore[attr-defined]
            result = [self.to(i, entry_type, factory) for i in self.to_list(raw, entry_type)]

PyType crashes with the traceback info:

File "/Users/fuyingbo/Desktop/into_the_wild_dev/venv/lib/python3.9/site-packages/pytype/output.py", line 312, in value_to_pytd_type
    elif isinstance(v, (typing_overlay.TypeVar, typing_overlay.ParamSpec)):
pytype.tools.annotate_ast.annotate_ast.PytypeError: Pytype error: RecursionError: maximum recursion depth exceeded in __instancecheck__

I guess one potential reason behind this crash is that Convert.to might call Convert._to_typing while Convert._to_typing might also call Convert.to back, which PyType consider as an infinite recursion and crashes.

The full file is at https://github.com/tox-dev/tox/blob/main/src/tox/config/loader/convert.py

I am unsure if it is a bug, but an error message might be better than crashes and thus report it.

Thanks a lot.

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

No branches or pull requests

1 participant