diff --git a/setup.py b/setup.py index efdf6933025..856c7fadb0c 100644 --- a/setup.py +++ b/setup.py @@ -71,7 +71,7 @@ def get_long_description() -> str: "click>=7.1.2", "appdirs", "toml>=0.10.1", - "typed-ast>=1.4.2", + "typed-ast>=1.4.2; python_version < '3.8'", "regex>=2020.1.8", "pathspec>=0.6, <1", "dataclasses>=0.6; python_version < '3.7'", @@ -81,6 +81,7 @@ def get_long_description() -> str: extras_require={ "d": ["aiohttp>=3.3.2", "aiohttp-cors"], "colorama": ["colorama>=0.4.3"], + "python2": ["typed-ast>=1.4.2"], }, test_suite="tests.test_black", classifiers=[ diff --git a/src/black/__init__.py b/src/black/__init__.py index a8f4f89a6bb..60d948ccd9c 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -48,7 +48,20 @@ from dataclasses import dataclass, field, replace import click import toml -from typed_ast import ast3, ast27 + +if sys.version_info < (3, 8): + try: + from typed_ast import ast3, ast27 + except ImportError: + print( + "The typed_ast package is not installed.\n" + "You must install typed_ast with `python3 -m pip install typed-ast`.", + file=sys.stderr, + ) + sys.exit(1) +else: + ast3 = ast27 = ast + from pathspec import PathSpec # lib2to3 fork