Improving the type signature of `setattr`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the typeshed stub for the built-in setattr and review the related discussion in python/typing#801. Determine how the signature should reject NoneType and EllipsisType without incorrectly excluding other valid targets; done means the intended calls are reported as type errors and existing setattr cases remain supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100