Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions mesonbuild/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


T = typing.TypeVar('T')
T_co = typing.TypeVar('T_co', covariant=True)


class StringProtocol(Protocol):
Expand All @@ -43,27 +44,56 @@ class ImmutableListProtocol(Protocol[T]):
def __iter__(self) -> typing.Iterator[T]: ...

@typing.overload
def __getitem__(self, index: int) -> T: ...
def __getitem__(self, index: int, /) -> T: ...
@typing.overload
def __getitem__(self, index: slice) -> typing.List[T]: ...
def __getitem__(self, index: slice, /) -> typing.List[T]: ...

def __contains__(self, item: T) -> bool: ...
def __contains__(self, item: T, /) -> bool: ...

def __reversed__(self) -> typing.Iterator[T]: ...

def __len__(self) -> int: ...

def __add__(self, other: typing.List[T]) -> typing.List[T]: ...
def __add__(self, other: typing.List[T], /) -> typing.List[T]: ...

def __eq__(self, other: typing.Any) -> bool: ...
def __ne__(self, other: typing.Any) -> bool: ...
def __le__(self, other: typing.Any) -> bool: ...
def __lt__(self, other: typing.Any) -> bool: ...
def __gt__(self, other: typing.Any) -> bool: ...
def __ge__(self, other: typing.Any) -> bool: ...
def __eq__(self, other: typing.Any, /) -> bool: ...
def __ne__(self, other: typing.Any, /) -> bool: ...
def __le__(self, other: typing.Any, /) -> bool: ...
def __lt__(self, other: typing.Any, /) -> bool: ...
def __gt__(self, other: typing.Any, /) -> bool: ...
def __ge__(self, other: typing.Any, /) -> bool: ...

def count(self, item: T) -> int: ...
def count(self, item: T, /) -> int: ...

def index(self, item: T) -> int: ...
def index(self, item: T, /) -> int: ...

def copy(self) -> typing.List[T]: ...


class Sequence(Protocol[T_co]):

"""A Sequence like type that does not allow str.

There are times where one wants to use a string as a sequence, but in that
case it would be better to write a `str | Sequence[str]` to be explicit.

This works because the `__contains__` method for `str` doesn't accept `object`.
"""

@typing.overload
def __getitem__(self, index: typing.SupportsIndex, /) -> T_co: ...

@typing.overload
def __getitem__(self, index: slice, /) -> typing.Sequence[T_co]: ...

def __contains__(self, value: object, /) -> bool: ...

def __len__(self) -> int: ...

def __iter__(self) -> typing.Iterator[T_co]: ...

def index(self, value: typing.Any, start: int = 0, stop: int = ..., /) -> int: ...

def count(self, value: typing.Any, /) -> int: ...

def __reversed__(self) -> typing.Iterator[T_co]: ...
26 changes: 13 additions & 13 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from typing_extensions import Literal, TypedDict

from . import environment
from ._typing import ImmutableListProtocol
from ._typing import ImmutableListProtocol, Sequence
from .backend.backends import Backend
from .compilers import Compiler
from .interpreter.interpreter import SourceOutputs, Interpreter
Expand Down Expand Up @@ -449,7 +449,7 @@ def __repr__(self) -> str:
return r.format(self.__class__.__name__, self.target.name, self.srclist)

