Skip to content

Commit dd6774a

Browse files
authored
Python typing support, part 2 (#18196)
Another incremental step in adding types to our python code. Rather than just running mypy on 2 source files, run on all files by default with an exclude like (currently this checks 118 source files).
1 parent 99cf031 commit dd6774a

16 files changed

+63
-43
lines changed

.mypy.ini

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
[mypy]
2-
mypy_path = third_party/leb128
3-
files = tools/shared.py,tools/config.py
2+
mypy_path = third_party/,third_party/ply,third_party/websockify
3+
files = .
4+
exclude = (?x)(
5+
third_party |
6+
conf\.py |
7+
emrun\.py |
8+
tools/scons/site_scons/site_tools/emscripten/__init__\.py |
9+
site/source/get_wiki\.py |
10+
test/parse_benchmark_output\.py
11+
)
412

5-
[mypy-*.toolchain_profiler,*.filelock]
13+
[mypy-tools.create_dom_pk_codes,tools.webidl_binder,tools.toolchain_profiler,tools.filelock,tools.find_bigvars,leb128,ply.*]
614
ignore_errors = True

test/benchmark_sse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from tools.shared import WINDOWS, CLANG_CXX, EMCC, PIPE
1919
from tools.shared import run_process
2020
from tools.config import V8_ENGINE
21-
from test.common import EMRUN, test_file
21+
from common import EMRUN, test_file
2222
import clang_native
2323

2424
temp_dir = tempfile.mkdtemp()

test/common.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from functools import wraps
88
from pathlib import Path
99
from subprocess import PIPE, STDOUT
10+
from typing import Dict, Tuple
1011
from urllib.parse import unquote, unquote_plus
1112
from http.server import HTTPServer, SimpleHTTPRequestHandler
1213
import contextlib
@@ -29,8 +30,7 @@
2930

3031
import clang_native
3132
import jsrun
32-
from tools.shared import TEMP_DIR, EMCC, EMXX, DEBUG, EMCONFIGURE, EMCMAKE
33-
from tools.shared import EMSCRIPTEN_TEMP_DIR
33+
from tools.shared import EMCC, EMXX, DEBUG, EMCONFIGURE, EMCMAKE
3434
from tools.shared import get_canonical_temp_dir, path_from_root
3535
from tools.utils import MACOS, WINDOWS, read_file, read_binary, write_file, write_binary, exit_with_error
3636
from tools import shared, line_endings, building, config, utils
@@ -411,8 +411,8 @@ def __new__(mcs, name, bases, attrs):
411411
class RunnerCore(unittest.TestCase, metaclass=RunnerMeta):
412412
# default temporary directory settings. set_temp_dir may be called later to
413413
# override these
414-
temp_dir = TEMP_DIR
415-
canonical_temp_dir = get_canonical_temp_dir(TEMP_DIR)
414+
temp_dir = shared.TEMP_DIR
415+
canonical_temp_dir = get_canonical_temp_dir(shared.TEMP_DIR)
416416

417417
# This avoids cluttering the test runner output, which is stderr too, with compiler warnings etc.
418418
# Change this to None to get stderr reporting, for debugging purposes
@@ -532,7 +532,7 @@ def setUp(self):
532532

533533
if not EMTEST_SAVE_DIR:
534534
self.has_prev_ll = False
535-
for temp_file in os.listdir(TEMP_DIR):
535+
for temp_file in os.listdir(shared.TEMP_DIR):
536536
if temp_file.endswith('.ll'):
537537
self.has_prev_ll = True
538538

@@ -947,7 +947,7 @@ def assertBinaryEqual(self, file1, file2):
947947
self.assertEqual(read_binary(file1),
948948
read_binary(file2))
949949

950-
library_cache = {}
950+
library_cache: Dict[str, Tuple[str, object]] = {}
951951

952952
def get_build_dir(self):
953953
ret = self.in_dir('building')
@@ -1005,8 +1005,8 @@ def get_library(self, name, generated_libs, configure=['sh', './configure'], #
10051005

10061006
def clear(self):
10071007
utils.delete_contents(self.get_dir())
1008-
if EMSCRIPTEN_TEMP_DIR:
1009-
utils.delete_contents(EMSCRIPTEN_TEMP_DIR)
1008+
if shared.EMSCRIPTEN_TEMP_DIR:
1009+
utils.delete_contents(shared.EMSCRIPTEN_TEMP_DIR)
10101010

10111011
def run_process(self, cmd, check=True, **args):
10121012
# Wrapper around shared.run_process. This is desirable so that the tests

test/sockets/socket_relay.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import socket
1919
import time
2020
import threading
21+
from typing import Optional
2122

2223
ports = [int(sys.argv[1]), int(sys.argv[2])]
2324

2425

2526
class Listener(threading.Thread):
27+
other: Optional[Listener] = None # noqa: F821
28+
2629
def run(self):
2730
self.conn = None
2831
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

test/test_benchmark.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import unittest
1313
import zlib
1414
from pathlib import Path
15+
from typing import List
1516

1617
if __name__ == '__main__':
1718
raise Exception('do not run this file directly; do something like: test/runner.py benchmark')
@@ -351,7 +352,7 @@ def get_output_files(self):
351352

352353
# Benchmarkers
353354

354-
benchmarkers = []
355+
benchmarkers: List[Benchmarker] = []
355356

356357
if not common.EMTEST_FORCE64:
357358
benchmarkers += [

test/test_sanity.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from common import parameterized, EMBUILDER
1818
from tools.config import EM_CONFIG
1919
from tools.shared import EMCC
20-
from tools.shared import CANONICAL_TEMP_DIR
2120
from tools.shared import config
2221
from tools.shared import EXPECTED_LLVM_VERSION, Cache
2322
from tools.utils import delete_file, delete_dir
@@ -355,7 +354,7 @@ def test_emcc(self):
355354
# but with EMCC_DEBUG=1 we should check
356355
with env_modify({'EMCC_DEBUG': '1'}):
357356
output = self.check_working(EMCC)
358-
delete_dir(CANONICAL_TEMP_DIR)
357+
delete_dir(shared.CANONICAL_TEMP_DIR)
359358

360359
self.assertContained(SANITY_MESSAGE, output)
361360
output = self.check_working(EMCC)
@@ -683,7 +682,7 @@ def test_with_fake(report, expected):
683682
self.check_working([EMCC] + MINIMAL_HELLO_WORLD + ['-c'], expected)
684683

685684
test_with_fake('got js backend! JavaScript (asm.js, emscripten) backend', 'LLVM has not been built with the WebAssembly backend')
686-
delete_dir(CANONICAL_TEMP_DIR)
685+
delete_dir(shared.CANONICAL_TEMP_DIR)
687686

688687
def test_required_config_settings(self):
689688
# with no binaryen root, an error is shown

test/test_sockets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import time
1212
from subprocess import Popen
13+
from typing import List
1314

1415
if __name__ == '__main__':
1516
raise Exception('do not run this file directly; do something like: test/runner sockets')
@@ -60,7 +61,7 @@ def __enter__(self):
6061
process = Popen([os.path.abspath('server')])
6162
self.processes.append(process)
6263

63-
import websockify
64+
import websockify # type: ignore
6465

6566
# start the websocket proxy
6667
print('running websockify on %d, forward to tcp %d' % (self.listen_port, self.target_port), file=sys.stderr)
@@ -153,7 +154,7 @@ def PythonTcpEchoServerProcess(port):
153154

154155

155156
class sockets(BrowserCore):
156-
emcc_args = []
157+
emcc_args: List[str] = []
157158

158159
@classmethod
159160
def setUpClass(cls):

tools/building.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def acorn_optimizer(filename, passes, extra_info=None, return_output=False):
405405
return output_file
406406

407407

408-
acorn_optimizer.counter = 0
408+
acorn_optimizer.counter = 0 # type: ignore
409409

410410
WASM_CALL_CTORS = '__wasm_call_ctors'
411411

@@ -1315,7 +1315,7 @@ def save_intermediate(src, dst):
13151315
shutil.copyfile(src, dst)
13161316

13171317

1318-
save_intermediate.counter = 0
1318+
save_intermediate.counter = 0 # type: ignore
13191319

13201320

13211321
def js_legalization_pass_flags():

tools/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import sys
88
import logging
9-
from typing import List
9+
from typing import List, Optional
1010

1111
from . import utils
1212
from .utils import path_from_root, exit_with_error, __rootpath__, which
@@ -21,16 +21,16 @@
2121
NODE_JS = None
2222
BINARYEN_ROOT = None
2323
SPIDERMONKEY_ENGINE = None
24-
V8_ENGINE = None
24+
V8_ENGINE: Optional[List[str]] = None
2525
LLVM_ROOT = None
2626
LLVM_ADD_VERSION = None
2727
CLANG_ADD_VERSION = None
2828
CLOSURE_COMPILER = None
2929
JAVA = None
30-
JS_ENGINES = None
30+
JS_ENGINES: List[List[str]] = []
3131
WASMER = None
3232
WASMTIME = None
33-
WASM_ENGINES: List[str] = []
33+
WASM_ENGINES: List[List[str]] = []
3434
FROZEN_CACHE = None
3535
CACHE = None
3636
PORTS = None

tools/emcoverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import uuid
3737
from glob import glob
3838

39-
import coverage.cmdline
39+
import coverage.cmdline # type: ignore
4040

4141
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
4242

0 commit comments

Comments
 (0)