I like like the aesthetic of assignment statements.
Seeing code formatted in this way brings me joy, so I'm writing a formatter in the hope that I can use it in some projects of mine. Ideally, it will extend something like black
's formatting, which I by-and-large find to be great.
# Before
foobar = baz
foo = foobar
# After
foobar = baz
foo = foobar
# Before
self.first_linear = nn.Linear(config.hidden_size, config.hidden_size, bias=False)
self.second_linear = nn.Linear(config.hidden_size, config.num_labels, bias=False)
self.third_linear = nn.Linear(config.hidden_size, config.num_labels)
# After
self.first_linear = nn.Linear(config.hidden_size, config.hidden_size, bias=False)
self.second_linear = nn.Linear(config.hidden_size, config.num_labels, bias=False)
self.third_linear = nn.Linear(config.hidden_size, config.num_labels)
# Before
def forward(
self,
input_ids: torch.Tensor = None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
): ...
# After
def forward(
self,
input_ids : torch.Tensor = None,
attention_mask = None,
token_type_ids = None,
position_ids = None,
head_mask = None,
inputs_embeds = None,
labels = None,
output_attentions = None,
output_hidden_states = None,
return_dict = None,
): ...
# Before
def f(
attention: torch.Tensor,
labels: np.ndarray,
logger: logging.Logger,
) -> tuple[torch.Tensor, bool]: ...
# After
def f(
attention : torch.Tensor,
labels : np.ndarray,
logger : logging.Logger,
) -> tuple[torch.Tensor, bool]: ...
Add the following to your .pre-commit-config.yaml
file. Note: compatibility with other linters /
formatters is not guaranteed, please create an issue for any conflicts you encounter.
repos:
- repo: https://github.com/JosephSBoyle/joy_formatter.git
rev: latest
hooks:
- id: align-assignments
- Git clone this repo.
- Copy the full path to
joy.py
- Install the 'custom local formatters extension'
- Open the
settings.json
file by pressing ctrl+shift+p and selecting "Open user settings json". - Add these key-value pairs.
"python.autoComplete.extraPaths": [],
"python.analysis.extraPaths": [],
"customLocalFormatters.formatters": [
{
"command": "python <PATH TO YOUR joy.py SCRIPT HERE>",
"languages": ["python"]
}
],
- Replace
<PATH TO YOUR joy.py SCRIPT HERE>
with the path from step #2!
The ./rt
command is used as a shorthand to run the tests, passing any flags to pytest
.
Sort of how some people use a test
command in their makefiles and invoke make test
to run their tests.
If any code is malformatted by this tool, please open an issue with the input and output that you expected / received and I'll do my best to take a look.