File: run_mypy.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (95 lines) | stat: -rw-r--r-- 2,983 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# This script is used to execute mypy within a tox environment.

from subprocess import check_call, CalledProcessError
import argparse
import os
import logging
import sys

from ci_tools.environment_exclusions import (
    is_ignored_package,
    MYPY_OPT_OUT,
    TYPE_CHECK_SAMPLES_OPT_OUT,
)

logging.getLogger().setLevel(logging.INFO)

root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run mypy against target folder. ")

    parser.add_argument(
        "-t",
        "--target",
        dest="target_package",
        help="The target package directory on disk. The target module passed to run mypy will be <target_package>/azure.",
        required=True,
    )

    args = parser.parse_args()
    package_name = os.path.basename(os.path.abspath(args.target_package))
    if package_name in MYPY_OPT_OUT or is_ignored_package(package_name):
        logging.info(
            f"Package {package_name} opts-out of mypy check. See https://aka.ms/python/typing-guide for information."
        )
        exit(0)

    commands = [
        sys.executable,
        "-m",
        "mypy",
        "--python-version",
        "3.10",
        "--show-error-codes",
        "--ignore-missing-imports",
    ]
    src_code = [*commands, os.path.join(args.target_package, "azure")]
    src_code_error = None
    sample_code_error = None
    try:
        logging.info(
            f"Running mypy commands on src code: {src_code}"
        )
        check_call(src_code)
    except CalledProcessError as src_err:
        src_code_error = src_err

    if package_name in TYPE_CHECK_SAMPLES_OPT_OUT:
        logging.info(
            f"Package {package_name} opts-out of mypy check on samples."
        )
    else:
        sample_code = [
            *commands,
            "--check-untyped-defs",
            "--follow-imports=silent",
            os.path.join(args.target_package, "samples")
        ]
        try:
            logging.info(
                f"Running mypy commands on sample code: {sample_code}"
            )
            check_call(sample_code)
        except CalledProcessError as sample_err:
            sample_code_error = sample_err

    print("See https://aka.ms/python/typing-guide for information.\n\n")
    if src_code_error and sample_code_error:
        raise Exception(
            [
                src_code_error,
                sample_code_error,
            ],
        )
    if src_code_error:
        raise src_code_error
    if sample_code_error:
        raise sample_code_error