Skip to content

Commit aec26fd

Browse files
authored
Enable preview checks in ruff by default. NFC (#26559)
The vast majority of the changes here fall into two catagories: 1. Adding `encoding='utf-8'` to file open operations. 2. Using set literals when testing membership. Also, update to the latest version of `ruff`.
1 parent 72ca2cf commit aec26fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+301
-286
lines changed

.circleci/config.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,6 @@ jobs:
539539
- checkout
540540
- pip-install
541541
- run: ruff check
542-
# TODO (cclauss): When ruff supports rules these errors without --preview, remove following line
543-
- run: ruff check --preview --select=E20,E30,E221,E225,E226,E275
544542
vulture:
545543
executor: ubuntu-lts
546544
steps:

emcc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def make_relative(filename):
127127
reproduce_file.add(utils.path_from_root('emscripten-version.txt'), os.path.join(root, 'version.txt'))
128128

129129
with shared.get_temp_files().get_file(suffix='.tar') as rsp_name:
130-
with open(rsp_name, 'w') as rsp:
130+
with open(rsp_name, 'w', encoding='utf-8') as rsp:
131131
ignore_next = False
132132
output_arg = None
133133

@@ -371,7 +371,7 @@ def get_next_arg():
371371
add_link_arg(get_next_arg())
372372
elif arg == '-s' or arg.startswith(('-l', '-L', '--js-library=', '-z', '-u')):
373373
add_link_arg(arg)
374-
elif not arg.startswith('-o') and arg not in ('-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'):
374+
elif not arg.startswith('-o') and arg not in {'-nostdlib', '-nostartfiles', '-nolibc', '-nodefaultlibs', '-s'}:
375375
# All other flags are for the compiler
376376
compiler_args.append(arg)
377377
if skip:
@@ -415,7 +415,7 @@ def phase_setup(state):
415415
# If we get here then the user specified both DISABLE_EXCEPTION_CATCHING and EXCEPTION_CATCHING_ALLOWED
416416
# on the command line. This is no longer valid so report either an error or a warning (for
417417
# backwards compat with the old `DISABLE_EXCEPTION_CATCHING=2`
418-
if user_settings['DISABLE_EXCEPTION_CATCHING'] in ('0', '2'):
418+
if user_settings['DISABLE_EXCEPTION_CATCHING'] in {'0', '2'}:
419419
diagnostics.warning('deprecated', 'DISABLE_EXCEPTION_CATCHING=X is no longer needed when specifying EXCEPTION_CATCHING_ALLOWED')
420420
else:
421421
exit_with_error('DISABLE_EXCEPTION_CATCHING and EXCEPTION_CATCHING_ALLOWED are mutually exclusive')

emcmake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Main run() function
1717
#
1818
def run():
19-
if len(sys.argv) < 2 or sys.argv[1] in ('--version', '--help'):
19+
if len(sys.argv) < 2 or sys.argv[1] in {'--version', '--help'}:
2020
print('''\
2121
emcmake is a helper for cmake, setting various environment
2222
variables so that emcc etc. are used. Typical usage:

emconfigure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Main run() function
2828
#
2929
def run():
30-
if len(sys.argv) < 2 or sys.argv[1] in ('--version', '--help'):
30+
if len(sys.argv) < 2 or sys.argv[1] in {'--version', '--help'}:
3131
print('''\
3232
emconfigure is a helper for configure, setting various environment
3333
variables so that emcc etc. are used. Typical usage:

emmake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# Main run() function
3434
#
3535
def run():
36-
if len(sys.argv) < 2 or sys.argv[1] in ('--version', '--help'):
36+
if len(sys.argv) < 2 or sys.argv[1] in {'--version', '--help'}:
3737
print('''\
3838
emmake is a helper for make, setting various environment
3939
variables so that emcc etc. are used. Typical usage:

emrun.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
from operator import itemgetter
4141
from urllib.parse import unquote, urlsplit
4242

43-
# We depend on python 3.8 features
44-
if sys.version_info < (3, 8): # noqa: UP036
45-
print(f'error: emrun requires python 3.8 or above ({sys.executable} {sys.version})', file=sys.stderr)
46-
sys.exit(1)
43+
assert sys.version_info >= (3, 10), f'emscripten requires python 3.10 or above ({sys.executable} {sys.version})'
4744

4845
# Populated from cmdline params
4946
emrun_options = None
@@ -109,7 +106,7 @@
109106
LINUX = True
110107
elif platform.system() == 'FreeBSD':
111108
FREEBSD = True
112-
elif platform.mac_ver()[0] != '':
109+
elif platform.mac_ver()[0]:
113110
MACOS = True
114111
import plistlib
115112

@@ -225,7 +222,7 @@ def delete_emrun_safe_firefox_profile():
225222
def create_emrun_safe_firefox_profile():
226223
global temp_firefox_profile_dir
227224
temp_firefox_profile_dir = tempfile.mkdtemp(prefix='temp_emrun_firefox_profile_')
228-
with open(os.path.join(temp_firefox_profile_dir, 'prefs.js'), 'w') as f:
225+
with open(os.path.join(temp_firefox_profile_dir, 'prefs.js'), 'w', encoding='utf-8') as f:
229226
f.write('''
230227
// Old Firefox browsers have a maxPerDomain limit of 20. Newer Firefox browsers default to 512. Match the new
231228
// default here to help test spawning a lot of threads also on older Firefox versions.
@@ -780,7 +777,7 @@ def get_cpu_info():
780777
logical_cores = int(check_output(['sysctl', '-n', 'machdep.cpu.thread_count']).strip())
781778
frequency = int(check_output(['sysctl', '-n', 'hw.cpufrequency']).strip()) // 1000000
782779
elif LINUX:
783-
for line in open('/proc/cpuinfo').readlines():
780+
for line in open('/proc/cpuinfo', encoding='utf-8').readlines():
784781
if 'model name' in line:
785782
cpu_name = re.sub('.*model name.*:', '', line, count=1).strip()
786783
lscpu = check_output(['lscpu'])
@@ -1031,7 +1028,7 @@ def win_get_file_properties(fname):
10311028
strInfo = {}
10321029
for propName in propNames:
10331030
strInfoPath = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName)
1034-
## print str_info
1031+
# print str_info
10351032
strInfo[propName] = win32api.GetFileVersionInfo(fname, strInfoPath)
10361033

10371034
props['StringFileInfo'] = strInfo
@@ -1043,7 +1040,7 @@ def get_computer_model():
10431040
try:
10441041
if MACOS:
10451042
try:
1046-
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'r') as f:
1043+
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), encoding='utf-8') as f:
10471044
model = f.read()
10481045
return model
10491046
except IOError:
@@ -1059,7 +1056,7 @@ def get_computer_model():
10591056
model = check_output(cmd)
10601057
model = re.search('<configCode>(.*)</configCode>', model)
10611058
model = model.group(1).strip()
1062-
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'w') as fh:
1059+
with open(os.path.join(os.getenv("HOME"), '.emrun.hwmodel.cached'), 'w', encoding='utf-8') as fh:
10631060
fh.write(model) # Cache the hardware model to disk
10641061
return model
10651062
except Exception:
@@ -1089,7 +1086,7 @@ def get_computer_model():
10891086

