future_import_names is not public, at the moment.

This commit is contained in:
Dave Halter
2017-09-01 00:25:32 +02:00
parent fa0bf4951c
commit 68fa70b959
2 changed files with 11 additions and 10 deletions
+8 -7
View File
@@ -315,14 +315,14 @@ class Module(Scope):
super(Module, self).__init__(children) super(Module, self).__init__(children)
self._used_names = None self._used_names = None
def iter_future_import_names(self): def _iter_future_import_names(self):
""" """
:return list of str: A list of future import names. :return list of str: A list of future import names.
""" """
# TODO this is a strange scan and not fully correct. I think Python's # In Python it's not allowed to use future imports after the first
# parser does it in a different way and scans for the first # actual (non-future) statement. However this is not a linter here,
# statement/import with a tokenizer (to check for syntax changes like # just return all future imports. If people want to scan for issues
# the future print statement). # they should use the API.
for imp in self.iter_imports(): for imp in self.iter_imports():
if imp.type == 'import_from' and imp.level == 0: if imp.type == 'import_from' and imp.level == 0:
for path in imp.get_paths(): for path in imp.get_paths():
@@ -330,13 +330,14 @@ class Module(Scope):
if len(names) == 2 and names[0] == '__future__': if len(names) == 2 and names[0] == '__future__':
yield names[1] yield names[1]
def has_explicit_absolute_import(self): def _has_explicit_absolute_import(self):
""" """
Checks if imports in this module are explicitly absolute, i.e. there Checks if imports in this module are explicitly absolute, i.e. there
is a ``__future__`` import. is a ``__future__`` import.
Currently not public, might be in the future.
:return bool: :return bool:
""" """
for name in self.iter_future_import_names(): for name in self._iter_future_import_names():
if name == 'absolute_import': if name == 'absolute_import':
return True return True
return False return False
+3 -3
View File
@@ -10,14 +10,14 @@ def test_explicit_absolute_imports():
Detect modules with ``from __future__ import absolute_import``. Detect modules with ``from __future__ import absolute_import``.
""" """
module = parse("from __future__ import absolute_import") module = parse("from __future__ import absolute_import")
assert module.has_explicit_absolute_import() assert module._has_explicit_absolute_import()
def test_no_explicit_absolute_imports(): def test_no_explicit_absolute_imports():
""" """
Detect modules without ``from __future__ import absolute_import``. Detect modules without ``from __future__ import absolute_import``.
""" """
assert not parse("1").has_explicit_absolute_import() assert not parse("1")._has_explicit_absolute_import()
def test_dont_break_imports_without_namespaces(): def test_dont_break_imports_without_namespaces():
@@ -26,4 +26,4 @@ def test_dont_break_imports_without_namespaces():
assume that all imports have non-``None`` namespaces. assume that all imports have non-``None`` namespaces.
""" """
src = "from __future__ import absolute_import\nimport xyzzy" src = "from __future__ import absolute_import\nimport xyzzy"
assert parse(src).has_explicit_absolute_import() assert parse(src)._has_explicit_absolute_import()