[{"data":1,"prerenderedAt":263},["ShallowReactive",2],{"project-28027016":3},{"id":4,"name":5,"line":6,"line_source":7,"title":8,"url":9,"domain":10,"maker":11,"posted_at":12,"year":13,"points":14,"comments":15,"rank_in_year":16,"shown_in_year":17,"top":18,"tier":19,"category":20,"subcategory":21,"format":22,"keywords":23,"reception":28,"sentiment":29,"stance_mix":30,"scored_comments":32,"aspects":33,"strip":36,"post_text":29,"what":37,"said":62,"maker_in_thread":127,"shape":138,"grid":142,"attempts":226,"same_week":227,"maker_shows":162,"maker_hidden":256,"reader_version":257,"card":258},"28027016","refactor","Python Source Code Refactoring Toolkit via AST","title","Show HN: Python Source Code Refactoring Toolkit via AST","https:\u002F\u002Fgithub.com\u002Fisidentical\u002Frefactor","github.com","treesciencebot",1627832057,2021,110,33,482,2533,20,"notable","dev_tools","code refactoring","library",[24,25,26,27],"python refactoring","ast","code transformation","source rewriting","quiet",null,{"positive":31,"neutral":32,"mixed":31,"critical":31},0,3,{"idea":34,"execution":35},{"positive":31,"critical":31},{"positive":31,"critical":31},[31,31,31,31,32,31,31,31,31,32,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,32,31,31],{"problem":38,"audience":29,"pricing":29,"motivation":48,"time_to_build":29,"stack":52,"openness":60,"stage":61,"team_size":61},{"value":39,"source":40,"evidence":41},"Refactoring Python code with the stdlib AST, for small code fragments","maker",[42,45],{"id":43,"by":11,"text":44},"28033491","Indeed! I also would suggest people to use a CST implementation (parso \u002F LibCST) instead of refactor if they intend do large scale refactors, but from what I can see in my…",{"id":46,"by":11,"text":47},"28038982","Extra clarification: I'm well aware the concerns about tokens vs CST vs AST, that is why I decided to write down a library that does refactors on small code fragments rather than…",{"value":49,"source":40,"evidence":50},"Maker needed to rewrite type(\u003Cliteral>) calls across CPython codebase, which led to the project",[51],{"id":43,"by":11,"text":44},[53],{"value":54,"source":40,"evidence":55},"Python",[56,59],{"id":57,"by":11,"text":58},"28027301","Author of this project here (and also a CPython core developer FWIW), for many aspects (including the AST) PyPy follows CPython closely even on implementation details so…",{"id":43,"by":11,"text":44},"open_source","unknown",{"praise":63,"objections":69,"requests":81,"questions":89,"alternatives":97},[64],{"value":65,"source":66,"evidence":67},"Useful for small-fragment refactors where formatting loss matters little","commenter",[68],{"id":43,"by":11,"text":44},[70],{"value":71,"source":66,"evidence":72},"stdlib ast loses comments and whitespace, so CST tools like parso or LibCST are better for large refactors",[73,77],{"id":74,"by":75,"text":76},"28030843","jreese","(Author of Bowler\u002Fmaintainer of fissix) The main concern with using the ast module from stdlib is that you lose all formatting\u002Fwhitespace\u002Fcomments in your syntax tree, so writing…",{"id":78,"by":79,"text":80},"28036681","boxed","You should check out parso which is a fully roundtrip-able AST. That way your tool won't throw away comments and formatting.",[82],{"value":83,"source":66,"evidence":84},"Support for other languages, a polyglot pattern-matching rewrite tool",[85],{"id":86,"by":87,"text":88},"28039396","bmc7505","I wonder if something like this exists for other languages? Most rewriting\u002Frefactoring tools make too many assumptions about the surrounding project. Something simple which…",[90],{"value":91,"source":66,"evidence":92},"Whether it works on PyPy",[93],{"id":94,"by":95,"text":96},"28027225","TekMol","The one thing I hope for when it comes to Python is that PyPy becomes the default runtime everywhere. CPython is so unbelievably slow when compared to languages like PHP and…",[98,105,109,113,120],{"value":99,"source":66,"evidence":100},"Bowler",[101],{"id":102,"by":103,"text":104},"28029351","graton","A similar type project. Though I haven't seen much activity recently: https:\u002F\u002Fgithub.com\u002Ffacebookincubator\u002FBowler",{"value":106,"source":66,"evidence":107},"parso",[108],{"id":78,"by":79,"text":80},{"value":110,"source":66,"evidence":111},"LibCST",[112],{"id":74,"by":75,"text":76},{"value":114,"source":66,"evidence":115},"semgrep",[116],{"id":117,"by":118,"text":119},"28033504","woile","Maybe something like this: https:\u002F\u002Fsemgrep.dev\u002F I found it last week. It may be able to detect a potential issue in multiple languages, but not sure if it supports refactoring.",{"value":121,"source":66,"evidence":122},"tree-sitter",[123],{"id":124,"by":125,"text":126},"28032826","jay-aye-see-key","This is not a problem I'd had, but it sounds like something tree-sitter might be able to help with. I've been using tree-sitter for syntax highlighting, simple refactors, and…",{"comments":128,"answered":129,"key":130},7,1,[131,134,135],{"id":132,"text":133},"28029388","I haven't used bowler, but from what I can see it is using lib2to3 (through fissix), which can't parse newer Python code (parenthesized context managers, new pattern matching…",{"id":43,"text":44},{"id":136,"text":137},"28033682","It would probably be better if you can just write a typescript parser, and then you can transpile type script AST directly to Python AST and use ast.unparse() without any…",{"commenters":18,"max_depth":139,"maker_replies":128,"first_comment_mins":140,"lifespan_hours":141},6,25,157,[143,147,149,152,153,155,158,161,163,165,167,170,172,174,176,180,182,187,190,193,196,198,200,201,204,207,210,213,216,219,221,222,225],[144,31,31,145,146,31],"28031376","commenter\u002Fneutral\u002Fnone","hsbauauvhabzb",[124,31,31,148,125,31],"other\u002Fneutral\u002Fnone",[150,31,31,148,151,31],"28033060","chrisphilip",[117,31,31,148,118,31],[86,129,32,154,87,31],"project\u002Fneutral\u002Fidea",[156,129,31,148,157,31],"28047286","erezsh",[159,129,31,160,87,31],"28104254","commenter\u002Fpositive\u002Fdesign",[102,162,31,148,103,31],2,[132,162,31,164,11,129],"project\u002Fcritical\u002Fexecution",[74,162,32,166,75,31],"project\u002Fneutral\u002Fexecution",[168,162,31,148,169,31],"28031567","westurner",[171,162,31,148,75,31],"28032621",[173,162,31,148,169,31],"28083861",[43,162,31,175,11,129],"maker\u002Fpositive\u002Fexecution",[177,32,31,178,179,31],"28033044","other\u002Fneutral\u002Fidea","dreamer7",[136,32,31,181,11,129],"commenter\u002Fneutral\u002Fidea",[183,184,31,185,186,31],"28028838",4,"other\u002Fpositive\u002Fidea","milkbikis",[188,184,31,189,11,129],"28029350","maker\u002Fneutral\u002Fexecution",[191,184,31,192,186,31],"28029762","commenter\u002Fpositive\u002Fnone",[194,195,31,178,179,31],"28033045",5,[197,195,31,181,157,31],"28047235",[94,139,31,199,95,31],"other\u002Fneutral\u002Fperformance",[57,139,31,175,11,129],[202,139,31,199,203,31],"28027335","keithasaurus",[205,139,31,199,206,31],"28027473","formerly_proven",[208,139,31,199,209,31],"28028753","xapata",[211,139,31,199,212,31],"28028190","rattray",[214,139,31,148,215,31],"28029067","heavyset_go",[217,139,31,148,218,31],"28029289","sitkack",[220,139,31,199,146,31],"28031393",[78,128,32,166,79,31],[223,128,31,224,11,129],"28038663","maker\u002Fneutral\u002Fnone",[46,128,31,166,11,129],[],[228,233,238,242,247,252],{"id":229,"name":230,"points":231,"category":232},"28012829","Static.wiki",796,"learning_science",{"id":234,"name":235,"points":236,"category":237},"28017051","redbean",223,"infrastructure",{"id":239,"name":240,"points":241,"category":20},"28059291","Till",209,{"id":243,"name":244,"points":245,"category":246},"27996348","Clockwise",162,"productivity",{"id":248,"name":249,"points":250,"category":251},"28048528","ocean",159,"languages",{"id":253,"name":254,"points":255,"category":237},"28063597","Hookdeck",126,false,"v2",{"id":259,"name":5,"line":6,"ai":256,"cat":20,"year":13,"points":14,"comments":15,"rec":28,"s":29,"top":18,"kw":260,"maker":11,"domain":10,"tier":19,"attempts":31,"strip":261,"url":9,"mix":262,"praise":65,"objection":71,"sub":21},28027016,[24,25,26,27],"000030000300000000000000000000300",[31,32,31,31],1793991031281]