python / python/typeshed

Improving the type signature of `setattr`

Open
#9,410 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: deferred
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.