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
record per-key types for heterogenous dicts and suggest TypedDict annotation #105
Comments
how about having a wrapper that 'acts like a dict' and shows intention-to-be-TypedDict ? eg). TypedDictPlaceHolder: https://gist.github.com/devdoomari3/3e6e9e8177d2ff97c4ef0091209984f5 usage:
|
@carljm my co-worker @Anylee2142 came up with an improvisation:
Here's an example of monkeytype "type_details":
what do you think about this? I'm hoping this can get merged (so we don't have to maintain a separate version of MonkeyType) |
@devdoomari3 Yes, recording And we would want MonkeyType itself to automatically generate TypedDict annotation if it finds a dict for which the set of keys and their types is "consistent enough" across traces, not just leave that to custom clients. One interesting choice is whether to ever generate Thanks for working on this issue! |
@carljm thank @Anylee2142 --- it's his idea & implementation I'll get him to do a pull-request today. ps: I personally don't believe in heuristics though... |
MonkeyType is all about best-effort annotations to be reviewed and improved by a human. We don't ever expect that MonkeyType can get things annotated correctly in a purely automated way, because good annotations require more knowledge about the intent of the code than MonkeyType can have access to. This isn't new with this feature, it's already how MonkeyType works; there are a lot of heuristics in the existing type rewriters (e.g. "Union larger than 5 elements becomes Any," for one example.) |
@carljm then would an IDE-plugin for reviewing/improving generated types be helpful? I don't like looking at git diffs... / 'applied' types getting mixed up with current edits |
Not sure, I guess if it would be useful to you you can build it and see if it's useful to others? No reason for it to live in this repo, but if it works well and gains some interest we could certainly link it from the MonkeyType docs.
Haven't had that problem or heard that complaint in using MonkeyType at Instagram. You generate draft annotations for a module using MonkeyType, open up the module in your editor, and edit the annotations as needed until they are ready for commit. |
@carljm you're right that the IDE plugin shouldn't live in this repo.
this is just a personal opinion (I'm too lazy). |
Fixed with merge of #143. |
Currently MonkeyType assumes dicts are homogenous. When heterogenous dicts are seen, we just combine all the value types into a Union (and then if that Union gets too big, by default we rewrite it to Any). It'd be more useful in such cases to suggest a TypedDict annotation with per-key value types.
The trick here is that we don't really know the intention, so there'd be some level of heuristic involved. Like, if there's multiple non-None value types (and probably if the number of keys is less than some threshold), rather than recording just a single value type, we record per-key value types. Then we'd also need some smarts when combining traces into an annotation: if we have any traces with per-key value types, try to construct a consistent TypedDict, but if we find that key names and value types aren't matching up consistently, fallback to normal Dict.
There's enough fuzziness in the heuristics here and this'd be enough work that it might not be worth it, but for codebases that use lots of heterogenous dicts, it'd make the generated annotations a lot more useful.
The text was updated successfully, but these errors were encountered: