13 ms·
Code formatting comes to uv experimentally
- photios 1y agoLove this! I would name it `uv fmt` and add `uv vet` to the roadmap too
- ayhanfuat 1y ago> an experimental new command that Python developers have been waiting for: uv format Have developers really been waiting for this? What's wrong with ruff format?
- loloquwowndueo 1y ago> Try out uv format in your next project and see how it fits into your development workflow. The experimental nature means your feedback could help shape how this feature evolves. So maybe nobody has been waiting for this and the feedback will be: we don’t need this. Also it uses ruff under the hood. If it’s integrated with uv, an advantage is one less tool to juggle.
- _ea1k 1y agoI've been playing around with go quite a bit lately, and now think that being bundled is actually a really serious advantage. Being able to just to "go fmt", without needing any additional tools, is a really great thing about the language. The only danger that I can see (and it is a big one) is that Python doesn't have the same kind of consistency that go does. It might end up failing just because so many people use Python in such different ways.
- worldsayshi 1y agoYes, one advantage is that tools/editors will start to standardize on it. I quite enjoyed the seamless experience of go formatting as I started learning go. It felt like one less thing to worry about. Just worked.
- mintplant 1y agoWell, the same functionality used to be bundled into rye before the switch to uv. I appreciate having one less dependency to declare again.
- chippiewill 1y agoI've been waiting. uv is trying to deliver the same experience as cargo does for rust. Just install a single CLI and it's the only tool you have to worry about when working on your Python project. There's nothing wrong with `ruff format`, but it's nice to have `uv` as a universal entrypoint.
- Calavar 1y agoWhy does a package manager need a formatter at all?
- kristjansson 1y agoIs cargo just a package manager? Go?
- kevmo314 1y agoAm I just a package manager?
- tsongas4 1y agoYou pass butter
- Calavar 1y agoCargo is a hybrid of package manager and build system. So it has frontend fommands wrapping a formatter and loads of other stuff, like code linters, as part of the build system. I've used cargo to build projects even when they have no dependencies and I don't plan to bundle them up and publish them as crates. I don't know much about go.
- Hamuko 1y agouv is also a package manager and a build system. https://docs.astral.sh/uv/concepts/build-backend/ https://docs.astral.sh/uv/concepts/build-backend/
- RossBencina 1y agoThanks for pointing that out. This is news to me. uv has been on my radar for a while and was considering switching to it as a better dependency manager. I didn't realise that it had ambitions beyond being "a better pip." At face value this is a real turn-off. Definitely violates the "do one thing and do it well" principle and puts it squarely in the "does complicated things that I want to avoid" (like poetry) category.
- rcarmo 1y agoThis feels like feature creep. I've been using uv more and more over the past year (mostly because I work with projects that use it) and although I like it and recognize the advantages, it is still not my first choice, and this kind of thing isn't going to help that...
- actinium226 1y agoWhat is your first choice?
- rcarmo 1y agoPip, because it introduces no third-party software. This is critical in regulated industries.
- NeutralCrane 1y agoWhat is “third party” here? Pip and UV are both external dependencies used to install other external dependencies. What makes them different from each other, or from the other dependencies you are using them to install?
- rcarmo 1y agoIt ships with your certified Linux distro. Period.
- actinium226 1y agoOk, so why not just use pip to install uv?
- actinium226 1y agoOh boy, you're gonna have a really bad day when you figure out what "pip install" does.
- deleted 1y ago[deleted]
- alkh 1y agoI enjoy using uv a lot but am getting afraid that it is getting bloated for no reason. For ex., the number of niche flags that a lot of subcommands support is very high + some of them seemingly achieve the same result(uv run --no-project and uv run --active). I'd rather them working on improving existing tools and documentation than adding new (redundant) functionality
- treyd 1y agoAre they baked into the main executable or are they separate binaries (a la apt, cargo, etc)?
- charliermarsh 1y agoIt's a separate binary -- we install Ruff if you invoke `uv format`. So if you don't invoke `uv format`, there's no impact on the binary size, etc.
- HelloNurse 1y agoBut, for example, my project already uses Ruff, and I have to worry about having a "managed" extra copy of Ruff that subtly alters the normal functioning both of "uv tool run" and of ruff itself.
- zanie 1y agoIt's intentionally distinct from the `uv tool` interface — it won't change `ruff` or `uv tool run` behaviors.
- benreesman 1y agoIt's really difficult to do Python projects in a sound, reproducible, reasonably portable way. uv sync is in general able to build you only a package set that it can promise to build again. But it can't in general build torch-tensorrt or flash-attn because it has no way of knowing if Mercury was in retrograde when you ran pip. They are trying to thread a delicate an economically pivotal needle: the Python community prizes privatizing the profits and socializing the costs of "works on my box". The cost of making the software deployable, secure, repeatable, reliable didn't go away! It just became someone else's problem at a later time in a different place with far fewer degrees of freedom. Doing this in a way that satisfies serious operations people without alienating the "works on my box...sometimes" crowd is The Lord's Work.
- amanzi 1y agoIt wouldn't surprise me if the ruff featurset eventually gets integrated into uv and ty. The linting seems to better suited to ty which would be able to provide more intelligent linting since it understands the codebase better. And the formatting seems better suited to uv since that's all about managing your project.
- IAmLiterallyAB 1y agoty is already in the same repo as ruff, so integrating seems likely
- calmbonsai 1y agoI see zero need for this given that excellent time-testing code formatting tooling options are already available. This smacks of feature-creep and I won't be incorporating it into any pipelines for the foreseeable future.
- nikisweeting 1y agoYou realize this is just a shortcut for one of those "time-tested" options, right? ruff format is already widely used.
- calmbonsai 1y agoBut why bundle? It makes zero design sense to me.
- nikisweeting 1y agogo fmt, cargo fmt, deno fmt, dart format, etc. it's hardly a new trend to ship a formatter with an all-in-one package manager for a language. It's convenient for new projects to not have to set up an additional dependency.
- RossBencina 1y agoI'm not clear on this, but does it not also setup a backward dependency between the new project and uv?
- nikisweeting 1y agoOh no I would hate to have to depend on my package manager and build system
- charliermarsh 1y ago`uv format` is just a front-end for `ruff format`. It isn't introducing a new formatter to the ecosystem or anything like that.
- thanhhaimai 1y agoI'd rather `ruff` being merged with `ty` instead. `uv` for me is about package / project manager. It's not about code style. The only time `uv` should edit a code file is to update its dependencies (PEP 723). On the other hand, both `ruff` and `ty` are about code style. They both edit the code, either to format or fix typing / lint issues. They are good candidates to be merged.
- WD-42 1y agoThey are mimicking Rust's cargo, which has `cargo fmt`
- munificent 1y agoAlso `go fmt` and `dart format`.
- Biganon 1y ago> They are mimicking Rust's cargo Cargo cargo cult?
- xdennis 1y agoIt's not a cargo cult if it actually works.
- petcat 1y agoDoesn't cargo just have a subcommand plugin system? Or is fmt actually hard-coded into the cargo code? I prefer the plugin system. I don't like god programs like what the npm monstrosity became.
- woodruffw 1y agocargo has an external subcommand system, but it also has "blessed" (my word choice) external subcommands that are typically bootstrapped via Rust toolchain components. This makes them pretty analogous to what uv does here with `uv format`, in my opinion.
- tomrod 1y agoOh neat. I wonder if they are integrating everything into uv then.
- rickydroll 1y agoWhen is UVA going to start handling email?
- vovavili 1y agoCan't you just do this? Why bundle? uvx ruff format .
- dkdcio 1y agothat’s a bit slow typically
- IshKebab 1y agoBecause that's a way less obvious command.
- ethan_smith 1y agoThe native integration offers persistent configuration, caching, and project-aware behavior that uvx (which just creates an ephemeral venv) doesn't provide.
- vovavili 1y agoI would imagine that a person who is seriously concerned about any of that would just install a standalone version of ruff. Given the existence of uvx, this addition seems like feature creep to me.
- tspng 1y agoIt's not bundled. `uv format` will use the `ruff` binary and more or less run `uvx ruff format` behind the scene.
- cwyers 1y agoThe HN commenters in a nutshell: ruff: Aww, you're sweet! uv format: Hello, human resources? Meanwhile, they provide identical functionality. (`Under the hood, it calls Ruff’s formatter to automatically style your code according to consistent standards.`)
- zahlman 1y agoMemes aside, the conceptual grouping of tools does matter.
- 0cf8612b2e1e 1y agoBut why do it? Keep each tool focused on its specific goal. Will there eventually be a call to deprecate ruff because it already exists in uv? Both tools are still evolving enough that I would not want their individual release cycles to impact each other.
- charliermarsh 1y agoGood questions. I don't think we'd ever deprecate Ruff because `uv format` exists, and adding `uv format` won't have any impact on Ruff's release cycles or development. The analogy would be to Cargo: `cargo fmt` just runs `rustfmt`, but you can also run `rustfmt` separately if you want. A lot of users just want a simpler experience. They want to install uv, run `uv run` to run their project, `uv format` to format it, etc. The idea here is to experiment with providing that functionality and see if folks find it useful. Maybe they won't want it! It's experimental :)
- replygirl 1y agoi expect to hear "hey replygirl, can we upgrade from ruff to uv format?" from 5 of my coworkers in the next month, and "what's the difference between ruff and uv format?" from another 10. per interaction i expect 2 minutes of reading and explaining, plus an average 5 minutes listening to the other party wax philosophical. so the convenience costs my job $400
- cwyers 1y ago
- nikisweeting 1y agoAs long as it does `ruff check --fix; ruff format` internally and not just one or the other, then I'm happy. Tired of running 2 commands every time.
- 0cf8612b2e1e 1y agoAlso needs to make sorting imports a default.
- nikisweeting 1y agoand stop removing extra leading whitespace before inline comments (#7684)
- zahlman 1y agoFWIW, you can pretty easily script this sort of thing. In Bash it can even be a function added to your ~/.bashrc .
- nikisweeting 1y agoOf course we already have it automated in pre-commit checks, I'm just mildly annoyed that it's two different commands in the first place (though I'm sure there is some good historical reason). The distinction/ordering between them isn't really clear as a user, I just want "find and fix everything you can, error if you cant autofix" every time.
- IshKebab 1y agoJust so you know, when people say "I hate this paper cut", they aren't really asking how they can put in extra work to get around it. Obviously hey can add some alias to ~/.bashrc... on every computer he uses. The complaint is that he has to do that in the first place. It's still annoying.
- zahlman 1y agoI prefer having things this way, because it respects the fact that I may have a different idea from the next person about how the pieces should be put together.
- CoderJoshDK 1y agointernally, they are working on ty and some of its logic is going to have to call out to uv. I imagine that things like this is being built to experiment with some of the connections and interfaces between tools. The astral team is pretty responsive to questions and feedback. If this type of change concerns you (as an actual user of the tool) please reach out to them. My big thing right now is integrations with workspaces. And if special case commands is the answer,,, well not ideal but I’ll take it.
- Kwpolska 1y agoWhy would a type checker need to call a package manager?
- IshKebab 1y agoThis is obviously a great move. I don't know why so many commenters here are against making things better. "Can't you just do <this slightly worse thing> already?". Well yes. But it's slightly worse.
- Spivak 1y agoI think the biggest thing is that it doesn't seem to support other formatters. If my project uses black I don't get to have uv format work for me.
- nikisweeting 1y agofwiw `ruff format` includes nearly all the black rules by default / supports a superset of black's options https://astral.sh/blog/the-ruff-formatter https://astral.sh/blog/the-ruff-formatter
- nmca 1y agoright, but you definitely shouldn’t be using any other formatter than ruff and this helps with that
- nickserv 1y agoRuff pretty much follows the same rules as black, and can be made identical with some options tweaking. So when I moved several projects from black to ruff, there were no changes made to the code.
- LtWorf 1y agoI find this hard to believe since black doesn't even have the same rules as black's next version and I never changed version without something being reformatted. Also because of fun, if you reformat again with the older version it won't go back to as it was before :)
- LtWorf 1y agoThanks for the downvotes. Really shows that you have no familiarity with black.
- infamia 1y agoMixing a package manager, (which is needed for prod package installs) with dev-only tooling is analogous to an "attractive nuisance" (not that I'm saying anyone is a child mind). I know Go and Rust do it, but thinking from first principles, it sounds like a bad idea.
- kstrauser 1y agoIt really does sound like a bad idea, and now that I've used cargo a lot, I want a lot more of those bad ideas in my life. Seriously, if uv becomes to Python what cargo is to Rust, the developer experience for Python is going to be vastly better than before. I've been writing Python professionally for more than 25 years, and getting paid to work around it's crummier parts, and I'm thrilled to be able to throw away all that knowledge and just use uv.
- time0ut 1y agoCool. I want a do it all python tool. Ruff is great. Having it out of the box with uv is great. Less crap to mess with. I haven’t tried ty yet, but looking forward to not having to mess with pyright. Wary of the vc funded aspect though…
- dirtbag__dad 1y agoHaving worked with many python repos, that didn’t know any better to follow conventions or that the tooling ecosystem has meaningful options, I am ecstatic to see formatting and linting be a first class feature of the modern python experience. I know this is a hot take, but so much headache saved down the road to “force” this stuff up front.
- RossBencina 1y agoDoes it have to be "force" stuff up front? Surely there are less violent ways to ease new developers into conventions and tooling than the dictatorial approach.
- deleted 1y ago[deleted]
- NeutralCrane 1y agoThis approach is “violent”?
- wiseowise 1y agoFoot massage and blowjob over coffee? Do you even read you write? Work is about efficiency, not catering to every special snowflake.
- Cogito 1y agoI anticipate this will make it much easier for me to get my little team of actuaries formatting their code. The impact of uv on our onboarding and utilisation of python has already been huge, and any way I can easily improve their code hygiene is a good one. Yes, I could get them to use ruff standalone, or set up pre-commit hooks etc for them, but the simple mental model that comes from `uv <do something>` is really beneficial. Will have a play around with this and see how it goes. Would love to be able to hook into other formatters too, but not sure if that's easy or even possible already. For example, I would love it if `uv format` also formatted our SQL/dbt models.
- kstrauser 1y agoAt that point I think you start wanting Makefiles, or preferably IMO justfiles. Then you can run `just format` and have it format your combined Python/SQL/Bash/TypeScript project.
- lxe 1y agouv is taking bun's route and just adding features "willy-nilly"?
- senand 1y agoI use black and I like it. Where does ruff differ in terms of formatting?
- notpushkin 1y agoIt’s mostly similar, just a bit more consistent: https://docs.astral.sh/ruff/formatter/black/ https://docs.astral.sh/ruff/formatter/black/
- VeejayRampay 1y agoand speed, it's way way faster
- pnt12 1y agoI switched to ruff for the great linting. When they introduced a formatter, I gave it a try and: - got similar results - but runs faster - I could delete one dev dependency Given black's motto (any color as long as it's black), now I pick ruff and go with whatever formatting it produces.
- Biganon 1y agoEvery single time someone asks "why?" in this thread, the answer is "well cargo does it too". Not sure what to think about that.
- Iridescent_ 1y agoNot all devs have very deep knowledge of the ecosystem, some will get to use a tool only if it's part of the default set of tools they're provided. Plus, it saves on memorizing a name if you only use python once in a long while. There just isn't much of a reason not to.
- hobofan 1y agoAs someone who has worked on package managers quite a bit in the past, and for that examined prior art across package managers, that's because Cargo is doing things very well on almost all fronts. Partially because of their RFC process and partially because they have the luxury of being a "newer" package manager that doesn't have to live with ecosystem baggage going many decades back. There are/were definitely some weaker spots with Cargo (e.g. private registry support was meh for some time), but if one were tasked to build a package manager and were only allowed to pick a single one to take inspration from, Cargo is definitely the way to go.
- hyperman1 1y agoI landed in python last year, the pre uv time for me. The language itself is fine, but the project organization sucked. What kind of folder structure to choose? Do I use pip, env, pipenv, poetry? How to format, and with which settings? I've read tens of blog post, all giving different choices, all equally reasonable, all having some downsides. The great thing about uv is not that their choices are best, it's that the choices have been made, the gordian knots cut. There is a good enough standard to build upon. Future python projects will look a lot more like each other, and less time will be wasted on organizational minutiae.
- wiseowise 1y ago“Cargo does it” is a shortcut for “yes, it’s fucking good”.
- renewiltord 1y agoExcellent. Love it. Keep on at it, guys.
- vivzkestrel 1y agoAnyone from uv reading this? please scrap this thing, there is a separate formatter ruff that anyone ll use if they want a formatter. please do not complicate stuff. ruff ll come with its own configuration. this will have its own config at some point. potential rule clashes, ignore plugins. please do not go in this direction
- jgeralnik 1y agoRuff and uv are written by the same company, astral
- agos 1y agothis command is more or less an alias for calling Ruff, it's quite explicitly written in the article
- HelloNurse 1y agoA clean alternative: automatically adding ruff as a tool when creating a new pyproject.toml file, with a command line flag to opt out for the minority who know they don't intend to use ruff. For the majority of users, simplicity is worth an accidental download.
- pjmlp 1y agoAs someone using formating tools on IDE since the 1990's and UNIX indent as well, I really don't get the hype for formatters. Even moreso in the LLM age of tooling and coding agents.
- jillesvangurp 1y agoIt's a minor issues in teams where people use inconsistent formatting. This causes needless complications when merging code and additional work related to resolving conflicts that shouldn't exist to begin with. Some languages (Go, Rust) have essentially solved this issue to the point where blindly running the formatters these languages have solves the problem and is uncontroversial. Sadly, the language of choice for me (Kotlin) has a big unaddressed problem here where what the IDE does for formatting and what independent formatting tools do are two things that are hard to align for mostly the historical reasons that what the IDE does is fragmented over different bits of code that don't listen to the same configuration. There is a lot of variation in preferences for indentation, where to put new lines, line length, what order to put imports in, which imports to use wildcards for (if any at all), etc. Particularly imports are a problem because the bit of code that organizes imports is separate from the bit of code that formats code in the IDE. The common solution of using IDE plugins to work around this is a bit of a kludge. The proper solution would be a more sane way to just make the IDE externally configurable so that build tools can make the IDE do exactly the same as what they do without requiring users to manually configure their IDEs just right by installing plugins or fiddling with configuration. This stuff should not be user controllable if there is a build file that defines the proper formatting. Coding agents actually add to this problem. Because getting those to stick to formatting conventions is tricky. Unless you have tools that just fix that properly. So good change in uv and probably something I'd be using on my next python project (I do those once in a while).
- pjmlp 1y agoEasily solvable with SCM pre-commit hooks, at very least since CVS days. All major IDEs allow to save the format configurations, which can be stored on the repo alongside the code as the team official's configuration. Coding agents solve this problem in that eventually all programming languages will be as relevant as mastering Assembly is nowadays. I already do stuff in low code/no code, that in the past I would be manually writing code to sort out those issues. In the meantime, you can also ask them to format generated code in whatever way one feels like it.
- lysecret 1y agoI love ruff and I love uv and I’m sure I will ty as soon as it’s ready but please keep them separate.
- sitzkrieg 1y agothe python crowd sure likes reinventing formatters every handful of years huh
- deleted 1y ago[deleted]
- amadeuspagel 1y agoWell that title was a lot more interesting without the parentheses that HN automatically removed.
- zelphirkalt 1y agoI hope for uv's sake, that the formatting is always optional and that it is not "opinionated" (read: half asses and no way to configure it), but configurable. And if we get into that, well, it's a whole different tool, that they are trying to put into uv. Seems like a bad idea.
- btbytes 1y agoThe new `uv format` is just a shortcut for `uv run --with ruff ruff`.
- vorgol 1y agoWe can argue if it's a feature feature creep and if it's the right choice of formatter. But let's appreciate that this will increase the use of formatters astronomically (or part thereof), which is an excellent thing.
- iandanforth 1y agoI recognize that uv is very popular but it still feels faddish. I don't want my package manager to format anything. I don't want my package manager to lint anything. I don't want my package manager to `run` anything. I don't want my package manager to even manage environments (yes yes this one is a bit extreme). I want to have single purpose tools that work together well. This way I can have a flexible, composable toolchain that can both adapt to me and my specific project. For example, I like conda, pip, ruff, mypy, flake8. Someone else might like venv, pip, black, pyright, flake8. This is the heart of the Unix Philosophy and I feel like it's a philosophy each software community has to reject, suffer due to that rejection, and re-adopt on a regular basis. I'd rather we skip the suffering bit.
- stakhanov 1y agoA Foolish Consistency is the Hobgoblin of Little Minds https://peps.python.org/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds https://peps.python.org/pep-0008/#a-foolish-consistency-is-t... I wonder how a code formatting tool is going to implement that.
- nilslindemann 1y agoIs it just me, or do other people also have the impression that a lot of sock puppets are advertising for the Astral company on the internet? That said, I may not have installed super complex packages, but for years now I have never ever experienced one situation (on Windows, ok) where pip did not just work. For me, pip is the way. I also have the Python folder be a Git repo, and every `pip install` is a commit, and every project is a branch. Why so difficult?