-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Should dict() constructor overloads be more permissive? #11532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Previously discussed in #2287. This is due to the lack of fixed-length sequences, see python/typing#592. |
Indeed, than you for the reference to the canonical upstream ticket. Come to think of it, it seems there's already a precedent for looser overload spec: Lines 1043 to 1044 in e80ad6b
Typing machinery allows both of the below, when only one of the two is allowed at runtime: dict([["a", "b"]])
dict([["a", "b", "c"]]) I suppose the wider discussion should involve type checker folks and maybe some proxy for user code, but I'm not sure where this discussion should take place, in this repo issue, or someplace else? |
This repo is the right place to make that decision. If you make PR with a change, the mypy-primer tool will test its effect on a sample of open-source code. |
There is more discussion about this in #10013. |
Note that mypy_primer is much better at measuring effect of changes that produce false positives than changes that produce false negatives. You're subject to survivorship bias [insert aeroplane meme here] and reliant on unused ignores. To your philosophical question, typeshed will usually prefer false negatives over false positives. That said, as the existence of the str splitting overloads indicate, we often try to be pragmatic. With that in mind, and that three issues about this in six years isn't the worst thing, I'd like to not have the fully general |
CodeThe summary of the original code (since rewritten to pass type checks): class CatClass: ...
class DogProvider: ...
REGISTRY: list[list[str|object]] = []
# some submodule
REGISTRY.extend((
["cats", CatClass],
["dogs", DogProvider],
# ...
))
# another submodule
REGISTRY.extend((
# ...
))
# somewhere else
d = dict(REGISTRY)
# or, simpler, like this
d = dict([
["cats", CatClass],
["dogs", DogProvider],
]) PyrightThe earlier dict fails with:
The latter with:
Extra points for the confusing "expectation" of "Iterable[list[bytes]]"... Like where did bytes even come from, there's not a single byte in the code? MyPyThe earlier:
The latter:
Extra points for "Iterable[tuple[Never, Never]]"... like Never what does that even mean? |
Current constructor overloads are here:
typeshed/stdlib/builtins.pyi
Lines 1041 to 1046 in e80ad6b
Someone took care to allow specific positive case, and to block specific negative case.
Many potential valid uses have fallen through the cracks:
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?
It was mentioned at microsoft/pyright#7382 that type checkers trust typeshed, and thus the question belongs here.
My personal preference would be for permissive type hints in these cases.
The text was updated successfully, but these errors were encountered: