Should dict() constructor overloads be more permissive?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
Current constructor overloads are here:
Someone took care to allow specific positive case, and to block specific negative case.
Many potential valid uses have fallen through the cracks:
dict([[1, 2]])
dict("AT TA GC CG".split())
dict([["str", b"bytes"]])
My understanding of the issue is that there's no way to specify sequence length (other than a tuple) in a type hint, that is there's no syntax to hint ["a", "b"] vs ["a", "b", "c"].
This brings a philosophical question: what side should typeshed err on when type hint syntax is not precise enough?
- type whatever Python may accept run time, or
- restrict users to what Python is guaranteed to accept at run time?
It was mentioned at https://github.com/microsoft/pyright/issues/7382 that type checkers trust typeshed, and thus the question belongs here.
My personal preference would be for permissive type hints in these cases.
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 stdlib/builtins.pyi at the linked dict() constructor overloads and compare them with the example calls in the issue. Check how the relevant type checkers handle those cases, then determine whether the intended policy is to accept broader runtime-valid inputs; done requires an agreed overload design and corresponding validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100