Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the type signature of setattr #9410

Open
not-my-profile opened this issue Dec 26, 2022 · 2 comments
Open

Improving the type signature of setattr #9410

not-my-profile opened this issue Dec 26, 2022 · 2 comments
Labels
status: deferred Issue or PR deferred until some precondition is fixed

Comments

@not-my-profile
Copy link
Contributor

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.

@srittau
Copy link
Collaborator

srittau commented Dec 27, 2022

The same is true when using an instance of a class that defines __slots__. I think the best we can do here is to use Any instead of object. Functionally it's mostly identical, but it signals that not all objects are valid here.

@JelleZijlstra
Copy link
Member

With TypingError (python/typing#1043) we could perhaps special-case None. It won't cover all cases where setattr doesn't work, but doing setattr on an object of an optional type is a plausible mistake.

@AlexWaygood AlexWaygood added the status: deferred Issue or PR deferred until some precondition is fixed label Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: deferred Issue or PR deferred until some precondition is fixed
Projects
None yet
Development

No branches or pull requests

4 participants