Skip to content

Commit f98b0b4

Browse files
authored
chore: fix mypy static type checking issues (magma#12086)
* chore: reduce mypy exclusion list Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: import_path is always given and must not be optional Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: exclude mypy warning Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: fix mypy type issue Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: fix mypy type issue Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: fix mypy type issue Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com> * chore: fix mypy type issue Signed-off-by: Fritz Lehnert <Fritz.Lehnert@tngtech.com>
1 parent 5437074 commit f98b0b4

File tree

8 files changed

+30
-35
lines changed

8 files changed

+30
-35
lines changed

lte/gateway/python/load_tests/common.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,10 @@
1313
import os
1414
import subprocess # noqa: S404
1515
from pathlib import Path
16-
from typing import List
16+
from typing import List, Optional
1717

1818
from lte.protos.subscriberdb_pb2 import SubscriberID
1919

20-
parents = Path.cwd().parents
21-
parts = Path.cwd().parts
22-
home = str(Path.home())
23-
if 'lte' in parts and len(parents) > 3:
24-
# Get relative import path for protos
25-
IMPORT_PATH = parents[3]
26-
else:
27-
IMPORT_PATH = str(home) + '/magma'
28-
2920
RESULTS_PATH = '/var/tmp'
3021
PROTO_DIR = 'lte/protos'
3122

@@ -84,7 +75,7 @@ def benchmark_grpc_request(
8475
output_file: str,
8576
num_reqs: int,
8677
address: str,
87-
import_path: str = None,
78+
import_path: str,
8879
):
8980
"""Run GHZ based GRPC benchmarking
9081
@@ -95,16 +86,22 @@ def benchmark_grpc_request(
9586
output_file (str): a path where result is written to
9687
num_reqs (int): number of requests to send
9788
address (str): address to the service being benchmarked
89+
import_path (str): protobuf import path
9890
"""
99-
import_path = import_path or IMPORT_PATH
10091
if not Path(import_path).exists():
10192
print('Protobuf import path directory does not exist, exiting')
10293
return
10394
cmd_list = [
10495
'ghz',
105-
'--insecure', '--proto', proto_path, '-i', import_path, '--total',
106-
str(num_reqs), '--call', full_request_type, '-D', input_file,
107-
'-O', 'json', '-o', output_file, address,
96+
'--insecure',
97+
'--proto', proto_path,
98+
'-i', import_path,
99+
'--total', str(num_reqs),
100+
'--call', full_request_type,
101+
'-D', input_file,
102+
'-O', 'json',
103+
'-o', output_file,
104+
address,
108105
]
109106

110107
try:

lte/gateway/python/magma/kernsnoopd/handlers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from functools import lru_cache
1818
from socket import AF_INET, AF_INET6, inet_ntop
1919
from struct import pack
20+
from typing import Tuple
2021

2122
import psutil
2223
from magma.kernsnoopd import metrics
@@ -92,7 +93,7 @@ def _get_cmdline(self, pid: int) -> list:
9293
return psutil.Process(pid=pid).cmdline()
9394

9495
@lru_cache(maxsize=1024)
95-
def _ip_addr_to_str(self, family: int, daddr: (int, int)) -> str:
96+
def _ip_addr_to_str(self, family: int, daddr: Tuple[int, int]) -> str:
9697
"""
9798
_ip_addr_to_str returns a string representation of an IPv4 or IPv6
9899
address. It caches results in an LRU cache to reduce cost of conversion
@@ -110,6 +111,8 @@ def _ip_addr_to_str(self, family: int, daddr: (int, int)) -> str:
110111
elif family == AF_INET6:
111112
# noinspection PyTypeChecker
112113
return inet_ntop(AF_INET6, self.Addr(*daddr))
114+
else:
115+
raise Exception("No valid socket family given!")
113116

114117
def handle(self, bpf):
115118
"""

lte/gateway/python/magma/kernsnoopd/snooper.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
"""
1313
import logging
1414
import os
15+
from typing import List
1516

1617
from bcc import BPF # pylint:disable=import-error
1718
from jinja2 import Template
1819
from magma.common.job import Job
19-
from magma.kernsnoopd.handlers import ebpf_handlers
20+
from magma.kernsnoopd.handlers import ByteCounter, ebpf_handlers
2021

2122
EBPF_SRC_DIR = "/var/opt/magma/ebpf/kernsnoopd/"
2223
if not os.path.isdir(EBPF_SRC_DIR):
@@ -65,7 +66,7 @@ def __init__(
6566

6667
super().__init__(interval=collect_interval, loop=service_loop)
6768
self._bpf = None
68-
self._handlers = []
69+
self._handlers: List[ByteCounter] = []
6970
self._loop = service_loop
7071
self._ebpf_programs = programs
7172
self._service_registry = service_registry

lte/gateway/python/precommit.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
import subprocess # noqa: S404 ignore security warning about subprocess
1717
from typing import List
1818

19-
MAGMA_ROOT = os.getenv('MAGMA_ROOT')
19+
if os.getenv('MAGMA_ROOT'):
20+
MAGMA_ROOT = os.environ["MAGMA_ROOT"]
21+
else:
22+
raise Exception("'MAGMA_ROOT' needs to be set and point to the Magma root directory!")
23+
2024
LINT_DOCKER_PATH = os.path.join(
2125
MAGMA_ROOT,
2226
'lte/gateway/docker/python-precommit/',
@@ -29,9 +33,6 @@
2933

3034
def main() -> None:
3135
"""Provide command-line options to format/lint Magma's Python codebase"""
32-
if MAGMA_ROOT is None:
33-
print("Please set the 'MAGMA_ROOT' environment variable to point to the root directory")
34-
return
3536
print("Magma root is " + MAGMA_ROOT)
3637
args = _parse_args()
3738

lte/gateway/python/scripts/generate_oai_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
DEFAULT_DNS_IPV6_ADDR = "2001:4860:4860:0:0:0:0:8888"
4141
DEFAULT_P_CSCF_IPV4_ADDR = "172.27.23.150"
4242
DEFAULT_P_CSCF_IPV6_ADDR = "2a12:577:9941:f99c:0002:0001:c731:f114"
43-
DEFAULT_NGAP_S_NSSAI_SST = 1
43+
DEFAULT_NGAP_S_NSSAI_SST = "1"
4444
DEFAULT_NGAP_S_NSSAI_SD = "ffffff"
4545
DEFAULT_NGAP_AMF_NAME = "MAGMAAMF1"
4646
DEFAULT_NGAP_AMF_REGION_ID = "1"

lte/gateway/python/scripts/state_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def _deserialize_generic_json(
7676
return element
7777

7878
if isinstance(element, dict):
79-
keys = element.keys()
79+
keys = list(element.keys())
8080
elif isinstance(element, list):
81-
keys = range(len(element))
81+
keys = list(range(len(element)))
8282
else:
8383
# in case it is neither of the know elements, just return as is
8484
return element

lte/gateway/python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# We can use an environment variable to pass in the package version during
1919
# build. Since we don't distribute this on its own, we don't really care what
2020
# version this represents. 'None' defaults to 0.0.0.
21-
VERSION = os.environ.get('PKG_VERSION', None)
21+
VERSION = os.environ.get('PKG_VERSION', '0.0.0')
2222

2323
setup(
2424
name='lte',

mypy.ini

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ namespace_packages = True
88
install_types = True
99
non_interactive = True
1010
exclude = (?x)(
11+
^lte/gateway/python/integ_tests/ |
1112
^lte/gateway/python/magma/pipelined/ |
1213
^lte/gateway/python/magma/enodebd/ |
13-
^lte/gateway/python/magma/subscriberdb/protocols/diameter/avp.py$ |
1414
^lte/gateway/python/magma/subscriberdb/protocols/m5g_auth_servicer.py$ |
15-
^lte/gateway/python/magma/kernsnoopd/ |
16-
^lte/gateway/python/precommit.py$ |
17-
^lte/gateway/python/scripts/agw_health_cli.py$ |
18-
^lte/gateway/python/load_tests.common.py$ |
19-
^lte/gateway/python/scripts/generate_oai_config.py$ |
20-
^lte/gateway/python/setup.py$ |
21-
^lte/gateway/python/scripts/state_cli.py$ |
22-
^lte/gateway/python/integ_tests/
15+
^lte/gateway/python/scripts/generate_oai_config.py$
2316
)

0 commit comments

Comments
 (0)