|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +__author__ = "heider" |
| 5 | +__doc__ = r""" |
| 6 | +
|
| 7 | + Created on 9/6/22 |
| 8 | + """ |
| 9 | + |
| 10 | +__all__ = ["is_missing_typing_hints"] |
| 11 | + |
| 12 | +import subprocess |
| 13 | +from pathlib import Path |
| 14 | +from typing import Optional, Iterable, Callable, Sequence |
| 15 | + |
| 16 | + |
| 17 | +def is_missing_typing_hints( |
| 18 | + path: Path, |
| 19 | + *, |
| 20 | + verbose: bool = True, |
| 21 | + flags: Optional[Sequence] = ( |
| 22 | + "--disallow-untyped-calls ", |
| 23 | + "--disallow-untyped-defs ", |
| 24 | + "--disallow-incomplete-defs ", |
| 25 | + "--strict-optional", |
| 26 | + "--strict", |
| 27 | + ), |
| 28 | +) -> bool: |
| 29 | + """ |
| 30 | +
|
| 31 | + :param verbose: |
| 32 | + :type verbose: |
| 33 | + :param path: |
| 34 | + :type path: |
| 35 | + :return: |
| 36 | + :rtype: |
| 37 | + """ |
| 38 | + a = subprocess.getoutput(f'mypy {" ".join(flags)} {str(path)}') |
| 39 | + if verbose: |
| 40 | + print(a) |
| 41 | + if "Success:" not in a: |
| 42 | + return True |
| 43 | + return False |
| 44 | + |
| 45 | + |
| 46 | +def is_missing_typing_hints_traverse( |
| 47 | + path: Path, |
| 48 | + exclusion_filter: Optional[Iterable[Callable]] = None, |
| 49 | + *, |
| 50 | + verbose: bool = True, |
| 51 | +): |
| 52 | + """ |
| 53 | +
|
| 54 | + :param path: |
| 55 | + :type path: |
| 56 | + :param exclusion_filter: |
| 57 | + :type exclusion_filter: |
| 58 | + :param init_name: |
| 59 | + :type init_name: |
| 60 | + :param verbose: |
| 61 | + :type verbose: |
| 62 | + """ |
| 63 | + path = Path(path) |
| 64 | + |
| 65 | + for child in path.iterdir(): |
| 66 | + if child.is_dir(): |
| 67 | + if exclusion_filter is None or not any( |
| 68 | + flt(child) for flt in exclusion_filter |
| 69 | + ): |
| 70 | + is_missing_typing_hints_traverse( |
| 71 | + child, exclusion_filter=exclusion_filter, verbose=verbose |
| 72 | + ) |
| 73 | + else: |
| 74 | + if verbose: |
| 75 | + print( |
| 76 | + f"{child} was excluded, filters:\n{[(flt.__name__, flt(child)) for flt in exclusion_filter]}" |
| 77 | + ) |
| 78 | + elif child.is_file(): |
| 79 | + if is_missing_typing_hints( |
| 80 | + child, |
| 81 | + verbose=verbose, |
| 82 | + ): |
| 83 | + break |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + print(is_missing_typing_hints(Path(__file__).parent)) |
0 commit comments