Reputation: 41
I'm working with a substantial codebase, and we're starting a company-wide effort towards static typing. I know it's possible to do gradual typing. While we don't want to spend the time and effort to statically type the entire codebase, we also want any changes to the codebase (new or modified code) to be properly typed, and would like to add some sort of CI step to nudge developers in that direction.
I'm not referring to solutions like MonkeyType, which attempt to draft type annotations to the codebase. The ideal solution would be able to run mypy in a CI pipeline to do static analysis on all objects that were changed. E.g., consider this code:
# main.py
"""
Legacy code that's dynamically typed
"""
MAX_LOOPS = 10
logger = ...
class Multiplier:
def __init__(...):
...
def __call__(self, left, right):
return left * right
def save(self):
...
def main(value, loops):
multiplier = Multiplier()
for ix in range(loops):
value = multiplier(value, ix)
return value
If I made a PR to change the value of MAX_LOOPS, then I'd have to type-hint it, but I wouldn't have to add type hints for the logger, the Multiplier class, or the main method. If I changed the main method signature, I'd only have to type hint its signature (but possibly not the body, though it could be useful to require that as well), and so on.
I've looked into pre-commit hooks, but it would require some setup from each developer, and it seems I'd only be able to make it work at a file level (i.e.: run static analysis for the whole modified file, even if only one line was changed).
If this isn't possible, I'm currently considering updating the CI pipeline to use a more lenient static analysis on older files, and a more stringent static analysis on any new files (e.g.: files created from today onwards should be fully statically typed, and any type annotation on older files should be coherent).
It doesn't quite do all that I wanted it to, but it might just be the best solution to avoid going overboard with this. Anyone has any experience dealing with this issue?
Upvotes: 4
Views: 302
Reputation: 799
You can try Scanyp for this need. It allows you to compare two versions of your codebase and focus only on the new code.
Upvotes: 1