Skip to content
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

[feature reques] double lines between methods #39

Open
derVedro opened this issue Dec 21, 2024 · 0 comments
Open

[feature reques] double lines between methods #39

derVedro opened this issue Dec 21, 2024 · 0 comments

Comments

@derVedro
Copy link

Thanks

First for all thanks for this fork. While black does a good job, the lack of some configuration freedom and the project's unwillingness to move away from the accepted dogmas towards a bit of greyness is discouraging to me. All the more I'm glad to find a different approach in this fork.

The Problem

In my projects, I often group logical code pieces inside functions and methods with single empty lines. The eyes cling better to such groups. Top-level functions are separated by two lines, so all the code looks well structured to me. Unfortunately, the PEP8 recommendation is that methods should be separated by only one line. I don't like the result especially if methods get bigger and more complex.

I have two feature suggestions for the project, the implementation of either of them I would be very happy with:

1. --force-double-lines-between-methods

The formatter preemptively insert two empty lines between methods.

2. --keep-double-lines-between-methods

My proffered behavior, the code formatter takes into account the number of empty lines between methods left by user and not interfere if there are two or one.

Attempted solution

For the moment the task did not seem so difficult to me. I prototyped the first feature relatively quickly in this branch. Empty lines handling is done in lines.py in top-down manner. This is a bit of a problem, because the decision to surround a method with empty lines is made only when the method is declared, and the empty lines are inserted just before the method. There are quite rare situations when there is other code block in the class besides methods, and then this approach gives unsatisfactory results. Here is a short abstract example (I don't know why some one would prefer to do something like this, but it's valid code):

input: current output: disired output:
class Weird:

    def __init__(self, x):
        self.x = x

    def __eq__(self, other):
        return self.x == other.x

    def __str__(self):
        return str(self.x)


    try:
        foo = 42 + "bar"
    except TypeError:
        print("baz")
class Weird:

    def __init__(self, x):
        self.x = x


    def __eq__(self, other):
        return self.x == other.x


    def __str__(self):
        return str(self.x)

    try:
        foo = 42 + 'bar'
    except TypeError:
        print('baz')
class Weird:

    def __init__(self, x):
        self.x = x


    def __eq__(self, other):
        return self.x == other.x


    def __str__(self):
        return str(self.x)


    try:
        foo = 42 + 'bar'
    except TypeError:
        print('baz')

The second feature is more difficult to implement, because formatter should track the state of the second last line. It also means there will be more divergence from black and the feature won't be as atomic anymore.

Thoughts and questions

I don't know if the cercis project is interested in this idea, I think more customization possibilities would be beneficial. I would love to see this feature in cercis. I would also appreciate any help in implementation and testing. With the last I have some troubles to find the right entry points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant