Skip to content

Commit 355f31a

Browse files
committed
add type annotations for when mypy feels confused
use 'forward references' of types that haven't been properly imported yet Including IocageEvent - which is referenced from from itself!
1 parent 2f0fa99 commit 355f31a

File tree

9 files changed

+29
-22
lines changed

9 files changed

+29
-22
lines changed

libiocage/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2222
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
# POSSIBILITY OF SUCH DAMAGE.
24+
from libiocage.lib import errors, events
25+
from libiocage.lib.Host import Host
2426
from libiocage.lib.Jail import Jail
2527
from libiocage.lib.Jails import Jails
28+
from libiocage.lib.Logger import Logger
2629
from libiocage.lib.Release import Release
2730
from libiocage.lib.Releases import Releases
28-
from libiocage.lib.Host import Host
29-
from libiocage.lib.Logger import Logger
30-
from libiocage.lib import errors

libiocage/cli/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
logger = Logger()
3737

38-
click.core._verify_python3_env = lambda: None
38+
click.core._verify_python3_env = lambda: None # type: ignore
3939
user_locale = os.environ.get("LANG", "en_US.UTF-8")
4040
locale.setlocale(locale.LC_ALL, user_locale)
4141

@@ -148,8 +148,7 @@ def get_command(self, ctx, name):
148148

149149
@click.option("--log-level", "-d", default=None)
150150
@click.command(cls=IOCageCLI)
151-
@click.version_option(version="0.2.11 08/29/2017", prog_name="ioc",
152-
message="%(version)s")
151+
@click.version_option(version="0.2.11 08/29/2017", prog_name="ioc")
153152
@click.pass_context
154153
def cli(ctx, log_level):
155154
"""A jail manager."""

libiocage/lib/Host.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ def processor(self):
9191

9292
class Host(HostGenerator):
9393

94-
class_distribution = libiocage.lib.Distribution.DistributionGenerator
94+
_class_distribution = libiocage.lib.Distribution.DistributionGenerator

libiocage/lib/Jail.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ class JailGenerator:
8484
release (stored in `zpool/iocage/base/<RELEASE>)
8585
"""
8686

87-
_class_host = libiocage.lib.Host.HostGenerator
88-
_class_storage = libiocage.lib.Storage.Storage
89-
9087
def __init__(self, data={}, zfs=None, host=None, logger=None, new=False):
9188
"""
9289
Initializes a Jail
@@ -124,7 +121,7 @@ def __init__(self, data={}, zfs=None, host=None, logger=None, new=False):
124121

125122
self.networks = []
126123

127-
self.storage = self._class_storage(
124+
self.storage = libiocage.lib.Storage.Storage(
128125
auto_create=True,
129126
safe_mode=False,
130127
jail=self,

libiocage/lib/JailFilter.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2222
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
# POSSIBILITY OF SUCH DAMAGE.
24-
from typing import List, Union, Iterable
2524
import re
26-
import libiocage.lib.Jail
25+
from typing import Iterable, List, Union
26+
2727
import libiocage.lib.errors
28+
import libiocage.lib.Jail
2829

2930

3031
def match_filter(value: str, filter_string: str):
@@ -54,7 +55,7 @@ def __init__(self, key, values=list()):
5455

5556
list.__init__(self, data)
5657

57-
def matches_jail(self, jail: libiocage.lib.Jail.JailGenerator) -> bool:
58+
def matches_jail(self, jail: 'libiocage.lib.Jail.JailGenerator') -> bool:
5859
return self.matches(jail.getstring(self.key))
5960

6061
def matches(self, value: str) -> bool:
@@ -83,7 +84,7 @@ def _filter_string_has_globs(self, filter_string: str) -> bool:
8384
return False
8485

8586
def _split_filter_values(self, user_input: str) -> List[str]:
86-
values = []
87+
values: List[str] = []
8788
escaped_comma_blocks = map(
8889
lambda block: block.split(","),
8990
user_input.split("\\,")
@@ -124,9 +125,9 @@ class Terms(list):
124125
This can be interpreted as logical AND
125126
"""
126127

127-
def __init__(self, terms: Iterable[Union[Term, str]]=None):
128+
def __init__(self, terms: Iterable[Union[Term, str]]=None) -> None:
128129

129-
data = []
130+
data: List[Union[Term, str]] = []
130131

131132
if terms is not None:
132133

@@ -138,7 +139,7 @@ def __init__(self, terms: Iterable[Union[Term, str]]=None):
138139

139140
list.__init__(self, data)
140141

141-
def match_jail(self, jail: libiocage.lib.Jail.JailGenerator) -> bool:
142+
def match_jail(self, jail: 'libiocage.lib.Jail.JailGenerator') -> bool:
142143
"""
143144
Returns True if all Terms match the jail
144145
"""

libiocage/lib/Jails.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def jail_datasets(self) -> list:
9898
def _load_jail_from_dataset(
9999
self,
100100
dataset: libzfs.ZFSDataset
101-
) -> Generator[libiocage.lib.Jail.JailGenerator, None, None]:
101+
) -> Generator['libiocage.lib.Jail.JailGenerator', None, None]:
102102

103103
return self._create_jail({
104104
"name": self._get_name_from_jail_dataset(dataset)

libiocage/lib/Logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import libiocage.lib.errors
2828

29+
from typing import List
30+
2931

3032
class LogEntry:
3133

@@ -100,7 +102,7 @@ class Logger:
100102

101103
INDENT_PREFIX = " "
102104

103-
PRINT_HISTORY = []
105+
PRINT_HISTORY: List[str] = []
104106

105107
def __init__(self, print_level=None, log_directory="/var/log/iocage"):
106108
self._print_level = print_level

libiocage/lib/errors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,13 @@ def __init__(self, *args, **kwargs):
412412

413413
class MissingFeature(IocageException, NotImplementedError):
414414

415-
def __init__(self, feature_name: str, plural: bool=False, *args, **kwargs):
415+
def __init__(
416+
self,
417+
feature_name: str,
418+
plural: bool=False,
419+
*args,
420+
**kwargs
421+
) -> None:
416422
message = (
417423
f"Missing Feature: '{feature_name}' "
418424
"are" if plural is True else "is"

libiocage/lib/events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
# POSSIBILITY OF SUCH DAMAGE.
2424
from timeit import default_timer as timer
25+
from typing import List
26+
2527
import libiocage.lib.errors
2628

2729
EVENT_STATUS = (
@@ -38,7 +40,7 @@ class IocageEvent:
3840
Base class for all other iocage events
3941
"""
4042

41-
HISTORY = []
43+
HISTORY: List['IocageEvent'] = []
4244

4345
PENDING_COUNT = 0
4446

0 commit comments

Comments
 (0)