Skip to content

Incompatible type when using Self #950

@last-partizan

Description

@last-partizan

Describe the Bug

This is happening in django with django-types, but minimal reproduction is independent:

from typing import ClassVar, Generic, Self, TypeVar, reveal_type

MT = TypeVar("MT", bound="Model")

class Manager(Generic[MT]):
    def get(self) -> MT: ...

class Model:
    objects: ClassVar[Manager[Self]]

class UserProfile(Model):
    ...

def get_user_profile() -> UserProfile:
    v = UserProfile.objects.get()
    reveal_type(v)
    return v
 INFO revealed type: Self@Model [reveal-type]
  --> /tmp/test.py:16:16
   |
16 |     reveal_type(v)
   |                ---
   |
ERROR Returned type `Self@Model` is not assignable to declared return type `UserProfile` [bad-return]
  --> /tmp/test.py:17:12
   |
17 |     return v
   |            ^
   |
 INFO 1 error

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoDCANgIYDOZAaiSADRQDiApikyEgMb0DKTRw9ACqIm1OlBBMAbkxJEA%2BvARMAUCoCygqAF4ow5WIAUAIk3H6AIzABXFABNtpsHb7GAlGo6kKUdSRQkaGyGzKzsHADamgC6bgBcKlBJUC7AUEEwhmR8wG5QALQAfL6CcVAAdJWe3mS%2BznwJyVBgFgBWTBwwZGXE5FQ0Uf6BbBG8-NHR1X1QAKrZIAAK4MBIREyG6vVE8YnJleVqqelMMPLW8-IIy6vreUWz80tgK2uNyVI6D2xPL0zlLe1OmRyhlDB4mpIZHJFCJDFJwclJDBrCAUFApEA&version=3.12

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions