Skip to content

Commit 67188c4

Browse files
authored
Deprecate astor for Python 3.9+ (#737)
Fixes #736
1 parent 14af837 commit 67188c4

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
* Fix issue with `ns-resolve` throwing an error on macros (#720)
2525
* Fix issue with py module `readerwritelock` locks handling (#722)
2626

27+
### Removed
28+
* Removed the dependency `astor` for versions of Python 3.9+ (#736)
29+
2730
## [v0.1.0a2]
2831
### Added
2932
* Added support for fixtures in `basilisp.test` (#654)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ include = ["README.md", "LICENSE"]
3030

3131
[tool.poetry.dependencies]
3232
python = "^3.8"
33-
astor = "^0.8.1"
3433
attrs = ">=20.1.0"
3534
immutables = "^0.15"
3635
prompt-toolkit = "^3.0.0"
3736
pyrsistent = "^0.18.0"
3837
python-dateutil = "^2.8.1"
3938
readerwriterlock = "^1.0.8"
4039

40+
astor = { version = "^0.8.1", python = "<3.9", optional = true }
4141
pytest = { version = "^6.2.5", optional = true }
4242
pygments = { version = "^2.9.0", optional = true }
4343

src/basilisp/lang/compiler/__init__.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import itertools
22
import os
3+
import sys
34
import types
45
from typing import Any, Callable, Iterable, List, Optional
56

6-
from astor import code_gen as codegen
7-
87
from basilisp import _pyast as ast
98
from basilisp.lang import map as lmap
109
from basilisp.lang import runtime as runtime
@@ -37,10 +36,33 @@
3736
_DEFAULT_FN = "__lisp_expr__"
3837

3938

40-
def to_py_str(t: ast.AST) -> str:
41-
"""Return a string of the Python code which would generate the input
42-
AST node."""
43-
return codegen.to_source(t)
39+
if sys.version_info >= (3, 9):
40+
from ast import unparse
41+
42+
def to_py_str(t: ast.AST) -> str:
43+
"""Return a string of the Python code which would generate the input
44+
AST node."""
45+
return unparse(t) + "\n\n"
46+
47+
else:
48+
try:
49+
from astor import code_gen as codegen
50+
51+
def to_py_str(t: ast.AST) -> str:
52+
"""Return a string of the Python code which would generate the input
53+
AST node."""
54+
return codegen.to_source(t)
55+
56+
except ImportError:
57+
import warnings
58+
59+
def to_py_str(t: ast.AST) -> str: # pylint: disable=unused-argument
60+
warnings.warn(
61+
"Unable to generate Python code from generated AST due to missing "
62+
"dependency 'astor'",
63+
RuntimeWarning,
64+
)
65+
return ""
4466

4567

4668
BytecodeCollector = Callable[[types.CodeType], None]

0 commit comments

Comments
 (0)