Skip to content

Cannot instantiate Foo because it is a protocol [bad-instantiation] #2420

@MarcoGorelli

Description

@MarcoGorelli

Describe the Bug

The following case satisfies other type checkers, but pyrefly throws

ERROR Cannot instantiate `Foo` because it is a protocol [bad-instantiation]
 --> t.py:7:26
  |
7 |         return type(self)(3)
  |                          ^^^
  |
 INFO 1 error
from typing import Protocol, Self

class Foo(Protocol):
    def __init__(self, a: int) -> None:
        ...
    def foo(self) -> Self:
        return type(self)(3)

Spotted in Narwhals

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AFag1wBjXFAA0dAMowoYADrploqKjhw6AMVy4AFENwjxUAJSJlda3UwwwdAPqPWEBs-1x5YaakSd0BjM6AFoAPjoAOVx0GEt0G0S6QhSrGzsHMD1Pb2Dw2W94pJtKGAYAV0oEpmIYHIUzfQBmM2VlEEkQcoZoOBJyRBAAYjoAVR6oN1I6MHL0UR6YuDasexneGlR3dHKabBhKfXx-ViDQiLgGSiKSssqEpRBI3f3rumB8AF9FEHbOslKYCgpEIIhoUAoIwEpEBwLoaCweHwdHE6EgbEqWwgMUIyhGchgdAAFgwGMQ4IgAPSUgH2YGEXhsSkwdCUzBiOCU1HozGLVnrSh0VAAN1Q0FQ2FgKJiPMoWJidFwxD5fWUZAYRJiIWFBzg2ISAF46D8moQAIwAJh%2B7U%2BnVQCwgOu00BgFAROAI-RAnyAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-discussionAn issue where it's not clear whether there is a bug or we are behaving as expected.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions