Skip to content

Commit

Permalink
don't require typed-ast
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Mar 21, 2021
1 parent 5446a92 commit 3bec1d4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -35,6 +35,10 @@

- Lines ending with `fmt: skip` will now be not formatted (#1800)

- Black no longer relies on typed-ast for python versions >= 3.8

- Python2 support is now optional, install with `python3 -m pip install black[python2]`

#### _Packaging_

- Self-contained native _Black_ binaries are now provided for releases via GitHub
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -50,7 +50,8 @@ _Contents:_ **[Installation and usage](#installation-and-usage)** |
### Installation

_Black_ can be installed by running `pip install black`. It requires Python 3.6.2+ to
run but you can reformat Python 2 code with it, too.
run. If you want to format Python 2 code as well, install with
`pip install black[python2]`.

#### Install from GitHub

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -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'",
Expand All @@ -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=[
Expand Down
23 changes: 21 additions & 2 deletions src/black/__init__.py
Expand Up @@ -48,7 +48,20 @@
from dataclasses import dataclass, field, replace
import click
import toml
from typed_ast import ast3, ast27

try:
from typed_ast import ast3, ast27
except ImportError:
if sys.version_info < (3, 8):
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
Expand Down Expand Up @@ -6336,7 +6349,13 @@ def parse_ast(src: str) -> Union[ast.AST, ast3.AST, ast27.AST]:
return ast3.parse(src, filename, feature_version=feature_version)
except SyntaxError:
continue

if ast27.__name__ == "ast":
print(
"Black attempted to parse python27 code but 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)
return ast27.parse(src)


Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -7,7 +7,7 @@ skip_install = True
deps =
-r{toxinidir}/test_requirements.txt
commands =
pip install -e .[d]
pip install -e .[d, python2]
coverage erase
coverage run -m pytest tests
coverage report
Expand Down

0 comments on commit 3bec1d4

Please sign in to comment.