Skip to content

Inferred type of returned cls() is not Self #2528

@spaceship-zebra

Description

@spaceship-zebra

Describe the Bug

The following example type checks in pyright, but not pyrefly:

from typing import Self

class Base:
    @classmethod
    def create(cls):
        return cls()

    @classmethod
    def create_self(cls) -> Self:
        return cls()

class Child(Base): pass

child1: Child = Child.create() # `Base` is not assignable to `Child` [bad-assignment]
child2: Child = Child.create_self() # ok

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BlGFDAAddOIDGUVHDh0AQrJiJxddXQAC02XBowGAC1yY1GzDDB1JlGKgYwAFNLgBKVeg1e6thgFdKTxdHV3EzdW0ZOX0jE3C6CysbOwcAfThhMGcoNzoAWgA%2BQUyPbw1fAKCckLD0HTk6AGFDaExHJQz3OmJdWskWqEwARkQmgcw6AF4x1sJk%2BydQuvGAJlHm1qmZwbnbBfTMkJAAGhA-Bmg4EnJEEABiOgBVC6gIJjowPzqL3HQ4WsSH14NHsqXQfho2BglEc%2BFGrAYrnyRTgDEopXKBkqH1EIAAchCoei6MB8ABfXHiE4gMi2MBQUiEBi0KAUB4ABVIdIZdDQWDw%2BGsv0gbAC9ggv0I4geQhgdEMDAYxDgiAA9KraZYGYReGxVTB0KrMLhJHBVZJhRBRZRxb9VUDKHRUAA3VDQVDYWBC9AisU-Ty4Yj%2Bq7iMixdB5Z3QuASzzTXEAZkIQxWlPQIDJp1QkguUYAYtAYBQ%2BTgCNcM0A

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions