Conformance suite: Questionable tests in dataclasses_descriptors.py
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- testing-qa
Research direction
Read conformance/tests/dataclasses_descriptors.py, focusing on the Desc2 and DC2 cases described in the issue. Check the asserted types against the runtime behavior and the differing pyrefly, mypy, and pyright results. Done means the conformance suite no longer requires behavior that is not specified or supported by runtime behavior, with the proposed test removal resolved by project maintainers.
Written by the indexing model from the issue text.
Description
The dataclasses_descriptors.py test case has a comment "This portion of the dataclass spec is under-specified in the documentation, but its behavior can be determined from the runtime implementation."
However, some of the behaviors in tests don't align with the runtime behavior.
The first part, which is using a data descriptor Desc1 and a dataclass DC1, is fine.
Then we get this, where Desc2 is a non-data descriptor (only implementing __get__):
@dataclass
class DC2:
x: Desc2[int]
y: Desc2[str]
z: Desc2[str] = Desc2()
assert_type(DC2.x, list[int])
assert_type(DC2.y, list[str])
assert_type(DC2.z, list[str])
However, DC2.x and DC2.y are AttributeErrors at runtime, because there is no Desc2 object in the runtime class dictionary. I don't know if we should require type checkers to produce an error but certainly we should allow it.
Then we construct a DC2 object:
dc2 = DC2(Desc2(), Desc2(), Desc2())
assert_type(dc2.x, int)
assert_type(dc2.y, str)
assert_type(dc2.z, str)
But at runtime, dc2's three attributes are all just those Desc2 objects, not ints or strs, because those objects are stored as instance attributes, so the descriptor protocol never runs on them.
The more complicated case is the z attribute, which has a Desc2 object stored in the class. Dataclasses would conventionally treat this as the default, but it gets the default by conventional access in the class body, which calls the descriptor's __get__ with obj=None. So the runtime default for this field is actually the value you'd get for accessing DC2.z, which is list[str].
Looking at actual type checker behavior, I think pyrefly gets all this pretty much right (which means it fails the test). Mypy and pyright pass the test, but their behavior is wrong.
Since (as the test acknowledges) the behavior is not fully specified, perhaps it is best to delete this test from the conformance suite for now, until we come up with a full specification for the behavior of descriptors in dataclasses.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
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.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
learningequality/ricecooker#747 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
run-llama/llama_index#23199 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
KhronosGroup/glTF-Blender-IO#2769 ·