plugin-python icon indicating copy to clipboard operation
plugin-python copied to clipboard

Integration model for Prettier + Python

Open taion opened this issue 7 years ago • 18 comments

This plugin stuff looks great!

I don't have a concrete proposal here, yet, but I'd like to raise a point about how integration should look. As a Python user, ideally I'd like my workflow to look something like:

$ brew install prettier
$ pip install prettier # Or maybe prettier-python.

Why? Well, it's nice not to have to reach out to npm when I'm working with Python. Additionally, while the vendoring here certainly works, it's not quite ideal (you'd have to tweak PYTHONPATH to scale things up).

It shouldn't be difficult to pull in the doc builders from Prettier and write them in Python, for example, which then ought to open up implementing all of this in Python.

The question, then, would be how to actually register the plugin.

taion avatar Dec 24 '17 16:12 taion

You'll still need node installed for Prettier itself, but we could definitely do things to streamline that install.

azz avatar Dec 24 '17 22:12 azz

Right – I wonder if you could use Homebrew to install Prettier though.

taion avatar Dec 25 '17 03:12 taion

Or, like, make pip install prettier pull down both the core (somehow) and the Python plugin.

taion avatar Dec 25 '17 03:12 taion

The thing I'd ultimately like to have is just:

$ pip install flake8-prettier

Like with eslint-plugin-prettier, and be able to think minimally about npm/Node in this context.

Consider someone running the equivalent of that in Travis or whatever – it's not always possible or convenient to pull in a Node runtime just to run my Python build pipeline.

taion avatar Dec 25 '17 20:12 taion

(Of course that isn't possible right now, but a plugin-style architecture that makes the Node API just a client might make that possible downstream. As noted, the printing isn't the hard part, necessarily – so much as the rest of the stuff that Prettier Core offers, which could be useful in a multi-language project.)

taion avatar Dec 25 '17 20:12 taion

You could make it a self-contained executable with pkg.

Timer avatar Dec 26 '17 03:12 Timer

My main concern here is less containing the Python parts than containing the Node parts. Ultimately, this project needs to target Python devs and work with Python workflows.

taion avatar Dec 26 '17 04:12 taion

Starting to wonder if round-tripping through Node is a great idea. Given little inconsistencies like https://github.com/prettier/prettier-python/commit/479ecce524ecbb4e6fc7fc1585a4417aca9f29a7#r26507439, it may be better to just pickle the AST, especially if we do the printing in Python.

That does bring up the question of how to make multi-parsers work, though.

taion avatar Dec 27 '17 21:12 taion

Moving this back from https://github.com/prettier/prettier/issues/3503.

ATM the most promising short-term approach seems to be to move the printing logic into Python, and have the "parser" output something closer to what most Prettier printers output, and make the printer be more of a stub. That's because there isn't a good canonical representation of the Python AST in JS anyway, so the distinction is somewhat moot.

That should be enough to make further progress here. If it turns out that, as with Markdown, the core Prettier printing code needs extensive modifications to work with Python, or if it becomes pressing to integrate with something like Flake8, then other things might be required down the road, but it's not really pressing.

taion avatar Jan 02 '18 08:01 taion

as with Markdown, the core Prettier printing code needs extensive modifications

That's an overstatement, the changes required were only to support the blockquote format:

> lorem ipsum dollar
> stat amit.

And those changes were generic so we can support any kind of line break/ wrapping characters now.

I doubt Python will require any changes to Doc.

azz avatar Jan 02 '18 09:01 azz

That’s great to hear, then.

taion avatar Jan 02 '18 09:01 taion

Could someone help me understand the constraints and requirements?

What I understood so far:

  • The plugin targets mainly python developers.
  • Python developers do not want to to deal with node.js runtime and dependencies.

What I did not understand is:

  • How would moving the printing code to python make a difference if the base prettier library which is written in JavaScript has to be loaded anyhow?

What I'd like to ask is:

  • Would a python parser written in JavaScript assist this project in any manner?

Thanks.

bd82 avatar Mar 06 '18 17:03 bd82

As a Python user I'm not personally fussed with which language prettier-python is implemented in. I'm happy using npm as it's easily conda-installable and is already required for the Jupyter ecosystem.

In any case, I wouldn't expect to be manually installing it but instead would like to simply use it from my IDE being either VSCode or PyCharm.

dhirschfeld avatar Mar 22 '18 23:03 dhirschfeld

Pinging in case any of the project authors have answers to my questions above?

bd82 avatar Apr 16 '18 09:04 bd82

Would a python parser written in JavaScript assist this project in any manner?

It'd be great, but wouldn't solve the "problem" of still needing node.js to format Python code.

azz avatar Apr 17 '18 11:04 azz

It'd be great, but wouldn't solve the "problem" of still needing node.js to format Python code.

If this project would be a "pure" node.js project without any python parts. Won't it be easier to bundle it as a native executable using specialized tools?

  • https://github.com/zeit/pkg
  • https://github.com/nexe/nexe

Or could this already be done?

  • even when the project utilizes two runtimes and communication between processes?

bd82 avatar Apr 17 '18 12:04 bd82

It is probably already doable - worth doing a spike to prove the concept?

azz avatar Apr 17 '18 12:04 azz

I've just learned a new world for POC, "spike" 😄

I don't know if it is worth it or not, depends how severe the issue of requiring a node.js runtime is...

I am considering creating a Python Parser in JavaScript using the parsing library I've authored - Chevrotain.

If I create such a parser would you it be advantageous to use it in the python prettier plugin?

bd82 avatar Apr 17 '18 13:04 bd82