Skip to content

Improving the type signature of setattr #9410

Open
@not-my-profile

Description

@not-my-profile

When the first argument of setattr is of type NoneType or EllipsisType the function will always raise an AttributeError on runtime, so it would be nice if the following calls would be detected as type errors.

x = None
setattr(x, 'foo', True) # AttributeError: 'NoneType' object has no attribute 'test'
y = ...
setattr(y, 'foo', True) # AttributeError: 'ellipsis' object has no attribute 'test'

I am not sure what's the best way to go about this is, there are probably many builtin types that do not support arbitrary attributes to be set. We do not appear to have a type to categorize such types, which could then in the future be used with Not[], once python/typing#801 is implemented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: deferredIssue or PR deferred until some precondition is fixed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions