Skip to content

Internal error when using typing.Self in specific contexts #2858

Closed

Description

Describe the bug
pyright will crash if Self is used as a generic argument to a decorator inside a class

To Reproduce
See code below, pyright emits the following error.

An internal error occurred while type checking file "test2.py": RangeError: Maximum call stack size exceeded
    at useSpeculativeMode ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:17159:36)
    at validateFunctionArgumentTypes ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:9160:17)
    at validateFunctionArguments ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:9352:16)
    at callback ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:7804:48)
    at callback ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3129:39)
    at doForEachSubtype ([...]\pyright\dist\pyright-internal\src\analyzer\typeUtils.ts:405:9)
    at expandSubtype ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3122:13)
    at mapSubtypesExpandTypeVars ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3153:13)
    at validateCallArguments ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:7757:28)
    at Ct ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:6505:36)
Error performing analysis: RangeError: Maximum call stack size exceeded
    at useSpeculativeMode ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:17159:36)
    at validateFunctionArgumentTypes ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:9160:17)
    at validateFunctionArguments ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:9352:16)
    at callback ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:7804:48)
    at callback ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3129:39)
    at doForEachSubtype ([...]\pyright\dist\pyright-internal\src\analyzer\typeUtils.ts:405:9)
    at expandSubtype ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3122:13)
    at mapSubtypesExpandTypeVars ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:3153:13)
    at validateCallArguments ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:7757:28)
    at Ct ([...]\pyright\dist\pyright-internal\src\analyzer\typeEvaluator.ts:6505:36)

Expected behavior
Not actually sure what's expected here, I'd like if Self was allowed in the context, but I'm not sure if it's supposed to be.

Screenshots or Code
If applicable, add screenshots or the text of the code (surrounded by triple back ticks) to help explain your problem.

from typing import Any, Callable, Generic, Type, TYPE_CHECKING, TypeVar

if TYPE_CHECKING:
    from typing_extensions import Self
else:
    Self = TypeVar('Self')

T = TypeVar('T')

def my_deco(cls: Type[T]) -> Callable[..., T]:
    return lambda f: cls()


class Foo(Generic[T]):
    ...


class Bar:

    @my_deco(Foo[Self])
    def foo() -> None:
        ...


reveal_type(Bar.foo)

VS Code extension or command-line
Pre 1.1.209

Additional context
Using @Gobot1234's mypy fork with Self support this type-checks fine reveal_type outputs test2.Foo*[Self@test2.Bar]
Unsure if that's the expected behaviour but if the reference implimentation allows it I suspect pyright should too?

Additional to that,

class Foo:
    instances: ClassVar[List[Self]]

pyright will dissalow this code as classvars cannot include type-variables, However, the mypy implementation will allow Self explicitly, should pyright follow suit?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions