Skip to content

Commit ead3f39

Browse files
authored
♻️ REFACTOR: Make __all__ imports explicit (via pre-commit) (aiidateam#5061)
This PR introduces a pre-commit hook, to auto-generate the `*` imports and `__all__` specifications for `__init__.py` files. This makes the `__all__` lists explicit rather than implicit, addresses incompatibilities with a number of static analysis tools (including mypy and pylance), allows for the exposed module APIs to be inspected and controlled, and detects any name clashes of these exposed objects. The hook code is contained in `utils/make_all.py`. It works by statically parsing all files, to search for their `__all__` specification (if present), then using this to build a recursive tree of module imports and using this to determine what to write to the `__init__.py` files.
1 parent 8e99581 commit ead3f39

File tree

101 files changed

+4194
-2471
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+4194
-2471
lines changed

.pre-commit-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ repos:
4444

4545
hooks:
4646

47+
- id: imports
48+
name: imports
49+
entry: python utils/make_all.py
50+
language: python
51+
types: [python]
52+
require_serial: true
53+
pass_filenames: false
54+
files: aiida/.*py
55+
4756
- id: mypy
4857
name: mypy
4958
entry: mypy

aiida/cmdline/__init__.py

+39-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,47 @@
77
# For further information on the license, see the LICENSE.txt file #
88
# For further information please visit http://www.aiida.net #
99
###########################################################################
10-
# pylint: disable=wildcard-import,undefined-variable
1110
"""The command line interface of AiiDA."""
1211

13-
from .params.arguments import *
14-
from .params.options import *
15-
from .params.types import *
16-
from .utils.decorators import *
17-
from .utils.echo import *
12+
# AUTO-GENERATED
13+
14+
# yapf: disable
15+
# pylint: disable=wildcard-import
16+
17+
from .params import *
18+
from .utils import *
1819

1920
__all__ = (
20-
params.arguments.__all__ + params.options.__all__ + params.types.__all__ + utils.decorators.__all__ +
21-
utils.echo.__all__
21+
'AbsolutePathParamType',
22+
'CalculationParamType',
23+
'CodeParamType',
24+
'ComputerParamType',
25+
'ConfigOptionParamType',
26+
'DataParamType',
27+
'EmailType',
28+
'EntryPointType',
29+
'FileOrUrl',
30+
'GroupParamType',
31+
'HostnameType',
32+
'IdentifierParamType',
33+
'LabelStringType',
34+
'LazyChoice',
35+
'MpirunCommandParamType',
36+
'MultipleValueParamType',
37+
'NodeParamType',
38+
'NonEmptyStringParamType',
39+
'PathOrUrl',
40+
'PluginParamType',
41+
'ProcessParamType',
42+
'ProfileParamType',
43+
'ShebangParamType',
44+
'TestModuleParamType',
45+
'UserParamType',
46+
'WorkflowParamType',
47+
'dbenv',
48+
'format_call_graph',
49+
'only_if_daemon_running',
50+
'with_dbenv',
2251
)
52+
53+
# yapf: enable

aiida/cmdline/params/__init__.py

+39
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,42 @@
77
# For further information on the license, see the LICENSE.txt file #
88
# For further information please visit http://www.aiida.net #
99
###########################################################################
10+
"""Commandline parameters."""
11+
12+
# AUTO-GENERATED
13+
14+
# yapf: disable
15+
# pylint: disable=wildcard-import
16+
17+
from .types import *
18+
19+
__all__ = (
20+
'AbsolutePathParamType',
21+
'CalculationParamType',
22+
'CodeParamType',
23+
'ComputerParamType',
24+
'ConfigOptionParamType',
25+
'DataParamType',
26+
'EmailType',
27+
'EntryPointType',
28+
'FileOrUrl',
29+
'GroupParamType',
30+
'HostnameType',
31+
'IdentifierParamType',
32+
'LabelStringType',
33+
'LazyChoice',
34+
'MpirunCommandParamType',
35+
'MultipleValueParamType',
36+
'NodeParamType',
37+
'NonEmptyStringParamType',
38+
'PathOrUrl',
39+
'PluginParamType',
40+
'ProcessParamType',
41+
'ProfileParamType',
42+
'ShebangParamType',
43+
'TestModuleParamType',
44+
'UserParamType',
45+
'WorkflowParamType',
46+
)
47+
48+
# yapf: enable

aiida/cmdline/params/arguments/__init__.py

+29-52
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,37 @@
1010
# yapf: disable
1111
"""Module with pre-defined reusable commandline arguments that can be used as `click` decorators."""
1212

13-
import click
13+
# AUTO-GENERATED
1414

15-
from .. import types
16-
from .overridable import OverridableArgument
15+
# yapf: disable
16+
# pylint: disable=wildcard-import
17+
18+
from .main import *
1719

1820
__all__ = (
19-
'PROFILE', 'PROFILES', 'CALCULATION', 'CALCULATIONS', 'CODE', 'CODES', 'COMPUTER', 'COMPUTERS', 'DATUM', 'DATA',
20-
'GROUP', 'GROUPS', 'NODE', 'NODES', 'PROCESS', 'PROCESSES', 'WORKFLOW', 'WORKFLOWS', 'INPUT_FILE', 'OUTPUT_FILE',
21-
'LABEL', 'USER', 'CONFIG_OPTION'
21+
'CALCULATION',
22+
'CALCULATIONS',
23+
'CODE',
24+
'CODES',
25+
'COMPUTER',
26+
'COMPUTERS',
27+
'CONFIG_OPTION',
28+
'DATA',
29+
'DATUM',
30+
'GROUP',
31+
'GROUPS',
32+
'INPUT_FILE',
33+
'LABEL',
34+
'NODE',
35+
'NODES',
36+
'OUTPUT_FILE',
37+
'PROCESS',
38+
'PROCESSES',
39+
'PROFILE',
40+
'PROFILES',
41+
'USER',
42+
'WORKFLOW',
43+
'WORKFLOWS',
2244
)
2345

24-
25-
PROFILE = OverridableArgument('profile', type=types.ProfileParamType())
26-
27-
PROFILES = OverridableArgument('profiles', type=types.ProfileParamType(), nargs=-1)
28-
29-
CALCULATION = OverridableArgument('calculation', type=types.CalculationParamType())
30-
31-
CALCULATIONS = OverridableArgument('calculations', nargs=-1, type=types.CalculationParamType())
32-
33-
CODE = OverridableArgument('code', type=types.CodeParamType())
34-
35-
CODES = OverridableArgument('codes', nargs=-1, type=types.CodeParamType())
36-
37-
COMPUTER = OverridableArgument('computer', type=types.ComputerParamType())
38-
39-
COMPUTERS = OverridableArgument('computers', nargs=-1, type=types.ComputerParamType())
40-
41-
DATUM = OverridableArgument('datum', type=types.DataParamType())
42-
43-
DATA = OverridableArgument('data', nargs=-1, type=types.DataParamType())
44-
45-
GROUP = OverridableArgument('group', type=types.GroupParamType())
46-
47-
GROUPS = OverridableArgument('groups', nargs=-1, type=types.GroupParamType())
48-
49-
NODE = OverridableArgument('node', type=types.NodeParamType())
50-
51-
NODES = OverridableArgument('nodes', nargs=-1, type=types.NodeParamType())
52-
53-
PROCESS = OverridableArgument('process', type=types.ProcessParamType())
54-
55-
PROCESSES = OverridableArgument('processes', nargs=-1, type=types.ProcessParamType())
56-
57-
WORKFLOW = OverridableArgument('workflow', type=types.WorkflowParamType())
58-
59-
WORKFLOWS = OverridableArgument('workflows', nargs=-1, type=types.WorkflowParamType())
60-
61-
INPUT_FILE = OverridableArgument('input_file', metavar='INPUT_FILE', type=click.Path(exists=True))
62-
63-
OUTPUT_FILE = OverridableArgument('output_file', metavar='OUTPUT_FILE', type=click.Path())
64-
65-
LABEL = OverridableArgument('label', type=click.STRING)
66-
67-
USER = OverridableArgument('user', metavar='USER', type=types.UserParamType())
68-
69-
CONFIG_OPTION = OverridableArgument('option', type=types.ConfigOptionParamType())
46+
# yapf: enable
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
###########################################################################
3+
# Copyright (c), The AiiDA team. All rights reserved. #
4+
# This file is part of the AiiDA code. #
5+
# #
6+
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
7+
# For further information on the license, see the LICENSE.txt file #
8+
# For further information please visit http://www.aiida.net #
9+
###########################################################################
10+
# yapf: disable
11+
"""Module with pre-defined reusable commandline arguments that can be used as `click` decorators."""
12+
13+
import click
14+
15+
from .. import types
16+
from .overridable import OverridableArgument
17+
18+
__all__ = (
19+
'PROFILE', 'PROFILES', 'CALCULATION', 'CALCULATIONS', 'CODE', 'CODES', 'COMPUTER', 'COMPUTERS', 'DATUM', 'DATA',
20+
'GROUP', 'GROUPS', 'NODE', 'NODES', 'PROCESS', 'PROCESSES', 'WORKFLOW', 'WORKFLOWS', 'INPUT_FILE', 'OUTPUT_FILE',
21+
'LABEL', 'USER', 'CONFIG_OPTION'
22+
)
23+
24+
25+
PROFILE = OverridableArgument('profile', type=types.ProfileParamType())
26+
27+
PROFILES = OverridableArgument('profiles', type=types.ProfileParamType(), nargs=-1)
28+
29+
CALCULATION = OverridableArgument('calculation', type=types.CalculationParamType())
30+
31+
CALCULATIONS = OverridableArgument('calculations', nargs=-1, type=types.CalculationParamType())
32+
33+
CODE = OverridableArgument('code', type=types.CodeParamType())
34+
35+
CODES = OverridableArgument('codes', nargs=-1, type=types.CodeParamType())
36+
37+
COMPUTER = OverridableArgument('computer', type=types.ComputerParamType())
38+
39+
COMPUTERS = OverridableArgument('computers', nargs=-1, type=types.ComputerParamType())
40+
41+
DATUM = OverridableArgument('datum', type=types.DataParamType())
42+
43+
DATA = OverridableArgument('data', nargs=-1, type=types.DataParamType())
44+
45+
GROUP = OverridableArgument('group', type=types.GroupParamType())
46+
47+
GROUPS = OverridableArgument('groups', nargs=-1, type=types.GroupParamType())
48+
49+
NODE = OverridableArgument('node', type=types.NodeParamType())
50+
51+
NODES = OverridableArgument('nodes', nargs=-1, type=types.NodeParamType())
52+
53+
PROCESS = OverridableArgument('process', type=types.ProcessParamType())
54+
55+
PROCESSES = OverridableArgument('processes', nargs=-1, type=types.ProcessParamType())
56+
57+
WORKFLOW = OverridableArgument('workflow', type=types.WorkflowParamType())
58+
59+
WORKFLOWS = OverridableArgument('workflows', nargs=-1, type=types.WorkflowParamType())
60+
61+
INPUT_FILE = OverridableArgument('input_file', metavar='INPUT_FILE', type=click.Path(exists=True))
62+
63+
OUTPUT_FILE = OverridableArgument('output_file', metavar='OUTPUT_FILE', type=click.Path())
64+
65+
LABEL = OverridableArgument('label', type=click.STRING)
66+
67+
USER = OverridableArgument('user', metavar='USER', type=types.UserParamType())
68+
69+
CONFIG_OPTION = OverridableArgument('option', type=types.ConfigOptionParamType())

0 commit comments

Comments
 (0)