10901087

10911088
def get_os_version():
1092-
bitness = ' (64bit)' if platform.machine() in ['AMD64', 'x86_64'] else ' (32bit)'
1089+
bitness = ' (64bit)' if platform.machine() in {'AMD64', 'x86_64'} else ' (32bit)'
10931090
try:
10941091
if WINDOWS:
10951092
versionHandle = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
@@ -1116,7 +1113,7 @@ def get_system_memory():
11161113
if emrun_options.android:
11171114
lines = check_output([ADB, 'shell', 'cat', '/proc/meminfo']).split('\n')
11181115
else:
1119-
mem = open('/proc/meminfo', 'r')
1116+
mem = open('/proc/meminfo', encoding='utf-8')
11201117
lines = mem.readlines()
11211118
mem.close()
11221119
for i in lines:
@@ -1383,13 +1380,14 @@ def get_system_info(format_json):
13831380
return info.strip()
13841381
else:
13851382
try:
1386-
with open(os.path.expanduser('~/.emrun.generated.guid')) as fh:
1383+
with open(os.path.expanduser('~/.emrun.generated.guid'), encoding='utf-8') as fh:
13871384
unique_system_id = fh.read().strip()
13881385
except Exception:
13891386
import uuid
13901387
unique_system_id = str(uuid.uuid4())
13911388
try:
1392-
open(os.path.expanduser('~/.emrun.generated.guid'), 'w').write(unique_system_id)
1389+
with open(os.path.expanduser('~/.emrun.generated.guid'), 'w', encoding='utf-8') as f:
1390+
f.write(unique_system_id)
13931391
except Exception as e:
13941392
logv(e)
13951393

@@ -1822,13 +1820,13 @@ def run(cmd):
18221820

18231821
if options.log_stdout:
18241822
global browser_stdout_handle
1825-
browser_stdout_handle = open(options.log_stdout, 'a')
1823+
browser_stdout_handle = open(options.log_stdout, 'a', encoding='utf-8')
18261824
if options.log_stderr:
18271825
global browser_stderr_handle
18281826
if options.log_stderr == options.log_stdout:
18291827
browser_stderr_handle = browser_stdout_handle
18301828
else:
1831-
browser_stderr_handle = open(options.log_stderr, 'a')
1829+
browser_stderr_handle = open(options.log_stderr, 'a', encoding='utf-8')
18321830
if options.run_browser:
18331831
logv("Starting browser: %s" % ' '.join(browser))
18341832
# if browser[0] == 'cmd':

pyproject.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
requires-python = ">=3.10"
33

