Skip to main content

A Hy pretty-printer / code formatter / beautifier.

Project description

🦑 Beautifhy

A Hy beautifier / code formatter / pretty-printer / linter.

Probably compatible with Hy 1.0.0 and later.

Install

$ pip install -U beautifhy

The pygments style may be modified with the environment variable HY_PYGMENTS_STYLE. This sets the name of a pygments style to use for highlighting. Defaults to lightbulb.

Usage: pretty-printer and syntax highlighter

From the command line, to pretty-print the file core.hy:

$ beautifhy core.hy

gives the output

(import toolz [first second last])

(defmacro defmethod [#* args]
  "Define a multimethod (using multimethod.multimethod).
  For example, the Hy code

  `(defmethod f [#^ int x #^ float y]
    (// x (int y)))`

  is equivalent to the following Python code:

  `@multimethod
  def f(x: int, y: float):
      return await x // int(y)`

  You can also define an asynchronous multimethod:

  `(defmethod :async f [#* args #** kwargs]
    (await some-async-function #* args #** kwargs))`
  "
  (if (= :async (first args))
    (let [f (second args) body (cut args 2 None)]
      `(defn :async [hy.I.multimethod.multimethod] ~f ~@body))
    (let [f (first args) body (cut args 1 None)]
      `(defn [hy.I.multimethod.multimethod] ~f ~@body))))

(defn slurp [fname #** kwargs]
  "Read a file and return as a string.
  kwargs can include mode, encoding and buffering, and will be passed
  to open()."
  (let [f (if (:encoding kwargs None) hy.I.codecs.open open)]
    (with [o (f fname #** kwargs)]
      (o.read))))

(defmacro rest [xs]
  "A slice of all but the first element of a sequence."
  `(cut ~xs 1 None))

To apply syntax highlighting (no pretty-printing), do

$ hylight core.hy

You can use stdin and pipe by replacing the filename with -:

$ beautifhy core.hy | hylight -

which will pretty-print core.hy and then syntax highlight the output.

To convert python code to Hy (using py2hy), autoformat, then apply syntax highlighting, do

$ pip3 install py2hy
$ python3 -m py2hy some_code.py | beautifhy - | hylight -

Usage: linter

hylint checks Hy source for syntax errors and common issues.

$ hylint myfile.hy

With --style / -s, also runs opinionated style checks (camelCase names, missing docstrings, earmuff variables):

$ hylint --style myfile.hy

With --error-only / -e, only errors are shown (suppresses warnings and info):

$ hylint --error-only myfile.hy

Reads from stdin with -:

$ cat myfile.hy | hylint -

Exit code is 1 if any errors are found, 0 otherwise. Suitable for CI.

Lint rules (always on):

  • (defn f (x) ...) — use [] for parameter lists, not ()
  • (defn f [...] (do ...)) — remove redundant do
  • (fn [...] (do ...)) — remove redundant do
  • (when cond (do ...)) — remove redundant do
  • (if cond (do ...)) — use (when cond ...)
  • (do expr) — redundant single-expression do
  • (+ x 0), (* x 1) — identity arithmetic
  • (+ x 1)(inc x), (- x 1)(dec x) (from hyrule)

Style rules (--style):

  • camelCase function names — use kebab-case
  • Missing docstrings on defn/defmacro
  • Trailing commas in parameter lists
  • Earmuff variables (*var*) — use UPPER_SNAKE or kebab-case

Acknowledgements

The whole library uses pygments. The autoformatter relies on polymorphic dispatch provided by multimethod.

Docs

The docstrings are not bad. Otherwise, try clicking below.

Ask DeepWiki

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beautifhy-1.3.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beautifhy-1.3.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file beautifhy-1.3.0.tar.gz.

File metadata

  • Download URL: beautifhy-1.3.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for beautifhy-1.3.0.tar.gz
Algorithm Hash digest
SHA256 c10c0ca286f64e7bf51620fb621cc4a897e36da4913ac7a35f638f4db1934eaa
MD5 4a647a548b614f9df51ae6d6c74118de
BLAKE2b-256 b42108dd9cf1d1846e3dd0dab365873649b29c2f4245d3b249550a735c01af78

See more details on using hashes here.

File details

Details for the file beautifhy-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: beautifhy-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for beautifhy-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bae7f89d508766dc0316788ba1974370021dbff2e0937515451cf351873acc31
MD5 b32a6b4810186b84f0b61e4f5fd95ff1
BLAKE2b-256 40c66d69993cbe6e3cf0ff193e01d42cd6b8d0f2682574eaf1e4e2da73b2121d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page