JelleZijlstra / autotyping

Automatically add simple type annotations to your code
215 stars 18 forks source link

Add --guess-common-names #51

Closed jakkdl closed 1 year ago

jakkdl commented 1 year ago

Fixes #46 leaving as draft until specs has been finalized, i.e. how to handle common names with default value of None and the extent of common names to use for inferring.

I decided that the least intrusive way of implementing was to hijack annotate_optionals [and annotate_named_params, so I just append the guessed names to those lists. I at first considered implementing this in leave_Param, and could move back there if that's preferred. It took me a while to parse leave_Param and I started adding comments for myself, and figured I might as well leave them.

jakkdl commented 1 year ago

Updated strategy as per @Zac-HD's suggestion and moved the logic down into leave_Param. Still leaving as draft for now, although it's mostly finished. The tests could be more ambitious if you want, since this covers far from all possible branches of guess_type_from_argname (I guess that's what I end up doing by default when there's no coverage to keep me accountable :sweat_smile: )

jakkdl commented 1 year ago

Wrote logic for inferring simple container types, removed speculative matches, and added a lot of tests. I also modified _annotate_param to handle recursive generics - so it can now do Optional[List[int]] etc.

jakkdl commented 1 year ago

Pushed a commit with minor changes from the above review

jakkdl commented 1 year ago

Moved both the constants and the function (since it as well is ~100 lines) into autotyping.guess_type_from_argname.py. Changed guess_type_from_argname to return a string instead of NamedParam to avoid a circular import, and did a minor restructure in leave_Param to avoid duplicate code calling guess_type_from_argname.

Also squashed the commits.