False positive in case of custom __setattr__ that changes the type of the attribute
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
I have a class with a custom __setattr__(self, attr, value:TYPE). This method converts value to another type.
I am getting false positives, since mypy assumes that for each particular TYPE, type(getattr(obj, attr)) == TYPE.
To Reproduce
Minimal code example:
from typing import *
class Context:
def __setattr__(self, attr:str, value:Any):
super().__setattr__(attr, str(value))
ctx = Context()
ctx.a = 10
print(len(ctx.a))
Actual Behavior
mypy output
....py:9: error: "Context" has no attribute "a"
....py:9: error: Argument 1 to "len" has incompatible type "int"; expected "Sized"
I would like to tell mypy that for this class, it mustn't assume that type(getattr(ctx, "a")) == type(10) .
Your Environment
- mypy 0.961 (compiled: yes) on Ubuntu Linux (Jupyter Docker image)
- Python 3.8.10 (Anaconda)
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 running the minimal Context example through mypy and compare the reported errors with the expected behavior described in the issue. Trace the type-checking path for custom setattr and inferred attributes; done means the example no longer produces false positives for the converted attribute type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100