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

Failed to resolve the boundary type in a PEP 695 new style definition #17347

Closed
danpascu opened this issue Jun 8, 2024 · 2 comments · Fixed by #17407
Closed

Failed to resolve the boundary type in a PEP 695 new style definition #17347

danpascu opened this issue Jun 8, 2024 · 2 comments · Fixed by #17407
Labels
bug mypy got something wrong topic-pep-695 Issues related to PEP 695 syntax topic-protocols topic-recursive-types

Comments

@danpascu
Copy link

danpascu commented Jun 8, 2024

The following code:

class ComparableNumber[T: ComparableNumber](Protocol):
    def __lt__(self, other: T, /) -> bool: ...
    def __gt__(self, other: T, /) -> bool: ...
    def __int__(self) -> int: ...

Fails with:

_typing.py:15: error: Cannot resolve name "ComparableNumber" (possible cyclic definition)  [misc]

Pyright seems to accept it with no issue and python itself doesn't complain or raise any exception when this definition is loaded at runtime.

This is with the current head version from github with default configuration, no command line switches and with

# mypy: enable-incomplete-feature=NewGenericSyntax

being used in the file where this is defined.

@JelleZijlstra
Copy link
Member

Works with non-PEP 695 syntax:

from typing import TypeVar, Protocol

T = TypeVar("T", bound="ComparableNumber", contravariant=True)

class ComparableNumber(Protocol[T]):
    def __lt__(self, other: T, /) -> bool: ...
    def __gt__(self, other: T, /) -> bool: ...
    def __int__(self) -> int: ...

I'll add a label for PEP 695 issues.

@JelleZijlstra JelleZijlstra added the topic-pep-695 Issues related to PEP 695 syntax label Jun 9, 2024
@danpascu
Copy link
Author

danpascu commented Jun 9, 2024

Initially I thought that the problem might be because at that point ComparableNumber is not yet defined so it cannot be referenced yet, the same way as a type cannot be referenced within its own implementation without using Self or a string annotation, but this form:

class ComparableNumber[T: "ComparableNumber"](Protocol):
    def __lt__(self, other: T, /) -> bool: ...
    def __gt__(self, other: T, /) -> bool: ...
    def __int__(self) -> int: ...

fails with the exact same error, so the issue must be somewhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-695 Issues related to PEP 695 syntax topic-protocols topic-recursive-types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants