-
Notifications
You must be signed in to change notification settings - Fork 259
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
Use black for loading black config #1950
Use black for loading black config #1950
Conversation
Hey! Many thanks for the PR :) Looks like The function I do like the idea of letting black find the config file instead of whats happening in right now. ASK: |
Yes! I was actually thinking about this last night as I was falling asleep. |
Once the changes are implemented, I'll gladly merge! |
617aee8
to
d389ddc
Compare
All set! |
current_version = parse_version(black.__version__) | ||
if current_version >= parse_version("21.5b1"): | ||
from black.files import find_pyproject_toml | ||
elif current_version >= parse_version("20.8b0"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did find_project_toml
not exist before this version?
If it didn't then we should have find_project_toml = None
as an else branch so that we don't throw an error later down the road since find_project_toml
won't be defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was introduced in May of 2020, and the next release was in August 2020. I'll set it to None
in the else
block. My original implementation defined a custom find_pyproject_toml
in the else
block, but due to the function signature, it was more complex than adding a check at the call site.
1b660a5
to
b3be4b5
Compare
This changes how black configuration is loaded. Instead of looking for `pyproject.toml` directly, use a black to load the configuration in the same way the black CLI would. This allows for other configuration files to be considered, such as `~/.black` or `~/.config/black`.
b3be4b5
to
0803e76
Compare
Merged. Thank You! |
This probably needs to be reverted or fixed. I'm pretty sure it is the root cause of #1955, which is currently being experienced in |
@jeremyheiler do you have some time to look into this for a potential fix? |
@@ -43,7 +51,10 @@ def fix_code(code, directory): | |||
# Get black config from pyproject.toml | |||
line_length = black.DEFAULT_LINE_LENGTH | |||
string_normalization = True | |||
pyproject_path = os.path.join(directory, "pyproject.toml") | |||
if find_pyproject_toml: | |||
pyproject_path = find_pyproject_toml((directory,)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this may be the root cause of #1955. It looks like (at least with black==21.12b0
that find_pyproject_toml
will return None
if it can't find "pyproject.toml"
in directory
.
Calling os.path.exists(pyproject_path)
then equates to os.path.exists(None)
, which will raise an exception:
>>> import os.path
>>> os.path.exists(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/MacPorts-20211130/Python.framework/Versions/3.9/lib/python3.9/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
if find_pyproject_toml: | ||
pyproject_path = find_pyproject_toml((directory,)) | ||
else: | ||
pyproject_path = os.path.join(directory, "pyproject.toml") | ||
if toml is not None and os.path.exists(pyproject_path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup! You'll have to forgive me, past that, though. I don't have much experience with elisp or elpy's test mechanisms to formulate an automated way to repro this, so I think that's about as useful as I can be in this context (at least right now). |
PR Summary
Fixes #1949
This changes how black configuration is loaded. Instead of looking for
pyproject.toml
directly, use a black to load the configuration in thesame way the black CLI would. This allows for other configuration
files to be considered, such as
~/.black
or~/.config/black
.PR checklist
Please make sure that the following things have been addressed (and check the relevant checkboxes):