add mypy to style checks; rename spack flake8 to spack style#20384
add mypy to style checks; rename spack flake8 to spack style#20384tgamblin merged 30 commits intospack:developfrom
spack flake8 to spack style#20384Conversation
|
It would be nice to rebase out #20376, otherwise we have to wait for it to be merged before this can be reviewed. I would love to have mypy run in CI! My naive understanding on mypy was that it used Python's new |
|
Sure, I'll rebase that out shortly. As to typing, yes that's exactly what it does. That's part of why I have to do the funny try/import/catch dance, since there's no backport of typing to 2.6 but the types have to be imported in the version being checked. For variables, it infers a type based on the type of the first assignment, and for functions it assumes something like The sphinx thing I'm not sure, I would think the comment type hints would work, but have never tried that TBH. |
|
With Mypy, is there a "strict mode" to ensure that all variables and functions are properly typed? We're obviously nowhere near ready for this, but if we ever drop Python 2 support I would love to enforce that for all functions. |
|
Rebased here, I meant. Should not work on two similar patches quite like this. Yep, there is definitely a strict mode: One upside to the approach they took to gradual typing is that the vast majority of this stuff can be done on a per-module basis, so if some things are ready earlier than others we can make the checking on them stricter without impacting the whole codebase. |
|
I've been using Python 3.5+ type hints in all of my research code for a while, so I've wanted them for Spack too. Unfortunately, until we drop Python 2 support, we can't use those directly, only the comments. I would rather not push for the use of comments (other than the minimal changes you made here). The other complaint I have about Mypy is that it doesn't work unless all of your dependencies also use Mypy. Like, my research code was mostly numpy/pandas, and those libraries don't yet have full type hint support yet, so that was frustrating. |
|
Generally I'd agree. It would be nice to have some type annotations on high use APIs for scripts and packages, but probably not globally. The dependency issue is one place where Spack's vendoring policy actually helps a lot. Since all the source is included in the project, rather than installed and accessed through a site-packages or something, the stubs are unnecessary because mypy will follow the import and do type inference through all the externals as well. At the moment, I have it ignoring |
|
Looks like everything is happy but gitlab, and I don't seem to have access to the results to see why. Should probably figure out how to get access. I dug into getting Mypy working on packages. There is no good way to do this, at all, because it doesn't support loading multiple modules with the same name in the same invocation and all packages are treated as being named "package", their directories also don't have valid module names. We could make a |
|
I don't think I have access to Gitlab either, I've just been ignoring it cuz it isn't required in order for the PR to be merged. |
|
Did a bit more digging, it seems like two packages being built in the pipeline that spuriously fail to build failed. |
There was a problem hiding this comment.
LGTM but 3 change requests:
- Integrate mypy checking with
spack flake8(ifmypyis available) - Change
spack flake8tospack styleand leave the oldflake8command so that it prints out that it's deprecated. - Add a dummy
typingmodule inlib/spack/external/py26/so that we don't have to catchImportErrorfor everytypingimport
|
I support renaming |
This allows type checking of spack and llnl library code, not yet packages as there isn't an obvious way to deal with the missing pkgkit import. Currently running mypy with this results in 106 errors across 46 files out of the 430 files it scans.
This reduces the spack lib from 106 type missing or mismatch issues to 0. Should be just about good enough to hit CI from here.
Currently aliases flake8 to style, and just runs flake8 as before. Will add mypy support after this.
Allow "import typing" and "from typing import ..." in spack without a try/except dance to support old versions. Doing it this way means we can only reliably use all type hints in python3.7+, so the mypy.ini will be updated to reflect that.
|
All checked off. The last one I went a slightly different direction with it and added another directory like py26 for python2, so we can just do |
Also re-format style.py since it no longer passed the black style check with the new length.
Update the style and docs action to use `spack style`, adding in mypy and black to the action even if it isn't running black right now.
tgamblin
left a comment
There was a problem hiding this comment.
This looks great -- minor change requests.
lib/spack/spack/cmd/flake8.py
Outdated
| changed.add(f) | ||
|
|
||
| return sorted(changed) | ||
| description = spack.cmd.style.description |
There was a problem hiding this comment.
This should probably say something about deprecation instead of just passing through the spack style description.
lib/spack/spack/cmd/flake8.py
Outdated
| sys.exit(1) | ||
| else: | ||
| print("Flake8 checks were clean.") | ||
| print("=======================================================") |
There was a problem hiding this comment.
can this stuff be tty.warn? Users will recognize that. Suggest replacing everything with:
tty.warn("`spack flake8` is deprecated", "please use `spack style` to run style checks.")| _InstallPhase_run_after = {} | ||
| # These are accessed only through getattr, by name | ||
| _InstallPhase_run_before = {} # type: Dict[str, List[Callable]] | ||
| _InstallPhase_run_after = {} # type: Dict[str, List[Callable]] |
There was a problem hiding this comment.
This is actually awesome because previously I had to remember how this was structured.
spack flake8 to spack style
spack flake8 to spack stylespack flake8 to spack style
spack flake8 to spack stylespack flake8 to spack style
spack flake8 to spack stylespack flake8 to spack style
|
Ok, I think that covers the comments. Have a look when you get a chance. Hopefully this will also cover the completion and other issues I was seeing in the CI. |
…ck#20384) I lost my mind a bit after getting the completion stuff working and decided to get Mypy working for spack as well. This adds a `.mypy.ini` that checks all of the spack and llnl modules, though not yet packages, and fixes all of the identified missing types and type issues for the spack library. In addition to these changes, this includes: * rename `spack flake8` to `spack style` Aliases flake8 to style, and just runs flake8 as before, but with a warning. The style command runs both `flake8` and `mypy`, in sequence. Added --no-<tool> options to turn off one or the other, they are on by default. Fixed two issues caught by the tools. * stub typing module for python2.x We don't support typing in Spack for python 2.x. To allow 2.x to support `import typing` and `from typing import ...` without a try/except dance to support old versions, this adds a stub module *just* for python 2.x. Doing it this way means we can only reliably use all type hints in python3.7+, and mypi.ini has been updated to reflect that. * add non-default black check to spack style This is a first step to requiring black. It doesn't enforce it by default, but it will check it if requested. Currently enforcing the line length of 79 since that's what flake8 requires, but it's a bit odd for a black formatted project to be quite that narrow. All settings are in the style command since spack has no pyproject.toml and I don't want to add one until more discussion happens. Also re-format `style.py` since it no longer passed the black style check with the new length. * use style check in github action Update the style and docs action to use `spack style`, adding in mypy and black to the action even if it isn't running black right now.
…ck#20384) I lost my mind a bit after getting the completion stuff working and decided to get Mypy working for spack as well. This adds a `.mypy.ini` that checks all of the spack and llnl modules, though not yet packages, and fixes all of the identified missing types and type issues for the spack library. In addition to these changes, this includes: * rename `spack flake8` to `spack style` Aliases flake8 to style, and just runs flake8 as before, but with a warning. The style command runs both `flake8` and `mypy`, in sequence. Added --no-<tool> options to turn off one or the other, they are on by default. Fixed two issues caught by the tools. * stub typing module for python2.x We don't support typing in Spack for python 2.x. To allow 2.x to support `import typing` and `from typing import ...` without a try/except dance to support old versions, this adds a stub module *just* for python 2.x. Doing it this way means we can only reliably use all type hints in python3.7+, and mypi.ini has been updated to reflect that. * add non-default black check to spack style This is a first step to requiring black. It doesn't enforce it by default, but it will check it if requested. Currently enforcing the line length of 79 since that's what flake8 requires, but it's a bit odd for a black formatted project to be quite that narrow. All settings are in the style command since spack has no pyproject.toml and I don't want to add one until more discussion happens. Also re-format `style.py` since it no longer passed the black style check with the new length. * use style check in github action Update the style and docs action to use `spack style`, adding in mypy and black to the action even if it isn't running black right now.
) I lost my mind a bit after getting the completion stuff working and decided to get Mypy working for spack as well. This adds a `.mypy.ini` that checks all of the spack and llnl modules, though not yet packages, and fixes all of the identified missing types and type issues for the spack library. In addition to these changes, this includes: * rename `spack flake8` to `spack style` Aliases flake8 to style, and just runs flake8 as before, but with a warning. The style command runs both `flake8` and `mypy`, in sequence. Added --no-<tool> options to turn off one or the other, they are on by default. Fixed two issues caught by the tools. * stub typing module for python2.x We don't support typing in Spack for python 2.x. To allow 2.x to support `import typing` and `from typing import ...` without a try/except dance to support old versions, this adds a stub module *just* for python 2.x. Doing it this way means we can only reliably use all type hints in python3.7+, and mypi.ini has been updated to reflect that. * add non-default black check to spack style This is a first step to requiring black. It doesn't enforce it by default, but it will check it if requested. Currently enforcing the line length of 79 since that's what flake8 requires, but it's a bit odd for a black formatted project to be quite that narrow. All settings are in the style command since spack has no pyproject.toml and I don't want to add one until more discussion happens. Also re-format `style.py` since it no longer passed the black style check with the new length. * use style check in github action Update the style and docs action to use `spack style`, adding in mypy and black to the action even if it isn't running black right now.
…ck#20384) I lost my mind a bit after getting the completion stuff working and decided to get Mypy working for spack as well. This adds a `.mypy.ini` that checks all of the spack and llnl modules, though not yet packages, and fixes all of the identified missing types and type issues for the spack library. In addition to these changes, this includes: * rename `spack flake8` to `spack style` Aliases flake8 to style, and just runs flake8 as before, but with a warning. The style command runs both `flake8` and `mypy`, in sequence. Added --no-<tool> options to turn off one or the other, they are on by default. Fixed two issues caught by the tools. * stub typing module for python2.x We don't support typing in Spack for python 2.x. To allow 2.x to support `import typing` and `from typing import ...` without a try/except dance to support old versions, this adds a stub module *just* for python 2.x. Doing it this way means we can only reliably use all type hints in python3.7+, and mypi.ini has been updated to reflect that. * add non-default black check to spack style This is a first step to requiring black. It doesn't enforce it by default, but it will check it if requested. Currently enforcing the line length of 79 since that's what flake8 requires, but it's a bit odd for a black formatted project to be quite that narrow. All settings are in the style command since spack has no pyproject.toml and I don't want to add one until more discussion happens. Also re-format `style.py` since it no longer passed the black style check with the new length. * use style check in github action Update the style and docs action to use `spack style`, adding in mypy and black to the action even if it isn't running black right now.
I lost my mind a bit after getting the completion stuff working and decided to get Mypy working for spack as well. This adds a
.mypy.inithat checks all of thespackandllnlmodules, though not yet packages, and fixes all of the identified missing types and type issues for the spack library.Depends on #20376 because of how I built this up, but could probably rebase that out if it's an issue.
In a related note, how would folks feel about having a Mypy run in CI?