Skip to content

Commit e65f683

Browse files
author
Florian Maas
authored
Added parsing of imports within try/except (#32)
1 parent 249df2f commit e65f683

File tree

7 files changed

+1643
-82
lines changed

7 files changed

+1643
-82
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ To scan your project for obsolete imports, run
4747
deptry .
4848
```
4949

50-
or for a more verbose version
51-
52-
```sh
53-
deptry . -v
54-
```
55-
5650
__deptry__ can be configured by using additional command line arguments, or
5751
by adding a `[tool.deptry]` section in __pyproject.toml__.
5852

deptry/import_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _get_import_nodes_from(self, root: Union[ast.Module, ast.If]):
5959
"""
6060
imports = []
6161
for node in ast.iter_child_nodes(root):
62-
if isinstance(node, ast.If):
62+
if isinstance(node, ast.If) or isinstance(node, ast.Try) or isinstance(node, ast.ExceptHandler):
6363
imports += self._get_import_nodes_from(node)
6464
elif isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom):
6565
imports += [node]

docs/docs/index.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,5 @@ To scan your project for obsolete imports, run
4343
deptry .
4444
```
4545

46-
which might output:
47-
48-
```
49-
pyproject.toml contains obsolete dependencies: ['pandas', 'numpy']
50-
```
51-
5246
_deptry_ can be configured by using additional command line arguments, or
53-
by adding a `[tool.deptry]` section in _pyproject.toml_. For more information, see the [Usage and Configuration](./usage.md)
54-
47+
by adding a `[tool.deptry]` section in _pyproject.toml_. For more information, see [Usage and Configuration](./usage.md)

notebooks/Untitled.ipynb

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"id": "064f4445-752d-48e7-bbac-72fe198e28a2",
77
"metadata": {},
88
"outputs": [],
@@ -12,106 +12,69 @@
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": 2,
15+
"execution_count": null,
1616
"id": "6d2dda83-00d2-407a-87ff-b602b415cb5c",
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
20-
"root = ast.parse(\n",
21-
"\"\"\"\n",
22-
"x=1\n",
20+
"my_py_file = \"\"\"\n",
2321
"import pandas as pd\n",
2422
"from numpy import random\n",
25-
"if x>0:\n",
23+
"try:\n",
2624
" import click\n",
27-
"elif x<0:\n",
28-
" from typing import List\n",
29-
"else:\n",
25+
"except:\n",
3026
" import logging\n",
31-
"\"\"\")"
27+
"\"\"\"\n",
28+
"root = ast.parse(my_py_file)"
3229
]
3330
},
3431
{
3532
"cell_type": "code",
36-
"execution_count": 3,
33+
"execution_count": null,
3734
"id": "00957720-c90a-47be-bb95-4e59dcc026d3",
3835
"metadata": {},
39-
"outputs": [
40-
{
41-
"data": {
42-
"text/plain": [
43-
"<ast.Module at 0x110524fd0>"
44-
]
45-
},
46-
"execution_count": 3,
47-
"metadata": {},
48-
"output_type": "execute_result"
49-
}
50-
],
36+
"outputs": [],
5137
"source": [
5238
"root"
5339
]
5440
},
5541
{
5642
"cell_type": "code",
57-
"execution_count": 8,
43+
"execution_count": null,
5844
"id": "729215e5-abc3-4dae-98f7-96ab7c41727a",
5945
"metadata": {},
60-
"outputs": [
61-
{
62-
"name": "stdout",
63-
"output_type": "stream",
64-
"text": [
65-
"<ast.Assign object at 0x110524fa0>\n",
66-
"<ast.Import object at 0x110524f10>\n",
67-
"<ast.ImportFrom object at 0x110524e50>\n",
68-
"<ast.If object at 0x110524e20>\n",
69-
"<ast.Compare object at 0x110524be0>\n",
70-
"<ast.ImportFrom object at 0x110524b20>\n",
71-
"<ast.Import object at 0x1105249a0>\n"
72-
]
73-
}
74-
],
46+
"outputs": [],
7547
"source": [
7648
"for node in ast.iter_child_nodes(root):\n",
7749
" print(node)\n",
78-
" if isinstance(node, ast.If):\n",
50+
" if isinstance(node, ast.Try):\n",
7951
" for node in ast.iter_child_nodes(node):\n",
80-
" if isinstance(node, ast.If):\n",
52+
" print(node)\n",
53+
" if isinstance(node, ast.Try):\n",
8154
" for node in ast.iter_child_nodes(node):\n",
82-
" print(node)"
55+
" pass"
8356
]
8457
},
8558
{
8659
"cell_type": "code",
87-
"execution_count": 6,
88-
"id": "8f70351b-1852-4628-b683-db42be55e943",
60+
"execution_count": null,
61+
"id": "d4978dbe-2173-404f-a46a-8839e09a4e52",
8962
"metadata": {},
90-
"outputs": [
91-
{
92-
"ename": "TypeError",
93-
"evalue": "'Module' object is not subscriptable",
94-
"output_type": "error",
95-
"traceback": [
96-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
97-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
98-
"Input \u001b[0;32mIn [6]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m node \u001b[38;5;129;01min\u001b[39;00m ast\u001b[38;5;241m.\u001b[39miter_child_nodes(\u001b[43mroot\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(node)\n",
99-
"\u001b[0;31mTypeError\u001b[0m: 'Module' object is not subscriptable"
100-
]
101-
}
102-
],
63+
"outputs": [],
10364
"source": [
104-
"for node in ast.iter_child_nodes(root):\n",
105-
" print(node)"
65+
"from deptry.import_parser import ImportParser\n",
66+
"imported_modules = ImportParser().get_imported_modules_from_str(my_py_file)"
10667
]
10768
},
10869
{
10970
"cell_type": "code",
11071
"execution_count": null,
111-
"id": "d4978dbe-2173-404f-a46a-8839e09a4e52",
72+
"id": "874769dc-5275-469d-9af8-3bf6d6ff0b1e",
11273
"metadata": {},
11374
"outputs": [],
114-
"source": []
75+
"source": [
76+
"imported_modules"
77+
]
11578
}
11679
],
11780
"metadata": {

0 commit comments

Comments
 (0)