Skip to content

False uninitialized-instance-variable for str slots initialized in __new__ #5280

Open

Description

@adamtheturtle

Describe the bug

The annotated slot is assigned before __new__ returns, and every normal construction supplies the required metadata. Reading Label("text", flag=True).flag succeeds. Pyrefly 1.4.0 still reports uninitialized-instance-variable; 1.3.2 accepts it under the same all-rules preset. This also prevents replacing factory-initialized string metadata with the standard __new__ pattern while adopting the new check.

Reproduced with Pyrefly 1.4.0 and CPython 3.14.8 on macOS. Pyrefly 1.3.2 accepts this example with the same configuration. Mypy 2.4.0 (strict, without project plugins) and Pyright 1.1.414 also accept it.

Reproducer

from typing import Self

class Label(str):
    __slots__ = ("_flag",)
    _flag: bool

    def __new__(cls, text: str, /, *, flag: bool) -> Self:
        result = super().__new__(cls, object=text)
        result._flag = flag
        return result

    @property
    def flag(self) -> bool:
        return self._flag

assert Label("text", flag=True).flag

Save this as pyrefly.toml:

preset = "all"
python-version = "3.14"

Compare:

uvx --from pyrefly==1.3.2 pyrefly check --config pyrefly.toml str_initialization.py
uvx --from pyrefly==1.4.0 pyrefly check --config pyrefly.toml str_initialization.py

Pyrefly 1.4.0 output

Instance attribute `_flag` is declared but never initialized [uninitialized-instance-variable]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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