@staticmethod
def get_sources(sources: T.Sequence['FileOrString'], generated_sources: T.Sequence['GeneratedTypes']) -> T.List['FileOrString']:
def get_sources(sources: Sequence['FileOrString'], generated_sources: Sequence['GeneratedTypes']) -> T.List['FileOrString']:
# Merge sources and generated sources
sources = list(sources)
for gensrc in generated_sources:
Expand All @@ -462,7 +462,7 @@ def get_sources(sources: T.Sequence['FileOrString'], generated_sources: T.Sequen
# Filter out headers and all non-source files
return [s for s in sources if is_source(s)]

def classify_all_sources(self, sources: T.List[FileOrString], generated_sources: T.Sequence['GeneratedTypes']) -> T.Dict['Compiler', T.List['FileOrString']]:
def classify_all_sources(self, sources: T.List[FileOrString], generated_sources: Sequence['GeneratedTypes']) -> T.Dict['Compiler', T.List['FileOrString']]:
sources_ = self.get_sources(sources, generated_sources)
return classify_unity_sources(self.target.compilers.values(), sources_)

Expand Down Expand Up @@ -1590,7 +1590,7 @@ def add_pch(self, language: str, pchlist: T.List[str]) -> None:
raise MesonException(f'File {f} does not exist.')
self.pch[language] = pchlist

def add_include_dirs(self, args: T.Sequence['IncludeDirs'], set_is_system: T.Optional[str] = None) -> None:
def add_include_dirs(self, args: Sequence['IncludeDirs'], set_is_system: T.Optional[str] = None) -> None:
ids: T.List['IncludeDirs'] = []
for a in args:
if not isinstance(a, IncludeDirs):
Expand Down Expand Up @@ -1798,7 +1798,7 @@ def process_vs_module_defs_kw(self, kwargs: T.Dict[str, T.Any]) -> None:
'a file object, a Custom Target, or a Custom Target Index')
self.process_link_depends(path)

def extract_targets_as_list(self, kwargs: T.Dict[str, T.Union[LibTypes, T.Sequence[LibTypes]]], key: T.Literal['link_with', 'link_whole']) -> T.List[LibTypes]:
def extract_targets_as_list(self, kwargs: T.Dict[str, T.Union[LibTypes, Sequence[LibTypes]]], key: T.Literal['link_with', 'link_whole']) -> T.List[LibTypes]:
bl_type = self.environment.coredata.optstore.get_value_for(OptionKey('default_both_libraries'))
if bl_type == 'auto':
if isinstance(self, StaticLibrary):
Expand Down Expand Up @@ -2791,7 +2791,7 @@ class CommandBase:
dependencies: T.List[T.Union[BuildTarget, 'CustomTarget']]
subproject: str

