Support PEP 328

Add support for proper relative imports
This commit is contained in:
Simon Gomizelj 2018-02-05 17:10:43 -05:00
parent 5c720c0110
commit 5c40f793a1
6 changed files with 21 additions and 2 deletions

1
NEWS
View File

@ -27,6 +27,7 @@ Changes from 0.13.0
* new `doc` macro and `#doc` tag macro
* support for PEP 492 with `fn/a`, `defn/a`, `with/a` and `for/a`
* remove `def`
* support for relative imports (PEP 328)
[ Bug Fixes ]
* Numeric literals are no longer parsed as symbols when followed by a dot

View File

@ -1156,10 +1156,17 @@ class HyASTCompiler(object):
def _compile_import(expr, module, names=None, importer=asty.Import):
if not names:
names = [ast.alias(name=ast_str(module), asname=None)]
ast_module = ast_str(module)
module = ast_module.lstrip(".")
level = len(ast_module) - len(module)
if not module:
module = None
ret = importer(expr,
module=ast_str(module),
module=module,
names=names,
level=0)
level=level)
return Result() + ret
expr.pop(0) # index

View File

@ -0,0 +1,3 @@
# Note that __init__.py is intentional so pytest (more specifically py.path)
# will detect us as a Python package. There's logic there that involves looking
# specifically for this file.

View File

@ -1812,3 +1812,8 @@ macros()
(defn f4 [[a b]] "not a docstring")
(assert (none? (. f4 __doc__)))
(assert (= (f4 [1 2]) "not a docstring")))
(defn test-relative-import []
"Make sure relative imports work properly"
(import [..resources [tlib]]))
(assert (= (tlib.*secret-message* "Hello World")))

View File

@ -2,6 +2,9 @@ from hy.macros import macro
from hy import HyList, HyInteger
SECRET_MESSAGE = "Hello World"
@macro("qplah")
def tmac(ETname, *tree):
return HyList((HyInteger(8), ) + tree)