Correct 2.5-7 relative import formatting

This commit is contained in:
rocky
2022-05-14 19:28:23 -04:00
parent 656a9aa290
commit fa9cc4c669
7 changed files with 21 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,2 +1,4 @@
# Tests relative imports # Tests relative imports
from . import bogus from . import bogus
from .. import foo
from ..bar import baz

View File

@@ -476,8 +476,17 @@ TABLE_DIRECT = {
"kv2": ("%c: %c", 1, 2), "kv2": ("%c: %c", 1, 2),
"import": ("%|import %c\n", 2), "import": ("%|import %c\n", 2),
"importlist": ("%C", (0, maxint, ", ")), "importlist": ("%C", (0, maxint, ", ")),
"import_from": ("%|from %[2]{pattr} import %c\n", (3, "importlist")),
"import_from_star": ("%|from %[2]{pattr} import *\n",), # Note: the below rule isn't really complete:
# n_import_from() smashes node[2].pattr
"import_from": (
"%|from %[2]{pattr} import %c\n",
(3, "importlist")
),
"import_from_star": (
"%|from %[2]{pattr} import *\n",
),
} }

View File

@@ -48,3 +48,9 @@ def customize_for_version25(self, version):
node[1][0][0][0].kind = 'tf_tryelsestmt' node[1][0][0][0].kind = 'tf_tryelsestmt'
self.default(node) self.default(node)
self.n_tryfinallystmt = tryfinallystmt self.n_tryfinallystmt = tryfinallystmt
def n_import_from(node):
if node[0].pattr > 0:
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
self.default(node)
self.n_import_from = n_import_from

View File

@@ -62,12 +62,7 @@ def customize_for_version26_27(self, version):
self.n_call = n_call self.n_call = n_call
def n_import_from(node): def n_import_from(node):
import_name = node[2] if node[0].pattr > 0:
if import_name == "IMPORT_NAME" and import_name.pattr == "": node[2].pattr = ("." * node[0].pattr) + node[2].pattr
fmt = "%|from . import %c\n"
self.template_engine(
(fmt, (3, "importlist")), node
)
self.prune()
self.default(node) self.default(node)
self.n_import_from = n_import_from self.n_import_from = n_import_from