From 21c4eaa614232e9cdc5d7e6ef8a7e207876dd5b2 Mon Sep 17 00:00:00 2001 From: cn Date: Thu, 19 Dec 2019 17:36:35 +0100 Subject: [PATCH] module: use type hints and validate with mypy --- .github/workflows/ci.yml | 1 + .gitignore | 1 + Pipfile | 1 + Pipfile.lock | 60 +++++++++++++++++++++++++++++++++++++++- kube-stale-resources.py | 14 ++++++++-- mypy.ini | 4 +++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 mypy.ini diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a55282c..fe3f358 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,4 +25,5 @@ jobs: pipenv run yapf -q *.py - name: Linting run: | + pipenv run mypy *.py pipenv run pylint *.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7f7a42 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.mypy_cache diff --git a/Pipfile b/Pipfile index cfd4e22..db215a1 100644 --- a/Pipfile +++ b/Pipfile @@ -12,3 +12,4 @@ requests = '*' rope = '*' pylint = '*' yapf = '*' +mypy = '*' diff --git a/Pipfile.lock b/Pipfile.lock index 03a30d7..0a9d0b9 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "dba3ebb339150511f91075ab30df27c809a6fee8c82646a583a5a92aaf121c6f" + "sha256": "a2ed1934f950798488c0ba6ef5e46f9faafe6f4688c7087c6f8aa57101dc7312" }, "pipfile-spec": 6, "requires": { @@ -114,6 +114,31 @@ ], "version": "==0.6.1" }, + "mypy": { + "hashes": [ + "sha256:0308c35fd16c96a81b8dfc4d09ec63b8fa607cfec087acf5aafb44c2c45197de", + "sha256:39f7be2f89668d21b2bbab45ce5aa15e69bf8d6f3b46f9e1cc1a88e4fcc84f3d", + "sha256:4223f576813c79a10d0fd14192c86f1b85e3bd235c93792f22ed811a20b5ee4e", + "sha256:4c8f812a2fbefa96185933fbe05aa035e9cf791cf3a23bbdb6a219c80b60e0b1", + "sha256:4ea9ee847ea5bb38ea275441f3aea7eeba1b96187a3f968ee359d33d9dcc0eda", + "sha256:573c68df69f0e399fa57866a0b72989acf0a56c4008eee59c789c2ca5ea9df03", + "sha256:588c0e38466306aa7dbe6522ceacf37dde8b13cfa5edde90be2ce382f078875f", + "sha256:6d1bd2e675823a19e6bf72149540ab9851bfe698b796aea698fb926ab2bedd02", + "sha256:aa8e3bd1540dd5c39ef580ec2146a9c99c45f7c62af890095fec9e87b5ca19fb", + "sha256:b978ba1ea90d0abe2fc720ec9a41824b7d3a1304569bd58c9038d8d61dc4dfdb", + "sha256:c85c5367c2e8247e06cc0aba84e3633e90f48e8a0677bc51b351e138b5ff80b1", + "sha256:ce69577b424058bfa177df27213869f37c1e964c3e1ebd3b3d54f1d10b234c4d", + "sha256:ec6eaf98a57624d96d9916352a5bad2d73959f6358fabf43838f7d1a4d2f8389" + ], + "version": "==0.760" + }, + "mypy-extensions": { + "hashes": [ + "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d", + "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8" + ], + "version": "==0.4.3" + }, "pylint": { "hashes": [ "sha256:3db5468ad013380e987410a8d6956226963aed94ecb5f9d3a28acca6d9ac36cd", @@ -136,6 +161,39 @@ ], "version": "==1.13.0" }, + "typed-ast": { + "hashes": [ + "sha256:1170afa46a3799e18b4c977777ce137bb53c7485379d9706af8a59f2ea1aa161", + "sha256:18511a0b3e7922276346bcb47e2ef9f38fb90fd31cb9223eed42c85d1312344e", + "sha256:262c247a82d005e43b5b7f69aff746370538e176131c32dda9cb0f324d27141e", + "sha256:2b907eb046d049bcd9892e3076c7a6456c93a25bebfe554e931620c90e6a25b0", + "sha256:354c16e5babd09f5cb0ee000d54cfa38401d8b8891eefa878ac772f827181a3c", + "sha256:48e5b1e71f25cfdef98b013263a88d7145879fbb2d5185f2a0c79fa7ebbeae47", + "sha256:4e0b70c6fc4d010f8107726af5fd37921b666f5b31d9331f0bd24ad9a088e631", + "sha256:630968c5cdee51a11c05a30453f8cd65e0cc1d2ad0d9192819df9978984529f4", + "sha256:66480f95b8167c9c5c5c87f32cf437d585937970f3fc24386f313a4c97b44e34", + "sha256:71211d26ffd12d63a83e079ff258ac9d56a1376a25bc80b1cdcdf601b855b90b", + "sha256:7954560051331d003b4e2b3eb822d9dd2e376fa4f6d98fee32f452f52dd6ebb2", + "sha256:838997f4310012cf2e1ad3803bce2f3402e9ffb71ded61b5ee22617b3a7f6b6e", + "sha256:95bd11af7eafc16e829af2d3df510cecfd4387f6453355188342c3e79a2ec87a", + "sha256:bc6c7d3fa1325a0c6613512a093bc2a2a15aeec350451cbdf9e1d4bffe3e3233", + "sha256:cc34a6f5b426748a507dd5d1de4c1978f2eb5626d51326e43280941206c209e1", + "sha256:d755f03c1e4a51e9b24d899561fec4ccaf51f210d52abdf8c07ee2849b212a36", + "sha256:d7c45933b1bdfaf9f36c579671fec15d25b06c8398f113dab64c18ed1adda01d", + "sha256:d896919306dd0aa22d0132f62a1b78d11aaf4c9fc5b3410d3c666b818191630a", + "sha256:fdc1c9bbf79510b76408840e009ed65958feba92a88833cdceecff93ae8fff66", + "sha256:ffde2fbfad571af120fcbfbbc61c72469e72f550d676c3342492a9dfdefb8f12" + ], + "version": "==1.4.0" + }, + "typing-extensions": { + "hashes": [ + "sha256:091ecc894d5e908ac75209f10d5b4f118fbdb2eb1ede6a63544054bb1edb41f2", + "sha256:910f4656f54de5993ad9304959ce9bb903f90aadc7c67a0bef07e678014e892d", + "sha256:cf8b63fedea4d89bab840ecbb93e75578af28f76f66c35889bd7065f5af88575" + ], + "version": "==3.7.4.1" + }, "wrapt": { "hashes": [ "sha256:565a021fd19419476b9362b05eeaa094178de64f8361e44468f9e9d7843901e1" diff --git a/kube-stale-resources.py b/kube-stale-resources.py index 61d74d0..e2b23ee 100644 --- a/kube-stale-resources.py +++ b/kube-stale-resources.py @@ -4,7 +4,7 @@ import argparse import re import sys -from typing import (List, Tuple, IO) +from typing import (IO, List, Tuple) import requests import yaml @@ -36,6 +36,10 @@ BLACKLIST_REGEXS = [ def get_live_namespaced_resources(url: str) -> List[K8sResourceIdentifier]: + """ + Returns list of Kubernetes resource identifiers of namespaced resources out of the live cluster reachable at url. + """ + result = [] # merges https://kubernetes.io/docs/reference/using-api/#api-groups @@ -80,6 +84,9 @@ def get_live_namespaced_resources(url: str) -> List[K8sResourceIdentifier]: def get_target_namespaced_resources(stream: IO) -> List[K8sResourceIdentifier]: + """ + Returns list of Kubernetes resource identifiers of namespaced resources out of the target stream. + """ result = [] target_documents = list(yaml.load_all(stream, Loader=yaml.SafeLoader)) @@ -96,6 +103,9 @@ def get_target_namespaced_resources(stream: IO) -> List[K8sResourceIdentifier]: def get_compact_resource_identifiers(tuples: List[K8sResourceIdentifier]) -> List[str]: + """ + Returns a compact, sortable string for a Kubernetes resource identifier. + """ return [namespace + ':' + apiVersion + ':' + kind + ':' + name for namespace, apiVersion, kind, name in tuples] @@ -116,7 +126,7 @@ def main(): args = parser.parse_args() - blacklist_regexs = [] + blacklist_regexs: List[str] = [] blacklist_regexs += BLACKLIST_REGEXS if args.blacklist_file: diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..732a65c --- /dev/null +++ b/mypy.ini @@ -0,0 +1,4 @@ +[mypy] +ignore_missing_imports=true +follow_imports=silent +show_column_numbers=true