Skip to content

Recognize hasattr narrowing when del-ing attributes #4481

Closed

Description

@yangdanny97

Describe the Bug

An attribute confirmed to be present with hasattr should be del-able. Right now they can be read but not deleted.

This example is based on a pattern found in the sphinx project

class Foo: pass

def f(foo: Foo) -> None:
    if hasattr(foo, "uid"):
        x = foo.uid # ok
        del foo.uid # error

Once it is deleted we should probably clear the narrow for the attribute, since hasattr operates via narrowing.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.fVQxDoMwDPyK1SwdKrzzgM59AEvUpICEMEqcqs_vEYIErWC9ix3nzrmjyFy-yl2kLjqV3by-Zgj4r7UwsLPRqob5yA2jpt41l-1CffACcBUIMiQwY2WcHzZM1upfXkOPBaAyKYIFvrQJ8QW9q2Y0SC5PneoUa-a1XELLfmQnz8i7Csadgezb9jkU9-1I8jpEtD3xdQo-es0Q5LFp0GzOmeJf

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    narrowing
    on Aug 9, 2026
  2. self-assigned this
    on Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

narrowingtypechecking

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions