| ▲ | chrikrah 2 hours ago | |
Tried it on a 10k loc project which never had a type checker: 0.5s, 450 results. More than 400 of them are one pattern, a dict of kwargs splatted into a call (`*kw`). ty infers the dict as `dict[str, str | int]` and flags every parameter against that. Is there any way to type the dict once, or is a TypedDict the way to go? | ||
| ▲ | ibraheemdev an hour ago | parent | next [-] | |
We do generally support this pattern for dictionary literals, but we can't track the precise types of arbitrary keys if the dictionary is created dynamically, you'd have to use a TypedDict in that case. | ||
| ▲ | arthur-st 44 minutes ago | parent | prev [-] | |
Typing spec has some detailed info if you'd like https://typing.python.org/en/latest/spec/typeddict.html Summarizing, for something that is an argument with specific fields you'd use a Mapping or a MutableMapping as the annotation, and if that's a return value, then it should be a TypedDict. Sometimes TypedDict also makes sense as an argument annotation, e.g., if it has to be a dict strictly. For a generic dictionary, like you know that you're drawing from a set of possible kwargs that are all string values, you can also just annotate them with an "anonymous" dict like what Ty is inferring on your codebase. | ||