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

Read shebangs to discover python files #930

Closed
wants to merge 3 commits into from

Conversation

florczakraf
Copy link

@florczakraf florczakraf commented Jul 19, 2019

At work we've often found ourselves manually adding python scripts without .py extensions to black calls because they were placed in various locations in repositories that weren't strictly python projects. Such special handling is error-prone and we've come to the conclusion that since black offers python file discovery (includes/excludes) when the directory is provided, it could be extended to also check shebangs.

Thanks to @D0han for the support!

@zsol
Copy link
Collaborator

zsol commented Jul 19, 2019

caveat: I haven't looked at the code yet. My main concern with this idea is the performance hit when crawling large directories of non-python code

@emdej
Copy link

emdej commented Jul 19, 2019

On *nix systems we could check executable files only.

@florczakraf
Copy link
Author

florczakraf commented Jul 19, 2019

@zsol: I've just checked the difference on some internal repo. I don't see much overhead on the repo with more than 10.5k files where there were ~150 python files to reformat.

Without this patch: 22.3s (avg over the 3 runs)
With: 23.5s (but there were extra files to reformat ;)

Moreover, this feature has been set as optional with an appropriate warning in help.

@emdej: it could produce inconsistent results on exotic platforms like Windows.

@florczakraf
Copy link
Author

I see one other problem with the current code -- it doesn't handle OSErrors while opening the files, for example when there's no read permission. I think that we can safely skip such files as black would not reformat them anyway.

@tyilo
Copy link
Contributor

tyilo commented Jul 24, 2019

At least #!/usr/bin/python should also be included by default.
Maybe also #!/usr/bin/python3 and #!/usr/bin/python2.

@D0han
Copy link

D0han commented Jul 24, 2019

I do not think that we should encourage usage of such explicit path with defaults.
If someone wants, regex is easily customizable with either toml or cli parameter.

@asottile
Copy link
Contributor

fwiw, when run using pre-commit run --all-files the shebang'd executable files will already be passed to black (via identify)

@D0han
Copy link

D0han commented Jul 28, 2019

pre-commit is entire different project, that black can, but not have to, be used with.

@asottile
Copy link
Contributor

pre-commit is entire different project, that black can, but not have to, be used with.

sure :) was just offering some potential prior art

@hugovk
Copy link
Contributor

hugovk commented Jul 29, 2019

fwiw, when run using pre-commit run --all-files the shebang'd executable files will already be passed to black (via identify)

Yeah, that caught me out, I was surprised when pre-commit run --all-files formatted more files than black .! Here is a workaround.

@florczakraf
Copy link
Author

I've just rebased the patch on the upstream and resolved conflicts. Please let me know if you're going to consider pulling this or not.

@florczakraf
Copy link
Author

I see that there are problems after the rebase with the new tests. I will have a look at that tomorrow.

@easbarba
Copy link

easbarba commented Aug 26, 2019

python3
#!/usr/bin/env python3

@florczakraf
Copy link
Author

I think that we can safely extend the default regex to include ^#!/usr/bin/python as well

@ambv
Copy link
Collaborator

ambv commented Oct 20, 2019

Thanks for your work on this. However, I don't think we're willing to slow down traversal for all users who are not putting Python code in non-.py files. For this use case pre-commit or something similar is a decent workaround.

@ambv ambv closed this Oct 20, 2019
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

Successfully merging this pull request may close these issues.

None yet

9 participants