44
[tool.ruff]
5+
preview = true
56
line-length = 100
7+
indent-width = 2
68
exclude = [
79
"./cache/",
810
"./node_modules/",
@@ -38,15 +40,26 @@ lint.ignore = [
3840
"B011", # See https://github.com/PyCQA/flake8-bugbear/issues/66
3941
"B023",
4042
"B026",
43+
"E272",
4144
"E402",
45+
"E241",
46+
"E266",
4247
"E501",
4348
"E721",
4449
"E741",
50+
"E111", # Does not seem to honor `indent-width = 2` above
51+
"E114", # Does not seem to honor `indent-width = 2` above
52+
"E261",
4553
"PERF203",
4654
"PERF401",
4755
"PLC0415",
56+
"PLR0904",
57+
"PLR0916",
58+
"PLR0914",
59+
"PLR1702",
4860
"PLR1704",
4961
"PLR5501",
62+
"PLR6301",
5063
"PLW0602",
5164
"PLW0603",
5265
"PLW1510",

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
coverage[toml]==6.5
88
mypy==1.14
99
psutil==7.0.0
10-
ruff==0.14.1
10+
ruff==0.15.7
1111
types-requests==2.32.0.20241016
1212
unittest-xml-reporting==3.2.0
1313
deadcode==2.3.1

site/source/get_api_items.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def addapiitems(matchobj):
6565
filepath = api_reference_directory + file
6666
print(file)
6767
# open file
68-
with open(filepath) as infile:
68+
with open(filepath, encoding='utf-8') as infile:
6969
for line in infile:
7070
# parse line for API items
7171
re.sub(r'^\.\.\s+((\w+)\:(\w+)\:\:(.*))', addapiitems, line)
@@ -74,7 +74,7 @@ def addapiitems(matchobj):
7474
def exportItems():
7575
"""Export the API items into form for use in another script.
7676
"""
77-
with open(api_item_filename, 'w') as infile:
77+
with open(api_item_filename, 'w', encoding='utf-8') as infile:
7878
# write function lead in
7979
infile.write("# Auto-generated file (see get_api_items.py)\n\ndef get_mapped_items():\n mapped_wiki_inline_code = dict()\n")
8080

site/source/get_wiki.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
wiki_checkout = 'emscripten.wiki/'
3939
temp_set_of_codemarkup = set()
40-
logfile = open(logfilename, 'w')
40+
logfile = open(logfilename, 'w', encoding='utf-8')
4141
# snapshot_version_information = '.. note:: This is a **snapshot** of the wiki: %s\n\n' % strftime("%a, %d %b %Y %H:%M", gmtime())
4242
snapshot_version_information = '.. note:: This article was migrated from the wiki (%s) and is now the "master copy" (the version in the wiki will be deleted). It may not be a perfect rendering of the original but we hope to fix that soon!\n\n' % time.strftime("%a, %d %b %Y %H:%M", time.gmtime())
4343

@@ -91,7 +91,7 @@ def ConvertFilesToRst():
9191
continue
9292

9393
inputfilename = wiki_checkout + file
94-
markdown = Path(inputfilename).read_text()
94+
markdown = Path(inputfilename).read_text(encoding='utf-8')
9595
if 'This article has moved from the wiki to the new site' in markdown:
9696
continue
9797
if 'This page has been migrated to the main site' in markdown:
@@ -127,16 +127,16 @@ def ConvertFilesToRst():
127127

128128
textinfile += snapshot_version_information
129129

130-
with open(outputfilename) as infile:
130+
with open(outputfilename, encoding='utf-8') as infile:
131131
for line in infile:
132132
textinfile += line
133133

134134
# print textinfile
135-
with open(outputfilename, 'w') as outfile:
135+
with open(outputfilename, 'w', encoding='utf-8') as outfile:
136136
outfile.write(textinfile)
137137

138138
# write the index
139-
with open(output_dir + 'index.rst', 'w') as outfile:
139+
with open(output_dir + 'index.rst', 'w', encoding='utf-8') as outfile:
140140
outfile.write(indexfiletext)
141141

142142

@@ -184,7 +184,7 @@ def fixcodemarkuplinks(matchobj):
184184
input_file = output_dir + file
185185
# print input_file
186186
textinfile = ''
187-
with open(input_file) as infile:
187+
with open(input_file, encoding='utf-8') as infile:
188188
for line in infile:
189189
textinfile += line
190190

@@ -195,7 +195,7 @@ def fixcodemarkuplinks(matchobj):
195195
# convert codemarkup to links if possible
196196
textinfile = fixWikiCodeMarkupToCodeLinks(textinfile)
197197

198-
with open(input_file, 'w') as outfile:
198+
with open(input_file, 'w', encoding='utf-8') as outfile:
199199
outfile.write(textinfile)
200200

201201
logfile.write('\n\nCODE MARKUP THAT WONT BE LINKED (add entry to mapped_wiki_inline_code if one of these need to be linked. The tool get-api-items.py can be used to generate the list of the documented API items. \n')

0 commit comments

Comments
 (0)