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',