mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 19:45:22 +00:00
Strip alias from import name for multiple imports on the same line. Fix for #13
This commit is contained in:
parent
5989c3b109
commit
ea2db2c29a
@ -52,11 +52,9 @@ def get_all_imports(start_path):
|
|||||||
for item in s.groups():
|
for item in s.groups():
|
||||||
if "," in item:
|
if "," in item:
|
||||||
for match in item.split(","):
|
for match in item.split(","):
|
||||||
imports.append(match.strip())
|
imports.append(get_import_name_without_alias(match))
|
||||||
else:
|
else:
|
||||||
to_append = item.partition(
|
imports.append(get_import_name_without_alias(item))
|
||||||
' as ')[0].partition('.')[0]
|
|
||||||
imports.append(to_append.strip())
|
|
||||||
third_party_packages = set(imports) - set(set(packages) & set(imports))
|
third_party_packages = set(imports) - set(set(packages) & set(imports))
|
||||||
logging.debug(
|
logging.debug(
|
||||||
'Found third-party packages: {0}'.format(third_party_packages))
|
'Found third-party packages: {0}'.format(third_party_packages))
|
||||||
@ -130,6 +128,10 @@ def get_pkg_names_from_import_names(pkgs):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_import_name_without_alias(import_name):
|
||||||
|
return import_name.partition(' as ')[0].partition('.')[0].strip()
|
||||||
|
|
||||||
|
|
||||||
def init(args):
|
def init(args):
|
||||||
print("Looking for imports")
|
print("Looking for imports")
|
||||||
imports = get_all_imports(args['<path>'])
|
imports = get_all_imports(args['<path>'])
|
||||||
|
@ -82,6 +82,12 @@ class TestPipreqs(unittest.TestCase):
|
|||||||
for item in self.modules2:
|
for item in self.modules2:
|
||||||
self.assertTrue(item.lower() in data)
|
self.assertTrue(item.lower() in data)
|
||||||
|
|
||||||
|
def test_get_import_name_without_alias(self):
|
||||||
|
import_name_with_alias = "requests as R"
|
||||||
|
expected_import_name_without_alias = "requests"
|
||||||
|
import_name_without_aliases = pipreqs.get_import_name_without_alias(import_name_with_alias)
|
||||||
|
self.assertEqual(import_name_without_aliases, expected_import_name_without_alias)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
try:
|
try:
|
||||||
os.remove(self.requirements_path)
|
os.remove(self.requirements_path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user