def flatten_command(self, cmd: T.Sequence[T.Union[str, File, programs.ExternalProgram, BuildTargetTypes]]) -> \
def flatten_command(self, cmd: Sequence[T.Union[str, File, programs.ExternalProgram, BuildTargetTypes]]) -> \
T.List[T.Union[str, File, BuildTarget, CustomTarget, programs.ExternalProgram]]:
cmd = listify(cmd)
final_cmd: T.List[T.Union[str, File, BuildTarget, 'CustomTarget']] = []
Expand Down Expand Up @@ -2859,10 +2859,10 @@ def __init__(self,
subdir: str,
subproject: str,
environment: environment.Environment,
command: T.Sequence[T.Union[
command: Sequence[T.Union[
str, BuildTargetTypes, GeneratedList,
programs.ExternalProgram, File]],
sources: T.Sequence[T.Union[
sources: Sequence[T.Union[
str, File, BuildTargetTypes, ExtractedObjects,
GeneratedList, programs.ExternalProgram]],
outputs: T.List[str],
Expand All @@ -2871,8 +2871,8 @@ def __init__(self,
build_by_default: T.Optional[bool] = None,
capture: bool = False,
console: bool = False,
depend_files: T.Optional[T.Sequence[FileOrString]] = None,
extra_depends: T.Optional[T.Sequence[T.Union[str, SourceOutputs]]] = None,
depend_files: T.Optional[Sequence[FileOrString]] = None,
extra_depends: T.Optional[Sequence[T.Union[str, SourceOutputs]]] = None,
depfile: T.Optional[str] = None,
env: T.Optional[EnvironmentVariables] = None,
feed: bool = False,
Expand Down Expand Up @@ -3121,8 +3121,8 @@ class RunTarget(Target, CommandBase):
typename = 'run'

def __init__(self, name: str,
command: T.Sequence[T.Union[str, File, BuildTargetTypes, programs.ExternalProgram]],
dependencies: T.Sequence[T.Union[Target, CustomTargetIndex]],
command: Sequence[T.Union[str, File, BuildTargetTypes, programs.ExternalProgram]],
dependencies: Sequence[T.Union[Target, CustomTargetIndex]],
subdir: str,
subproject: str,
environment: environment.Environment,
Expand Down Expand Up @@ -3171,7 +3171,7 @@ class AliasTarget(RunTarget):

typename = 'alias'

def __init__(self, name: str, dependencies: T.Sequence[Target],
def __init__(self, name: str, dependencies: Sequence[Target],
subdir: str, subproject: str, environment: environment.Environment):
super().__init__(name, [], dependencies, subdir, subproject, environment)

Expand Down
38 changes: 20 additions & 18 deletions mesonbuild/modules/_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from ..programs import NonExistingExternalProgram

if T.TYPE_CHECKING:
from .._typing import Sequence

from . import ModuleState
from ..dependencies.qt import QtPkgConfigDependency, QmakeQtDependency
from ..interpreter import Interpreter
Expand All @@ -39,15 +41,15 @@ class ResourceCompilerKwArgs(TypedDict):
"""Keyword arguments for the Resource Compiler method."""

name: T.Optional[str]
sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
sources: Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
extra_args: T.List[str]
method: str

class UICompilerKwArgs(TypedDict):

"""Keyword arguments for the Ui Compiler method."""

sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
sources: Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
extra_args: T.List[str]
method: str
preserve_paths: bool
Expand All @@ -56,8 +58,8 @@ class MocCompilerKwArgs(TypedDict):

"""Keyword arguments for the Moc Compiler method."""

sources: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
headers: T.Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
sources: Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
headers: Sequence[T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]]
extra_args: T.List[str]
method: str
include_directories: T.List[T.Union[str, build.IncludeDirs]]
Expand Down Expand Up @@ -98,8 +100,8 @@ class CompileTranslationsKwArgs(TypedDict):

class GenQrcKwArgs(TypedDict):

sources: T.Sequence[File]
aliases: T.Sequence[str]
sources: Sequence[File]
aliases: Sequence[str]
prefix: str
output: str

Expand All @@ -108,9 +110,9 @@ class GenQmldirKwArgs(TypedDict):
module_name: str
module_version: str
module_prefix: str
qml_sources: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_singletons: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_internals: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_sources: Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_singletons: Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_internals: Sequence[T.Union[FileOrString, build.GeneratedTypes]]
designer_supported: bool
imports: T.List[str]
optional_imports: T.List[str]
Expand All @@ -122,7 +124,7 @@ class GenQmldirKwArgs(TypedDict):
class GenQmlCachegenKwArgs(TypedDict):

target_name: str
qml_sources: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_sources: Sequence[T.Union[FileOrString, build.GeneratedTypes]]
qml_qrc: T.Union[FileOrString, build.GeneratedTypes]
extra_args: T.List[str]
module_prefix: str
Expand All @@ -146,7 +148,7 @@ class GenQmlTypeRegistrarKwArgs(TypedDict):
class MocJsonCollectKwArgs(TypedDict):

target_name: str
moc_json: T.Sequence[build.GeneratedList]
moc_json: Sequence[build.GeneratedList]
method: str

class QmlModuleKwArgs(TypedDict):
Expand Down Expand Up @@ -327,7 +329,7 @@ def _qrc_nodes(state: ModuleState, rcc_file: FileOrString) -> T.Tuple[str, T.Lis
def _parse_qrc_deps(self, state: ModuleState,
rcc_file_: T.Union[FileOrString, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList]) -> T.List[File]:
result: T.List[File] = []
inputs: T.Sequence['FileOrString'] = []
inputs: Sequence['FileOrString'] = []
if isinstance(rcc_file_, (str, File)):
inputs = [rcc_file_]
else:
Expand Down Expand Up @@ -745,7 +747,7 @@ def compile_translations(self, state: ModuleState, args: T.Tuple, kwargs: Compil
else:
return ModuleReturnValue(translations, [translations])

def _source_to_files(self, state: ModuleState, sources: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]) -> T.List[File]:
def _source_to_files(self, state: ModuleState, sources: Sequence[T.Union[FileOrString, build.GeneratedTypes]]) -> T.List[File]:

content_files = []
for s in sources:
Expand Down Expand Up @@ -803,7 +805,7 @@ def _gen_qmldir(self, state: ModuleState, kwargs: GenQmldirKwArgs) -> File:

with open(fileout_abs, 'w', encoding='utf-8') as fd:

def __gen_import(import_type: str, importlist: T.Sequence[str]) -> None:
def __gen_import(import_type: str, importlist: Sequence[str]) -> None:
for import_string in importlist:
match = import_re.match(import_string)
if not match:
Expand All @@ -812,7 +814,7 @@ def __gen_import(import_type: str, importlist: T.Sequence[str]) -> None:
version: str = match.group(4) or ''
fd.write(f'{import_type} {module} {version}\n')

def __gen_declaration(qualifier: str, version: str, importlist: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]]) -> None:
def __gen_declaration(qualifier: str, version: str, importlist: Sequence[T.Union[FileOrString, build.GeneratedTypes]]) -> None:
importpathlist = self._source_to_files(state, importlist)
for s in importpathlist:
basename: str = os.path.basename(s.fname)
Expand Down Expand Up @@ -853,7 +855,7 @@ def _moc_json_collect(self, state: ModuleState, kwargs: MocJsonCollectKwArgs) ->
self.tools['moc'].name + ' not found')

target_name: str = kwargs['target_name']
moc_json: T.Sequence[build.GeneratedList] = kwargs['moc_json']
moc_json: Sequence[build.GeneratedList] = kwargs['moc_json']

#there may be a better way :-/
input_args: T.List[str] = []
Expand Down Expand Up @@ -936,7 +938,7 @@ def _qml_type_registrar(self, state: ModuleState, kwargs: GenQmlTypeRegistrarKwA
target_name: str = kwargs['target_name']
collected_json: T.Optional[T.Union[FileOrString, build.CustomTarget]] = kwargs['collected_json']

inputs: T.Sequence[T.Union[FileOrString, build.CustomTarget]] = [collected_json] if collected_json else []
inputs: Sequence[T.Union[FileOrString, build.CustomTarget]] = [collected_json] if collected_json else []
outputs: T.List[str] = [f'{target_name}_qmltyperegistrations.cpp']
install_dir: T.List[T.Union[str, Literal[False]]] = [False]
install_tag: T.List[T.Union[str, None]] = [None]
Expand Down Expand Up @@ -1051,7 +1053,7 @@ def qml_module(self, state: ModuleState, args: T.Tuple[str], kwargs: QmlModuleKw
target_name = re.sub(r'[^A-Za-z0-9]', '_', module_name)

qrc_resouces: T.List[T.Union[FileOrString, build.GeneratedTypes]] = []
all_qml: T.Sequence[T.Union[FileOrString, build.GeneratedTypes]] = kwargs['qml_sources'] + kwargs['qml_singletons'] + kwargs['qml_internals']
all_qml: Sequence[T.Union[FileOrString, build.GeneratedTypes]] = kwargs['qml_sources'] + kwargs['qml_singletons'] + kwargs['qml_internals']
all_qml_files: T.List[File] = self._source_to_files(state, all_qml)
all_qml_basename: T.List[str] = [os.path.basename(p.fname) for p in all_qml_files]

Expand Down
Loading
Loading