-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Recognizing dictionary-like objects #2120
Comments
If you want to get the actual dictionary (for serialization) you have |
If you really want If you only want access with string literals, you still have something similar to requiring mypy to understand |
In my case I'd only be advocating for a NamedDict that allows access by string literals only. To elaborate more on my use case, I have some data structures that spend most of their life either (1) in a JSON-field in a database or (2) being serialized to and from a JavaScript frontend, which is why they are defined directly as dict objects with specific keys rather than as something nicer like namedtuple (which would actually enforce correct key usage at runtime). So I'm looking for a way to preserve the value as a dict at runtime but get type checking for key accesses by a type checker. Usage of For further illustration, here are some things I'd love to be able to do:
I know that I've been using the spelling
I'm afraid I don't follow this comment at all. I've only been talking about dictionaries so far, not tuples or tuple manipulations. |
Regarding the type definition, you basically want a type that is parameterized, in addition to types, by string literals. Currently the only parameterization that's not only types is that of My last comment was about type checking process; I am sorry about the telegraphic phrasing. Let me elaborate. In order to support checking
|
There's an existing issue in the typing repo for this (python/typing#28) and also a mypy issue (#985). Special casing this in the type checker would be possible, similar to special casing tuples. As @elazarg pointed out, this would be harder to do for non-literal keys. I've thought about this in the past, and the design and implementation wouldn't be simple. Some challenges are mentioned in the issue above. The feature could be pretty useful, though. |
Closing as dupe. You can continue the discussion in the issues mentioned above -- the typing issue could be better for more general things such as type syntax, while the mypy issue is good for technical details. |
The project I am currently working on, which I am starting to use mypy to typecheck, makes occasional use of what I call "dictionary-like objects". Namely JSON-compatible structures with specific types. For example:
My understanding is that there is no way in mypy to spell the type for a dictionary-like object such as
Action
above. Instead the best I could do isMapping[str, Any]
orobject
which is too broad to be useful. All reads of anAction
must use a one of the standard keys (target_type, target_pk, action_type, action_params
) and the value type for each particular key is known in advance.It would be nice to annotate the type of
Action
using something like:Thoughts? Is this kind of typed dictionary actually understood by mypy and I've just missed it? Is this a missing capability that has been brought up before?
The text was updated successfully, but these errors were encountered: