From 0f622de27c96df54a7498dd59962c7985bb072c4 Mon Sep 17 00:00:00 2001 From: Vadim Kravcenko Date: Mon, 11 May 2015 21:46:33 +0200 Subject: [PATCH] fix(pipreqs): ignore word "import" in package names --- pipreqs/mapping | 1 + pipreqs/pipreqs.py | 6 ++++-- tests/_data/test.py | 22 +++++++++++++++++++++- tests/test_pipreqs.py | 7 +++---- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/pipreqs/mapping b/pipreqs/mapping index 4738ba1..9e1ca29 100644 --- a/pipreqs/mapping +++ b/pipreqs/mapping @@ -261,6 +261,7 @@ socketio:gevent_socketio socketserver:pies2overrides sockjs:sockjs_tornado socks:SocksiPy_branch +IPython:ipython solr:solrpy solution:Solution sorl:sorl_thumbnail diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 4b13a72..ccccb57 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -126,8 +126,10 @@ def get_pkg_names(pkgs): def get_name_without_alias(name): - if "import" in name: - name = REGEXP[0].match(name.strip()).groups(0)[0] + if "import " in name: + match = REGEXP[0].match(name.strip()) + if match: + name = match.groups(0)[0] return name.partition(' as ')[0].partition('.')[0].strip() diff --git a/tests/_data/test.py b/tests/_data/test.py index 9809a0d..b1843bc 100644 --- a/tests/_data/test.py +++ b/tests/_data/test.py @@ -7,8 +7,28 @@ from sys import argv as test2 # [unused-import] from sys import flags # [unused-import] # +1:[unused-import,unused-import] from collections import deque, OrderedDict, Counter -import requests # [unused-import] # All imports above should be ignored +import requests # [unused-import] + +# setuptools +import zipimport # command/easy_install.py + +# twisted +from importlib import invalidate_caches # python/test/test_deprecate.py + +# astroid +import zipimport # manager.py +# IPython +from importlib.machinery import all_suffixes # core/completerlib.py +import importlib # html/notebookapp.py + +from IPython.utils.importstring import import_item # Many files + +# pyflakes +from pyflakes.test.test_imports import Test as TestImports # test/test_doctests.py + +# Nose +from nose.importer import Importer, add_path, remove_path # loader.py import atexit from __future__ import print_function diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 47d49c7..d216bfc 100755 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -18,7 +18,7 @@ class TestPipreqs(unittest.TestCase): def setUp(self): self.modules = ['flask', 'requests', 'sqlalchemy', - 'docopt', 'boto', 'peewee', 'ujson', 'nonexistendmodule', 'bs4',] + 'docopt', 'boto', 'ipython', 'pyflakes', 'nose', 'peewee', 'ujson', 'nonexistendmodule', 'bs4',] self.modules2 = ['beautifulsoup4'] self.project = os.path.join(os.path.dirname(__file__), "_data") self.requirements_path = os.path.join(self.project, "requirements.txt") @@ -27,7 +27,7 @@ class TestPipreqs(unittest.TestCase): def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) - self.assertEqual(len(imports), 9) + self.assertEqual(len(imports), 12) for item in imports: self.assertTrue( item.lower() in self.modules, "Import is missing: " + item) @@ -42,8 +42,7 @@ class TestPipreqs(unittest.TestCase): imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain only 5 Elements without the "nonexistendmodule" - self.assertEqual( - len(with_info), 7) + self.assertEqual(len(with_info), 10) for item in with_info: self.assertTrue(item['name'].lower( ) in self.modules, "Import item appears to be missing " + item['name'])