Replies: 1 comment 1 reply
-
This would require an update to the type standards. It's not something pyright would implement on its own. You could suggest it in the python/typing discussion forum if you would like to champion such a change. It would probably require a new PEP to be written and approved. I think the proposal is problematic because type inference for dictionary values will likely be inaccurate in many cases. In your example above, would you expect the value type for |
Beta Was this translation helpful? Give feedback.
-
I often want to annotate certain variables, initialized with a dictionary in-place, in order to have better IDE autocompletion and better type hints. I know the correct way to this with pyright is to define a typed dict:
This is great, but many times I find these dictionaries are only declared once in the code, and the created type is not reused explicitly anywhere else.
I find this is too verbose, and wondered if pyright could do something to make this case less verbose.
I know that by simply declaring a dict like this:
d = {'a': 'a', 'b': [1, 2, 3]}
, pyright can not infere it is a typed dict because of the dict mutability. However, maybe it could be made something to tell the type checker that it should infer an in-place dictionary as a typed dict.I am not sure if this is feasible, but I was thinking that when declaring a variable like this:
pyright could use the bidirectional inference to infer the deatils of the typed dict. This is just an idea, maybe there is a better way to this this.
Beta Was this translation helpful? Give feedback.
All reactions