-
-
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
Dictionaries as structs #985
Comments
Here's an example from Hack - http://docs.hhvm.com/manual/en/hack.shapes.php |
See also python/typing#28. |
I would be very interested in the ability to recognize this kind of "typed dictionary". Significant portions of my current codebase use such dictionaries. Currently I have to approximate with Mapping[str, Any] which nullifies any type checks on the keys. Would this Github issue be the appropriate venue for hashing out the proposed semantics and spellings for an initial implementation in mypy? |
I recommend first trying to rally support for a specific syntax, and for that python/typing#28 seems more appropriate. |
I've figured out most of what I need to know to start on an initial implementation. What I haven't been able to determine is what the "dict" type is in mypy. Via static analysis I found many other types, including the Two questions: (1) Since you probably know offhand, what is the type used for dict? Perhaps a generic (2) Is there an easy way for me to do a dynamic analysis to see what internal type variables mypy sees? Perhaps there's a way to print out some kind of annotated AST with type information? Knowing this would make answering questions similar to (1) easier in the future. |
The Dict type is currently an alias for builtins.dict (though it should really be the other way around). It has some hand-coded properties but mostly the definition just comes from builtins.pyi in typeshed. Grepping for builtins.dict should reveal the many places where it is used. One way to see what types mypy infers for a given piece of code is to use reveal_type(x) -- this is a pseudo builtin that's always available. E.g.
produces:
|
Here's my initial implementation plan: (A) It must be possible to define a named TypedDict type.
It is necessary to guard Details:
(B) It must be possible to declare a variable as having a named TypedDict as its type.
In the initial iteration the "meet" operation will only recognize exact matches for simplicity (and consider inexact matches to be errors). Future iterations will likely refine it to intersect the keys of TypedDictType in some fancier way. (C) It must be possible to declare a variable as having an anonymous TypedDict as its type.
(D) Index expressions involving TypedDicts must be recognized when used as an rvalue:
(E) Index expressions involving TypedDicts must be recognized when used as an lvalue:
(F) Support for printing TypedDictType will be added.
When checking the above program mypy should emit something like:
The above implies that the canonical form of (G) Support for the alternate dict-literal syntax for the TypedDict type constructor will be added to (A) and (C):
Comments? |
Thanks for the detailed write-up -- sounds like a good plan! (But I have some suggestions below.) Comments:
Feel free to ask about any things that are unclear, and it can be useful to send a half-finished PR for feedback (just mark it as so). |
Understood. I will begin. |
Implementation Checklist(will be revised over time) Prerequisites - Understanding
PR - Declare TypedDict type --> #2206 (merged), #2492 (on deck)
Tests:
PR - Create TypedDict instance --> #2342 (merged)Literal constructor:
Copy constructor:
Reveal Type:
Tests:
PR - Manipulate named TypedDict --> #2526 (merged)
PR - Declare TypedDict type with class-based syntax (for Python 3.6+) --> #2740
(HOLD) PR - Create and manipulate anonymous TypedDict --> (not filed)
... (HOLD) PR - Dictionary literals (via
|
Is it still worth keeping this open? There's now a label "topic-typed-dict" whose issues track remaining work. Or is the TODO list above still more detailed? |
There are a few line items above that haven't been converted to subissues. I can do that, which will allow this TODO-issue to be closed. |
Ok, let's close this issue after all still relevant TODO items discussed above are covered by other, smaller issues. |
I've filed smaller issues for the items in the TODO list above that I'm pretty sure we want to tackle. Thus suggest closing this tracking issue. |
Thanks! Closing this now. |
We should probably support precise types for heterogeneous dictionaries used like structures, such as
{'name': 'Mary', 'age': 5}
. Potential example:This is just a placeholder issue for now and doesn't actually propose how to implement this.
The text was updated successfully, but these errors were encountered: