Change sys.exit to raise ImportError (#2440)

The fix for #1688 in #1761 breaks help("modules") introspection and also leads
to unhappy results when inadvertently importing blackd from Python. Basically
the sys.exit(-1) causes the whole Python REPL to exit -- not great to suffice.

Commit history before merge:

* Change sys.exit to Raise.
* Add #2440 to changelog.
* Fix lint error from prettier
* Remove exception chain for more helpful user message.

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
This commit is contained in:
erykoff 2021-08-24 13:59:24 -07:00 committed by GitHub
parent b97a4ac449
commit 0969ca4a46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -10,6 +10,10 @@
- The failsafe for accidentally added backslashes in f-string expressions has been - The failsafe for accidentally added backslashes in f-string expressions has been
hardened to handle more edge cases during quote normalization (#2437) hardened to handle more edge cases during quote normalization (#2437)
### _Blackd_
- Replace sys.exit(-1) with raise ImportError (#2440)
### Integrations ### Integrations
- The provided pre-commit hooks no longer specify `language_version` to avoid overriding - The provided pre-commit hooks no longer specify `language_version` to avoid overriding

View File

@ -1,6 +1,5 @@
import asyncio import asyncio
import logging import logging
import sys
from concurrent.futures import Executor, ProcessPoolExecutor from concurrent.futures import Executor, ProcessPoolExecutor
from datetime import datetime from datetime import datetime
from functools import partial from functools import partial
@ -11,13 +10,11 @@
from aiohttp import web from aiohttp import web
import aiohttp_cors import aiohttp_cors
except ImportError as ie: except ImportError as ie:
print( raise ImportError(
f"aiohttp dependency is not installed: {ie}. " f"aiohttp dependency is not installed: {ie}. "
+ "Please re-install black with the '[d]' extra install " + "Please re-install black with the '[d]' extra install "
+ "to obtain aiohttp_cors: `pip install black[d]`", + "to obtain aiohttp_cors: `pip install black[d]`"
file=sys.stderr, ) from None
)
sys.exit(-1)
import black import black
from black.concurrency import maybe_install_uvloop from black.concurrency import maybe_install_uvloop