change: fix linting errors in python files

This commit is contained in:
Peter Dragun
2025-07-28 10:11:04 +02:00
parent 519042a1e2
commit 300ff9fc78
3 changed files with 26 additions and 48 deletions

View File

@@ -6,7 +6,6 @@ import argparse
import os
import re
import sys
from typing import Optional
try:
from packaging.requirements import Requirement
@@ -24,13 +23,6 @@ from importlib.metadata import PackageNotFoundError
from importlib.metadata import requires as _requires
from importlib.metadata import version as _version
try:
from typing import Set
except ImportError:
# This is a script run during the early phase of setting up the environment. So try to avoid failure caused by
# Python version incompatibility. The supported Python version is checked elsewhere.
pass
PYTHON_PACKAGE_RE = re.compile(r'[^<>=~]+')
@@ -44,7 +36,7 @@ def validate_requirement_list(file_path: str) -> str:
)
try:
open(file_path, encoding='utf-8').close()
except IOError as e:
except OSError as e:
raise argparse.ArgumentTypeError(f'Cannot read requirement file {file_path}: {e}')
return file_path
@@ -68,7 +60,7 @@ def get_version(name: str) -> str:
return version
def get_requires(name: str) -> Optional[list]:
def get_requires(name: str) -> list | None:
try:
requires = _requires(name)
except PackageNotFoundError:
@@ -112,20 +104,20 @@ if __name__ == '__main__':
elif con.startswith('-e') and '#egg=' in con:
con_m = re.search(r'#egg=([^\s]+)', con)
if not con_m:
print('Malformed input. Cannot find name in {}'.format(con))
print(f'Malformed input. Cannot find name in {con}')
sys.exit(1)
con = con_m[1]
name_m = PYTHON_PACKAGE_RE.search(con)
if not name_m:
print('Malformed input. Cannot find name in {}'.format(con))
print(f'Malformed input. Cannot find name in {con}')
sys.exit(1)
constr_dict[name_m[0]] = con.partition(' #')[0] # remove comments
not_satisfied = [] # in string form which will be printed
# already_checked set is used in order to avoid circular checks which would cause looping.
already_checked: Set[Requirement] = set()
already_checked: set[Requirement] = set()
# required_set contains package names in string form without version constraints. If the package has a constraint
# specification (package name + version requirement) then use that instead. new_req_list is used to store
@@ -192,7 +184,7 @@ if __name__ == '__main__':
# We are running inside a private virtual environment under IDF_TOOLS_PATH,
# ask the user to run install.bat again.
install_script = 'install.bat' if sys.platform == 'win32' else 'install.sh'
print('To install the missing packages, please run "{}"'.format(install_script))
print(f'To install the missing packages, please run "{install_script}"')
else:
print(
'Please follow the instructions found in the "Set up the tools" section of '
@@ -202,7 +194,7 @@ if __name__ == '__main__':
print('Diagnostic information:')
idf_python_env_path = os.environ.get('IDF_PYTHON_ENV_PATH')
print(' IDF_PYTHON_ENV_PATH: {}'.format(idf_python_env_path or '(not set)'))
print(' Python interpreter used: {}'.format(sys.executable))
print(f' Python interpreter used: {sys.executable}')
if not idf_python_env_path or idf_python_env_path not in sys.executable:
print(' Warning: python interpreter not running from IDF_PYTHON_ENV_PATH')
print(' PATH: {}'.format(os.getenv('PATH')))

View File

@@ -1,39 +1,33 @@
#!/usr/bin/env python
#
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import argparse
import subprocess
from sys import exit
try:
from typing import List
except ImportError:
# Only used for type annotations
pass
import sys
IGNORE_LIST_MYPY = 'tools/ci/mypy_ignore_list.txt'
def types_valid_global_rules(file_name, ignorelisted): # type: (str, bool) -> bool
def types_valid_global_rules(file_name: str, ignorelisted: bool) -> bool:
"""
Run Mypy check with global rules on the given file, return TRUE if Mypy check passes
"""
output = subprocess.DEVNULL if ignorelisted else None
mypy_exit_code = subprocess.call('mypy {}'.format(file_name), shell=True, stdout=output)
mypy_exit_code = subprocess.call(f'mypy {file_name}', shell=True, stdout=output)
return not bool(mypy_exit_code)
def types_valid_ignored_rules(file_name): # type: (str) -> bool
def types_valid_ignored_rules(file_name: str) -> bool:
"""
Run Mypy check with rules for ignore list on the given file, return TRUE if Mypy check passes
"""
mypy_exit_code = subprocess.call('mypy {} --python-version 3.10 --allow-untyped-defs'.format(file_name), shell=True)
mypy_exit_code = subprocess.call(f'mypy {file_name} --python-version 3.10 --allow-untyped-defs', shell=True)
return not bool(mypy_exit_code)
def check_files(files): # type: (List[str]) -> List[str]
def check_files(files: list[str]) -> list[str]:
"""
Check files for type annotatins:
- new python file -> run Mypy check with global rules
@@ -45,7 +39,7 @@ def check_files(files): # type: (List[str]) -> List[str]
"""
type_issues = []
with open(IGNORE_LIST_MYPY, 'r') as f:
with open(IGNORE_LIST_MYPY) as f:
ignore_list = [item.strip() for item in f.readlines()]
updated_ignore_list = ignore_list.copy()
@@ -53,7 +47,7 @@ def check_files(files): # type: (List[str]) -> List[str]
if file_name in ignore_list:
if types_valid_global_rules(file_name, ignorelisted=True):
updated_ignore_list.remove(file_name)
print('\33[93m\n File {} removed from ignore list - run commit again! \n\33[0m'.format(file_name))
print(f'\33[93m\n File {file_name} removed from ignore list - run commit again! \n\33[0m')
continue
if types_valid_ignored_rules(file_name):
@@ -68,12 +62,12 @@ def check_files(files): # type: (List[str]) -> List[str]
if updated_ignore_list != ignore_list:
with open(IGNORE_LIST_MYPY, 'w') as f:
for item in updated_ignore_list:
f.write('{}\n'.format(item))
f.write(f'{item}\n')
return type_issues
def main(): # type: () -> None
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
args = parser.parse_args()
@@ -84,7 +78,7 @@ def main(): # type: () -> None
print('mypy check failed for:')
for file_name in type_issues:
print('\t', file_name)
exit(1)
sys.exit(1)
if __name__ == '__main__':

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -11,32 +11,24 @@
# (python3.10, python3.11, ...) cannot be hardcoded there and at the end, the user is responsible to set-up a system
# where "python" or "python3" of compatible version is available.
import sys
try:
# Python 2 is not supported anymore but still the old way of typing is used here in order to give a nice Python
# version failure and not a typing exception.
from typing import Iterable
except ImportError:
pass
from collections.abc import Iterable
OLDEST_PYTHON_SUPPORTED = (3, 10) # keep it as tuple for comparison with sys.version_info
def _ver_to_str(it): # type: (Iterable) -> str
def _ver_to_str(it: Iterable) -> str:
return '.'.join(str(x) for x in it)
def is_supported(): # type: () -> bool
def is_supported() -> bool:
return sys.version_info[:2] >= OLDEST_PYTHON_SUPPORTED[:2]
def check(): # type: () -> None
def check() -> None:
if not is_supported():
raise RuntimeError(
'ESP-IDF supports Python {} or newer but you are using Python {}. Please upgrade your '
'installation as described in the documentation.'.format(
_ver_to_str(OLDEST_PYTHON_SUPPORTED), _ver_to_str(sys.version_info[:3])
)
f'ESP-IDF supports Python {_ver_to_str(OLDEST_PYTHON_SUPPORTED)} or newer but you are using Python '
f'{_ver_to_str(sys.version_info[:3])}. Please upgrade your installation as described in the documentation.'
)