Skip to content

Commit 57fcaf4

Browse files
Merge pull request #54 from QuantStack/fix/star-import-filtering
Fix incorrect behavior when star importing a module
2 parents b7b79b2 + 576401b commit 57fcaf4

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

memestra/memestra.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,19 @@ def get_deprecated_users(self, defuse, ancestors):
181181
deprecated_node, reason))
182182

183183
else:
184-
# special node: a node imported from *; in that case we do
185-
# selective matching
184+
# There's a special handler in ImportFrom for these
186185
if isinstance(deprecated_node, DeprecatedStar):
187-
def filter_out(user):
188-
return user.name() != deprecated_node.name
189-
deprecated = deprecated_node.dnode
190-
# regular symbol processing
191-
else:
192-
def filter_out(user):
193-
return False
194-
deprecated = deprecated_node
195-
196-
for user in defuse.chains[deprecated].users():
197-
if filter_out(user):
198-
continue
186+
continue
187+
188+
for user in defuse.chains[deprecated_node].users():
199189
user_ancestors = [n
200190
for n in ancestors.parents(user.node)
201191
if isinstance(n, _defs)]
202192
if any(f in self.deprecated for f in user_ancestors):
203193
continue
204194

205195
user_ancestor = user_ancestors[-1] if user_ancestors else user.node
206-
deprecated_uses.append((deprecated, user.node,
196+
deprecated_uses.append((deprecated_node, user.node,
207197
user_ancestor,
208198
reason))
209199
if self.recursive and isinstance(user_ancestor, _defs):
@@ -241,6 +231,9 @@ def visit_ImportFrom(self, node):
241231
def alias_extractor(deprec):
242232
return star_alias
243233

234+
def alias_selector(deprec, node):
235+
return getattr(node, 'id', None) == deprec
236+
244237
except ValueError:
245238
# otherwise only pick the imported one
246239

@@ -252,12 +245,18 @@ def alias_extractor(deprec):
252245
except ValueError:
253246
return None
254247

248+
def alias_selector(deprec, node):
249+
return True
250+
255251
for deprec, reason in deprecated.items():
256252
deprec_alias = alias_extractor(deprec)
257253
if deprec_alias is None:
258254
continue
259255

260256
for user in self.def_use_chains.chains[deprec_alias].users():
257+
# if deprec_alias is '*' we need to be selective
258+
if not alias_selector(deprec, user.node):
259+
continue
261260
self.deprecated.add(make_deprecated(user.node, reason))
262261

263262
def visit_Module(self, node):

tests/misc/ipy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
from .a import *
2+
3+
def useless():
4+
pass

tests/test_imports.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ def test_forwarding_all_symbols2(self):
181181

182182
self.checkDeprecatedUses(
183183
code,
184-
[('*', '<>', 3, 4, None),
185-
('Test', '<>', 3, 4, None)])
184+
[('Test', '<>', 3, 4, None)])
186185

187186
def test_importing_non_existing_file(self):
188187
code = '''
@@ -236,6 +235,16 @@ def test_import_pkg_level_star(self):
236235
code,
237236
[('foo', '<>', 2, 0, 'why'), ('foo', '<>', 4, 4, 'why')])
238237

238+
def test_import_pkg_level_star2(self):
239+
code = '''
240+
from ipy import *
241+
a = useless()
242+
b = foo()'''
243+
244+
self.checkDeprecatedUses(
245+
code,
246+
[('foo', '<>', 4, 4, 'why')])
247+
239248
def test_shared_cache(self):
240249
# We have a fake description for gast in tests/share/memestra
241250
# Setup the shared cache to use it.

0 commit comments

Comments
 (0)