From 68fa70b959e4536094029740ef017901ad003545 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 1 Sep 2017 00:25:32 +0200 Subject: [PATCH] future_import_names is not public, at the moment. --- parso/python/tree.py | 15 ++++++++------- test/test_absolute_import.py | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/parso/python/tree.py b/parso/python/tree.py index 581f230..35e9e82 100644 --- a/parso/python/tree.py +++ b/parso/python/tree.py @@ -315,14 +315,14 @@ class Module(Scope): super(Module, self).__init__(children) 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. """ - # TODO this is a strange scan and not fully correct. I think Python's - # parser does it in a different way and scans for the first - # statement/import with a tokenizer (to check for syntax changes like - # the future print statement). + # In Python it's not allowed to use future imports after the first + # actual (non-future) statement. However this is not a linter here, + # just return all future imports. If people want to scan for issues + # they should use the API. for imp in self.iter_imports(): if imp.type == 'import_from' and imp.level == 0: for path in imp.get_paths(): @@ -330,13 +330,14 @@ class Module(Scope): if len(names) == 2 and names[0] == '__future__': 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 is a ``__future__`` import. + Currently not public, might be in the future. :return bool: """ - for name in self.iter_future_import_names(): + for name in self._iter_future_import_names(): if name == 'absolute_import': return True return False diff --git a/test/test_absolute_import.py b/test/test_absolute_import.py index 6b8407f..c959ea5 100644 --- a/test/test_absolute_import.py +++ b/test/test_absolute_import.py @@ -10,14 +10,14 @@ def test_explicit_absolute_imports(): Detect modules with ``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(): """ 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(): @@ -26,4 +26,4 @@ def test_dont_break_imports_without_namespaces(): assume that all imports have non-``None`` namespaces. """ src = "from __future__ import absolute_import\nimport xyzzy" - assert parse(src).has_explicit_absolute_import() + assert parse(src)._has_explicit_absolute_import()