Better HN
topnewbestaskshowjobs
Log inSign up

treesciencebot

1,949 karma · joined Dec 27, 2020 (5y ago) · 250 submissions

python, hot silicon and anything in between.
Submissions

comment 5y ago · link · parent

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 just unparse()ing everything. CST is definitely more sound in terms of preserving these details but dealing with it much more complicated than the regular AST. Refactor's README these also mentions other libraries for complex refactors: parso, LibCST, FixIT

comment 5y ago · link · parent

Thanks for the suggestion though just want to clarify, I am also one of the maintainers of parso :-) (and also the ast module in the standard library)

comment 5y ago · link · parent

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 additional libraries.

comment 5y ago · link · parent

Indeed! I also would suggest people to use a CST implementation (parso / LibCST) instead of refactor if they intend do large scale refactors, but from what I can see in my previous attempts (e.g teyit, a unittest assertion formatter) when you deal with small code fragments (a single expression, or a small statement) then you generally don't need to worry much about the style. The only concern is the literals (especially strings, which there are a few different variations of the same AST) where you could resurrect them back from the token stream (which the CustomUnparser representative in refactor allows).

The real start point for this project was to find / replace all type(<literal>)'s in CPython codebase with type(type(<literal>)) (e.g type('') would become type(str)) which is very light weight transformation, and I was able to write a script which did it without having any major problems about style on over 2000 files. Here it is for the reference: https://github.com/isidentical/refactor/blob/master/examples...

Also one thing to note here is that; in the last couple of years, thanks to black (and yapf), the adoptance of code formatters have really increased which is very nice for custom refactoring tools like refactor since the end-code would be refactored anyways so that means if you convert a multi line call, or a list to a single line version then the formatter you use probably reformat that segment anyways.

But thanks for authoring Bowler! It is a very cool project.

comment 5y ago · link · parent

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 statement etc.) due to it being using a LL(1) parser. The regular ast on the other hand is always compatible with the current grammar, since that is what CPython internally consumes. It is also more handy to work with since majority of the linters already using it, so it would be very easy to port rules.

comment 5y ago · link · parent

Yes! I love to do transformations with lib2to3, but unfortunately it is now deprecated. As I've stated in the README, for complex refactorings I also would personally prefer working with CST (probably through parso instead of regular lib2to3), but for refactoring small fragments I wasn't able to observe major differences in formatting in big codebases. Thanks for sharing this btw!

comment 5y ago · link · parent

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 `refactor` would probably work out of the box once PyPy releases 3.9 (the latest stable version for PyPy is 3.7, and 3.8 is on the way!).

248.

Show HN: Python Source Code Refactoring Toolkit via AST(github.com ↗)

110 points · by treesciencebot · 5y ago · 33 comments

comment 5y ago · link · parent

This has been brought up: https://github.com/python/cpython/pull/26264#discussion_r640...

Though personally I would dislike this, since every time somebody mentioned about type hints they commented on the property of them not causing 'ANY RUNTIME EFFECTS'. Of course the specializations come with guards, so if the annotation is wrong it is just going to work as a mispredicted option and fallback to the generic implementation though the concept of them changing the initial layer of optimizations is not something that I'd rather see in 'the Python language. PEP 563 even moves this properties forward and renders them totally uneffective in the eyes of the interpreter.

250.

Should we say stop to the syntactical growth of Python?(tree.science ↗)

3 points · by treesciencebot · 5y ago · discuss

1
56789