-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
☂️ Formatter: Support formatting of embedded code #8237
Comments
I think we should keep the linter in mind while designing the infrastructure as, and I'm not 100% sure but it's mainly my intuition from working on the Notebook support, it's more than likely than we get the formatter support for free. Free in the sense that it'll require considerably less effort on the formatter side once the infrastructure is in place. |
I agree, but I wanted to keep this issue scoped. The linter and formatter likely have similar requirements and need similar infrastructure, but with slight nuances. |
potential duplicate of #3792 |
https://github.com/adamchainz/blacken-docs does this with black and markdown, ReST, and LaTeX. You can see the block types supported there. (python, pycon, etc). Even better, maybe the block types could be configurable, say setting |
Hello, I would be keen to see doctests formatting, e.g.: def least_square_mapping_MoorePenrose(
y: ArrayLike, x: ArrayLike
) -> NDArrayFloat:
"""
Compute the *least-squares* mapping from dependent variable :math:`y` to
independent variable :math:`x` using *Moore-Penrose* inverse.
Parameters
----------
y
Dependent and already known :math:`y` variable.
x
Independent :math:`x` variable(s) values corresponding with :math:`y`
variable.
Returns
-------
:class:`numpy.ndarray`
*Least-squares* mapping.
References
----------
:cite:`Finlayson2015`
Examples
--------
>>> prng = np.random.RandomState(2)
>>> y = prng.random_sample((24, 3))
>>> x = y + ( prng.random_sample( (24, 3)) - 0.5) * 0.5
>>> least_square_mapping_MoorePenrose(y, x) # doctest: +ELLIPSIS
array([[ 1.0526376..., 0.1378078..., -0.2276339...],
[ 0.0739584..., 1.0293994..., -0.1060115...],
[ 0.0572550..., -0.2052633..., 1.1015194...]])
"""
y = np.atleast_2d(y)
x = np.atleast_2d(x)
return np.dot(np.transpose(x), np.linalg.pinv(np.transpose(y))) Ruff currently does not format anything inside Cheers, Thomas |
Hey @KelSolaar Docstring code block formatting is supported but off by default. You can enable it in your settings using |
Hi @MichaReiser, I have it enabled but it does not seem to format the above. Cheers, Thomas |
The issue is that array([[ 1.0526376..., 0.1378078..., -0.2276339...],
[ 0.0739584..., 1.0293994..., -0.1060115...],
[ 0.0572550..., -0.2052633..., 1.1015194...]]) is not valid python syntax (because of the |
Right I see! So this particular code output is used as a doctest where any whitespace counts so it should NOT be formatted, ever. Ruff formatter should ignore those outputs fully which is what |
For that specific case, Ruff should only consider docstring lines starting with either |
That example should use the If no language is given (such code with a 4-space indent), I think it should be treated as whatever the default is (which IIRC might be Python). |
corresponding google-style doctest: def least_square_mapping_MoorePenrose(
y: ArrayLike, x: ArrayLike
) -> NDArrayFloat:
"""
Compute the *least-squares* mapping from dependent variable :math:`y` to
independent variable :math:`x` using *Moore-Penrose* inverse.
Examples:
>>> prng = np.random.RandomState(2)
>>> y = prng.random_sample((24, 3))
>>> x = y + ( prng.random_sample( (24, 3)) - 0.5) * 0.5
>>> least_square_mapping_MoorePenrose(y, x) # doctest: +ELLIPSIS
array([[ 1.0526376..., 0.1378078..., -0.2276339...],
[ 0.0739584..., 1.0293994..., -0.1060115...],
[ 0.0572550..., -0.2052633..., 1.1015194...]])
"""
y = np.atleast_2d(y)
x = np.atleast_2d(x)
return np.dot(np.transpose(x), np.linalg.pinv(np.transpose(y))) |
+1 to this - beyond |
Support formatting Python code embedded in other languages like:
The goal of this issue is not that we implement support for all these languages but to build up the infrastructure to run ruff (at least the formatter) on files that contain embedded python code and format it. Ideally, the infrastructure would, in the future, allow us to support arbitrary nesting:
Prettier and JetBrains code formatter do an excellent job at this.
Related:
ruff
tomarkdown
code blocks #3792The text was updated successfully, but these errors were encountered: