From 46f512263b524bd471730ff2d329e23fc6fc0df7 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Fri, 5 Dec 2025 10:32:34 -0800 Subject: [PATCH] mesondata: fix mypy warnings Mypy is wrong, because typeshed is wrong, this is just fine. We do need to add a version check, because mypy doesn't understand that the code inside the try can fail, and that's fine. The try/except remains, as it is needed for the zipapp case. --- mesonbuild/mesondata.py | 18 +++++++++++------- run_mypy.py | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/mesonbuild/mesondata.py b/mesonbuild/mesondata.py index 50a88857172a..4d53a492f442 100644 --- a/mesonbuild/mesondata.py +++ b/mesonbuild/mesondata.py @@ -6,6 +6,7 @@ import importlib.resources from pathlib import PurePosixPath, Path +import sys import typing as T if T.TYPE_CHECKING: @@ -24,13 +25,16 @@ def write_once(self, path: Path) -> None: path.write_text(data, encoding='utf-8') def write_to_private(self, env: 'Environment') -> Path: - try: - resource = importlib.resources.files('mesonbuild') / self.path - if isinstance(resource, Path): - return resource - except AttributeError: - # fall through to python 3.7 compatible code - pass + if sys.version_info >= (3, 9): + try: + # The issue that mypy/pyright see here is caused by a bug in typeshed: + # https://github.com/python/typeshed/pull/15108 + resource = importlib.resources.files('mesonbuild') / self.path # type: ignore[operator] + if isinstance(resource, Path): + return resource + except AttributeError: + # fall through to python 3.7 compatible code + pass out_file = Path(env.scratch_dir) / 'data' / self.path.name out_file.parent.mkdir(exist_ok=True) diff --git a/run_mypy.py b/run_mypy.py index 5823e090be93..48a75b1802a7 100755 --- a/run_mypy.py +++ b/run_mypy.py @@ -41,6 +41,7 @@ 'mesonbuild/interpreter/interpreterobjects.py', 'mesonbuild/interpreter/type_checking.py', 'mesonbuild/machinefile.py', + 'mesonbuild/mesondata.py', 'mesonbuild/mcompile.py', 'mesonbuild/mdevenv.py', 'mesonbuild/mconf.py',