Replies: 3 comments 5 replies
-
One of the main problems is that A_DICT = {
'a': 1,
'b': 2,
}
reveal_type(A_DICT)
A_DICT.update(OTHER)
reveal_type(A_DICT) What should both reveal? |
Beta Was this translation helpful? Give feedback.
-
No, pyright doesn't ever infer a The problem with inferring a It would be better if the developer could explicitly say "I'd like this variable to be inferred as a What is a convenient way to do this without having to define the full
A_DICT: dict[{"a": int, "b": int}] = ... This is still admittedly cumbersome because it duplicates the keys and type information.
A_DICT: TypedDict = ...
A_DICT: dict[{}] = ... If there is support for the inlined |
Beta Was this translation helpful? Give feedback.
-
I would expect using:
to infer that A_DDICT is:
This is not the case (At least not in my setup) |
Beta Was this translation helpful? Give feedback.
-
Assume you have a "constant" dictionary:
Because dictionaries are mutable, and because the type checker does not know that you do not intend to add or remove items from the dictionary, its type will be inferred as
dict[str, int]
- the broadest type possible. What would be some way to instruct type checkers to use the narrowest type possible, as they would do for e.g. a tuple - to derive a hypotheticalTypedDict
from a constant dict? (I assume Pyright at least does something like this internally since it is able to provide code completion forA_DICT
.)More generally, would it make sense to have some way to annotate symbols partially, instructing the type checker to fill in the rest? For example, if I wanted to mark
A_DICT
as immutable for type-checking purposes, I'd be forced to paremetrise it in full:Mapping[str, int]
. What if, instead, I was able to sayInfer[Mapping]
orInfer[TypedDict]
- has something like this been considered or explored in the past?Beta Was this translation helpful? Give feedback.
All reactions