Mypy does not correctly understand MemberDescriptors for objects with slots.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When a Python object uses slots, MemberDescriptors are created for each attribute defined as a slot.
Mypy incorrectly interprets these fields as the type of the slotted value, rather than as a member descriptor.
To Reproduce
class TestObject(object):
__slots__ = ["a", "b"]
a: int
b: str
def __init__(self, a: int, b: str) -> None:
self.a = a
self.b = b
def test() -> None:
x = TestObject(5, "hi")
reveal_type(TestObject.a)
reveal_type(TestObject.b)
reveal_type(x.a)
reveal_type(x.b)
Expected Behavior
Here is what the types actually are based on the python repl.
>>> TestObject
<class '__main__.TestObject'>
>>> x = TestObject(10, "hi")
>>> type(TestObject.a)
<class 'member_descriptor'>
>>> type(TestObject.b)
<class 'member_descriptor'>
>>> type(x.a)
<class 'int'>
>>> type(x.b)
<class 'str'>
Actual Behavior
Mypy output:
12: note: Revealed type is 'builtins.int'
13: note: Revealed type is 'builtins.str'
14: note: Revealed type is 'builtins.int'
15: note: Revealed type is 'builtins.str'
Your Environment
Python 3.8.1
mypy 0.782
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 with the provided slots and MemberDescriptor reproduction, then trace how mypy determines the types revealed for class attributes versus instance attributes. Confirm the fix by checking that TestObject.a and TestObject.b are reported as member descriptors while x.a and x.b remain int and str.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100