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

Add ability to exclude files like black's extend-exclude option #181

Closed
fizbin opened this issue Aug 25, 2021 · 4 comments · Fixed by #171
Closed

Add ability to exclude files like black's extend-exclude option #181

fizbin opened this issue Aug 25, 2021 · 4 comments · Fixed by #171
Assignees
Labels
duplicate This issue or pull request already exists maybe invalid? Can't reproduce, or seems already fixed, or need more information question Further information is requested
Milestone

Comments

@fizbin
Copy link

fizbin commented Aug 25, 2021

Short version: if I exclude files from being formatted with black's extend-exclude option in pyproject.toml, then darker should also not touch those files.

Longer version:

Consider a repo with three files in it:

testproj $ cat pyproject.toml 
[tool.black]
line-length = 100
target-version = ['py36']
extend-exclude = '''
DontReformatMe.py  # Put generated code in files named this
'''

[tool.darker]
line-length = 100
src = [
    ".",
]
revision = "main..."
extend-exclude = '''
DontReformatMe.py  # Put generated code in files named this
'''

testproj $ cat ReformatMe.py 
import sys

if len(sys.argv) >= 2:
    if sys.argv[1] == 'cat':
        print('meow')
testproj $ cat DontReformatMe.py 
import sys

if len(sys.argv) >= 2:
    if sys.argv[1] == 'cat':
        print('kitten')

Now do a git add * and git commit -m 'Initial commit', followed by black . and git commit -a -m 'Reformatted with black'. Now imagine that the time comes to add canine support to these files, so after making this obvious changes, you run darker before committing.

Now look as what git diff says:

testproj dtm$ git diff
diff --git a/DontReformatMe.py b/DontReformatMe.py
index d131e58..93d0c5f 100644
--- a/DontReformatMe.py
+++ b/DontReformatMe.py
@@ -1,5 +1,7 @@
 import sys
 
 if len(sys.argv) >= 2:
-    if sys.argv[1] == 'cat':
-        print('kitten')
+    if sys.argv[1] == "cat":
+        print("kitten")
+    if sys.argv[1] == "dog":
+        print("puppy")
diff --git a/ReformatMe.py b/ReformatMe.py
index 07f3639..f5f34cc 100644
--- a/ReformatMe.py
+++ b/ReformatMe.py
@@ -3,3 +3,5 @@ import sys
 if len(sys.argv) >= 2:
     if sys.argv[1] == "cat":
         print("meow")
+    elif sys.argv[1] == "dog":
+        print("woof")

Behold, darker has gone and reformatted the file called DontReformatMe.py, exactly as we told black not to. (And indeed, black won't touch the file if asked to reformat everything)

@akaihola
Copy link
Owner

akaihola commented Sep 1, 2021

I believe this has already been reported in #146 and fixed in #171, and this will be included in the upcoming 1.3.0 release. Could you try installing the development version from the master branch and verify that it behaves correctly?

Here's how to install from master:

pip install git+https://github.com/akaihola/darker.git@master

@akaihola akaihola added the duplicate This issue or pull request already exists label Sep 1, 2021
@akaihola akaihola linked a pull request Sep 1, 2021 that will close this issue
@akaihola akaihola added the question Further information is requested label Sep 4, 2021
@akaihola akaihola added this to the 1.3.1 milestone Sep 4, 2021
@akaihola
Copy link
Owner

akaihola commented Sep 5, 2021

@fizbin, this feature has now been released in Black 1.3.0. Does this fulfill what you needed?

@akaihola akaihola self-assigned this Sep 9, 2021
@akaihola akaihola added the maybe invalid? Can't reproduce, or seems already fixed, or need more information label Sep 10, 2021
@akaihola akaihola modified the milestones: 1.3.1, 1.3.2 Sep 15, 2021
@akaihola akaihola modified the milestones: 1.3.2, 1.4.0 Oct 28, 2021
@akaihola
Copy link
Owner

I tried out @fizbin's original example using Darker 1.3.2, and it did not reformat DontReformatMe.py. It seems version 1.3.0 fixed this problem, so I'm closing this issue now.

@brtkwr
Copy link

brtkwr commented Apr 19, 2022

Hmm I cant get it to work unfortunately. I added the following into my pyproject.toml file:

[tool.black]
extend-exclude = '''
^/src/alembic/versions/*.py
src/alembic/versions/20211210_154740_679ff6652fc6_merged_initial_prod_db.py
'''
[tool.darker]
extend-exclude = '''
^/src/alembic/versions/*.py
src/alembic/versions/20211210_154740_679ff6652fc6_merged_initial_prod_db.py
'''

If I run black --check, I notice that the files get ignored but doesnt seem to be getting ignored by darker somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists maybe invalid? Can't reproduce, or seems already fixed, or need more information question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants