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

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

Closed
bijij opened this issue Jan 14, 2022 · 2 comments
Closed

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

bijij opened this issue Jan 14, 2022 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@bijij
Copy link

bijij commented Jan 14, 2022

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*[[email protected]]
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

@erictraut erictraut added the bug Something isn't working label Jan 14, 2022
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be fixed in the next release.

I've also changed the ClassVar check to allow typing.Self.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 14, 2022
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.209, which I just published. It will also be included in the next release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants