Skip to content

Two object.__getattribute__ errors caused by cls.__name__ lookup in subclass #990

@lost-theory

Description

@lost-theory

Describe the Bug

This code:

from typing import TypeVar, Type, Any

BaseT = TypeVar("BaseT", bound="Base")

class Base:
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        pass

    @classmethod
    def bar(cls: Type[BaseT], flag: bool) -> BaseT:
        if flag:
            raise ValueError(f"throwing error for {cls.__name__} because {flag=}")
        else:
            return cls("nah...")

class Foo(Base):
    def __init__(self, x: str):
        self.x = x

def main() -> None:
    print(Foo.bar(False))
    print(Foo.bar(True))

if __name__ == "__main__":
    main()

Causes these two errors related to object.__getattribute__:

$ ~/sand2-env/bin/pyrefly check --python-interpreter ~/sand2-env/bin/python x.py
ERROR Argument `Literal['__name__']` is not assignable to parameter `self` with type `BaseT` in function `object.__getattribute__` [bad-argument-type]
  --> ./x.py:12:52
   |
12 |             raise ValueError(f"throwing error for {cls.__name__} because {flag=}")
   |                                                    ^^^^^^^^^^^^
   |
ERROR Missing positional argument `name` in function `object.__getattribute__` [bad-argument-count]
  --> ./x.py:12:52
   |
12 |             raise ValueError(f"throwing error for {cls.__name__} because {flag=}")
   |                                                    ^^^^^^^^^^^^
   |
 INFO 2 errors

Pyrefly version:

$ ~/sand2-env/bin/pyrefly --version
pyrefly 0.29.2

This code passes type checking in 0.28.0, 0.28.1, 0.29.0, 0.29.1. It only started failing after installing 0.29.2 this morning.

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqiApgGoCGIANISTQIIpwBQzAQuQM7EFQC8tBGUoAKAEQduBMTQBGYAK4oAJnwldiYgJSsAxgBsunKJOIAuZlCtRlxYFAD6D1EhhOR3fcBoAqSmk4zKEY4Xx8AawB3f0Dgpi0oAFoAPigAOTAUc0trXIQjVlyAAQMjCGIYAAswZRyrW3tZUQNYoiEAbVMCAF0aYEM0IPkwfQSUkw0CC1zcpHt%2B8kG6mdyQciRuKAp9BWIAURBwEBFgMSrwSNQMYkPcKGA7gG8WgDonFHJypwBfKFliXTkBSbR4LNB8b7aZa5Yj6bjTFYzEAVBQgFBQFriD6VF64qHMUqcYwAMTAYBEpi0CPqdkczhQrncnm8UAAHkFODAQGNUhkstSZsyXqz%2BGzWA0oBB1igRDz0plsnkQKgYCJSWAXk1jsTyHDiFodEqVWqyZrRAQQLsDaw5nSPl8HPwBGInFLUE4xAK3TKdEA&version=3.12

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions