google / pyink Goto Github PK
View Code? Open in Web Editor NEWThis project forked from psf/black
Pyink, pronounced pī-ˈiŋk, is a Python formatter, forked from Black with a few different formatting behaviors.
License: MIT License
This project forked from psf/black
Pyink, pronounced pī-ˈiŋk, is a Python formatter, forked from Black with a few different formatting behaviors.
License: MIT License
Hello, as someone who was frustrated by some of Black's limitations, I greatly appreciate the work done by PyInk.
I would like to request support for pre-commit, which is a popular tool and is supported by Black.
I believe that creating a pre-commit hook to allow automated PyInk formatting would greatly increase the popularity and utility of the project while being very easy to implement.
Thank you in advance.
The package requirements show support for >=3.7.
However, running on 3.8 results in:
pyink --version
Traceback (most recent call last):
File "/cygdrive/c/cygwin/usr/local/bin/pyink", line 5, in <module>
from pyink import patched_main
File "/usr/local/lib/python3.8/site-packages/pyink/__init__.py", line 823, in <module>
lines: Optional[Collection[tuple[int, int]]] = None,
TypeError: 'type' object is not subscriptable
It is likely that tuple[int, int]
should be Tuple[int, int]
?
Black released https://github.com/psf/black/releases/tag/24.4.2 last week addressing f-string parsing. Can you pull in these changes?
$ pyink --version
pyink, 24.3.0 (compiled: no)
Python (CPython) 3.12.2
Given a simple test file (pyink-test.py):
def main():
d = {"foo": "bar"}
print(f"{d['foo']}")
if __name__ == "__main__":
main()
Results in:
$ /path/to/my/venv/bin/python -m pyink -v pyink-test.py
Identified `/path/to/my/venv` as project root containing a .git directory.
Using configuration from project root.
target_version: ['py312']
Found input source: "pyink-test.py"
Traceback (most recent call last):
File "/path/to/my/venv/lib/python3.12/site-packages/pyink/__init__.py", line 950, in reformat_one
if changed is not Changed.CACHED and format_file_in_place(
^^^^^^^^^^^^^^^^^^^^^
File "/path/to/my/venv/lib/python3.12/site-packages/pyink/__init__.py", line 992, in format_file_in_place
dst_contents = format_file_contents(
^^^^^^^^^^^^^^^^^^^^^
File "/path/to/my/venv/lib/python3.12/site-packages/pyink/__init__.py", line 1124, in format_file_contents
check_stability_and_equivalence(
File "/path/to/my/venv/lib/python3.12/site-packages/pyink/__init__.py", line 1098, in check_stability_and_equivalence
assert_equivalent(src_contents, dst_contents)
File "/path/to/my/venv/lib/python3.12/site-packages/pyink/__init__.py", line 1592, in assert_equivalent
raise ASTSafetyError(
pyink.parsing.ASTSafetyError: INTERNAL ERROR: Black produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_qpj__4sq.log
error: cannot format pyink-test.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_qpj__4sq.log
Oh no! 💥 💔 💥
1 file failed to reformat.
Upgrading Black in the same venv, however:
$ black --version
python -m black, 24.4.2 (compiled: yes)
Python (CPython) 3.12.2
Black 24.4.2 works:
$ /path/to/my/venv/bin/python -m black -v pyink-test.py
Identified `/path/to/my/venv` as project root containing a .git directory.
Using configuration from project root.
target_version: ['py312', 'py313']
Found input source: "pyink-test.py"
pyink-test.py wasn't modified on disk since last run.
All done! ✨ 🍰 ✨
1 file left unchanged.
The lines in my settings.json
don't match the ones in the instructions.
My environment: Windows 11, VS Code 1.74.3 with a Python 3.10 venv in WSL2. I ran pip install pyink
in the virtual_env and selected Black as the formatter for Python.
I changed the settings using the VS Code GUI and checked settings.json
where I now have:
"python.formatting.provider": "black",
"python.formatting.blackPath": "pyink"
And the formatter works on one of the snippets where Pyink formats differently from Black.
Maybe there are multiple valid ways to specify it in settings.json
or maybe VS Code changed. Thought you might want the updated values for your docs.
Hello, I have tried out the new PyInk pre-commit, and I have found a difference with the behavior in Black.
For multiline docstrings in file definitions, PyInk appears to move the last triple double quotes to the end of the last line instead of giving them their own line.
For example,
"""This is my file.
It works like this.
"""
import something
becomes as follows.
"""This is my file.
It works like this."""
import something
We use 2 blank lines between class methods for readability, as this visually separates one method from another without re-using the same separation as within a method. It would be great if pyink enabled this via a configuration option.
Right now, pyink with --pyink-indentation 2
will reformat code like this:
def func(lots_of_arguments, for_this_function_call, so_that_it_has_to, span_multiple_lines, to_fit_them_all):
pass
To this:
def func(
lots_of_arguments,
for_this_function_call,
so_that_it_has_to,
span_multiple_lines,
to_fit_them_all,
):
pass
So the 2-space preference was applied to the block indentation of the function's body, but not to the indentation within the parentheses, which still uses 4 spaces like vanilla black.
My guess is that most people who prefer 2-space indentation for the former also prefer it for the latter, yet there doesn't seem to be a way to achieve such formatting with pyink as it is now.
Would it be possible to implement this?
Just for your info, I created a feature request to support pyink in VSCode: microsoft/vscode-python#20099
Having auto-formatting for open-source Google projects will be a huge improvement for external contributors!
When I use --pyink-lines
on a file, it works as expected, it reformats required lines. But when I read the same file from stdin
, it reformats everything we pass to it.
$ echo 'if True: print("CHANGED TEXT")\nif False: print("there")' > test.py
$ cat test.py
if True: print("CHANGED TEXT")
if False: print("there")
$ pyink test.py --pyink-lines=1-1 --quiet && cat test.py
if True:
print("CHANGED TEXT")
if False: print("there")
It reformatted only the lines we issued. When I try to use stdin
though, it reformats everything.
$ echo 'if True: print("CHANGED TEXT")\nif False: print("there")' > test.py
$ cat test.py
if True: print("CHANGED TEXT")
if False: print("there")
$ cat test.py | pyink --quiet --pyink-lines=1-1 -
if True:
print("CHANGED TEXT")
if False:
print("there")
As you can see, it reformats everything.
I use vim's format feature which replaces a piece of text with externals program's output, which in this case is pyink
. Right now, it's not possible to use pyink with vim because it changes the file in-place. Is it possible to make pyink
print to stdout
instead of changing the file in-place?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.