-
Notifications
You must be signed in to change notification settings - Fork 11
implement type-hints for all our functions #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
this first attempt at #39, is currently failing to bootstrap:
|
Do we need to add an entry to requirements.txt? |
new set of errors:
i am working speculatively here, so your help would be greatly appreciated! |
i don't think so: it's not a run-time requirement, only for test, hence an entry in |
libiocage/lib/Jail.py
Outdated
@@ -60,19 +62,24 @@ class Jail: | |||
release (stored in `zpool/iocage/base/<RELEASE>) | |||
""" | |||
|
|||
def __init__(self, data={}, zfs=None, host=None, logger=None, new=False): | |||
def __init__(self, | |||
data: Union[str, dict]={}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a union? It is never a string, but always a dict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because that's exactly what the pydoc comment below says
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, coffee. You're right, sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, but, we should still figure out if we want to simplify that
as i have done with the execs that would've allowed Union[str, List[str]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to check fully.qualified.class.names?
libiocage/lib/Jail.py
Outdated
def __init__(self, data={}, zfs=None, host=None, logger=None, new=False): | ||
def __init__(self, | ||
data: Union[str, dict]={}, | ||
zfs: Optional[libzfs.ZFS]=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can't verify this on linux ☹
libiocage/lib/Jail.py:67: error: Name 'libzfs' is not defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which means we won't be able to verify this on Travis…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed that's a problem. Can we provide a Typing file for libzfs?
libiocage/lib/Jail.py
Outdated
data: Union[str, dict]={}, | ||
zfs: Optional[libzfs.ZFS]=None, | ||
host: Optional[libiocage.lib.Host]=None, | ||
logger: Optional[libiocage.lib.Logger]=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for these two, i'm getting
libiocage/lib/Jail.py:68: error: Invalid type "libiocage.lib.Host"
libiocage/lib/Jail.py:69: error: Invalid type "libiocage.lib.Logger"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new data! this is a module, not a type
how do i access the actual type, or the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libiocage.lib.Host.Host 🌮
Python interprets the file itself as module. Within that module there's the Host class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but that's uglt 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I know! The other option is to put everything in one file. Let's quickly forget that.
Yet another solution is to mess with sys.path
, but @william-gr taught me this is a no-go for libraries and bad practice everywhere else.
I think lib/__init__.py
is an appropriate solution: The public exposed classes are available as libiocage.Jail
, libiocage.Release
, etc when importing the directory (for development) or importing globally installed libiocage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to handle multiple return values?
libiocage/lib/helpers.py
Outdated
if isinstance(command, str): | ||
command = [command] | ||
def exec(command, logger=None, ignore_error=False) -> ( | ||
subprocess.Popen, str, str,): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is… wrong, apparently:
libiocage/lib/helpers.py:52: error: Invalid tuple literal type
796b72e
to
680a198
Compare
UPDATE!!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our different Basejail Storage thingies are all… wrong…ish.
libiocage/lib/Jail.py
Outdated
libiocage.lib.NullFSBasejailStorage.NullFSBasejailStorage, | ||
libiocage.lib.ZFSBasejailStorage.ZFSBasejailStorage, | ||
None, | ||
] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea what i'm doing
libiocage/lib/Jail.py
Outdated
] = None | ||
|
||
if self.config["basejail_type"] == "standalone": | ||
backend = libiocage.lib.StandaloneJailStorage.StandaloneJailStorage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libiocage/lib/Jail.py:163: error: Incompatible types in assignment (expression has type Type[StandaloneJailStorage], variable has type "Union[StandaloneJailStorage, NullFSBasejailStorage, ZFSBasejailStorage, None]")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it type Type[StandaloneJailStorage]
, but not type "Type[Union[StandaloneJailStorage, ...."
libiocage/lib/Jail.py
Outdated
] = None | ||
|
||
if self.config["basejail_type"] == "standalone": | ||
backend = libiocage.lib.StandaloneJailStorage.StandaloneJailStorage | ||
|
||
if self.config["basejail_type"] == "zfs": | ||
backend = libiocage.lib.ZFSBasejailStorage.ZFSBasejailStorage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libiocage/lib/Jail.py:166: error: Incompatible types in assignment (expression has type Type[ZFSBasejailStorage], variable has type "Union[StandaloneJailStorage, NullFSBasejailStorage, ZFSBasejailStorage, None]")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for line 169, we get:
libiocage/lib/Jail.py:169: error: Incompatible types in assignment (expression has type Type[NullFSBasejailStorage], variable has type "Union[StandaloneJailStorage, NullFSBasejailStorage, ZFSBasejailStorage, None]")
and finally, for line 172, we get:
libiocage/lib/Jail.py:172: error: Too many arguments for "apply"
@@ -2,7 +2,7 @@ | |||
# Run pep8 on all .py files in all subfolders | |||
|
|||
tmpafter=$(mktemp) | |||
find ./libiocage/ -name \*.py -exec flake8 --ignore=E203,W391 {} + | grep -v "./libiocage/__init__.py" > ${tmpafter} | |||
find ./libiocage/ -name \*.py -exec flake8 --ignore=E203,E241,W391 {} + | grep -v "./libiocage/__init__.py" > ${tmpafter} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is E241
good for? The documentation says E241 (*) | multiple spaces after ‘,’
, but I can't spot why we need to ignore this rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I added the ignore for spaces after :
so we shouldn't need to add another.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without this,
./libiocage/lib/Jail.py:72:23: E241 multiple spaces after ':'
./libiocage/lib/Jail.py:73:22: E241 multiple spaces after ':'
./libiocage/lib/Jail.py:74:23: E241 multiple spaces after ':'
./libiocage/lib/Jail.py:76:22: E241 multiple spaces after ':'
i've basically just followed @skarekrow's lead of how to indent… structures.
as a first step, we'll ad mypy static analyzer to check those. This should give us extra (offline) guarantees about those types.
in the process i also found a few instances where we were mutating input parameters — unnecessarily so. n.b.: libiocage.lib.Foo is the module, libiocage.lib.Foo.Foo is the actual class that we want
explicitly, with Tuple[foo, bar], not implicitly with (foo, bar,)
but not always right ;)
and not a string..
this leads to some circular, or otherwise impossible imports.
as a first step, we'll ad mypy static analyzer to check those.
This should give us extra (offline) guarantees about those types.