dependencies: Use a typing.NewType for Dependency.type_name

This allow mypy to catch cases where we accidently assign the dependency
name to the type_name, as it sees them as having different types (though
at runtime they're all strings).
pull/8878/head
Dylan Baker 3 years ago
parent 203a548d60
commit 0412bdd753
  1. 13
      mesonbuild/dependencies/base.py
  2. 4
      mesonbuild/dependencies/cmake.py
  3. 4
      mesonbuild/dependencies/configtool.py
  4. 4
      mesonbuild/dependencies/dub.py
  5. 13
      mesonbuild/dependencies/factory.py
  6. 4
      mesonbuild/dependencies/framework.py
  7. 4
      mesonbuild/dependencies/pkgconfig.py
  8. 4
      mesonbuild/dependencies/platform.py
  9. 4
      mesonbuild/dependencies/system.py
  10. 6
      mesonbuild/dependencies/ui.py

@ -60,6 +60,9 @@ class DependencyMethods(Enum):
DUB = 'dub'
DependencyTypeName = T.NewType('DependencyTypeName', str)
class Dependency:
@classmethod
@ -72,7 +75,7 @@ class Dependency:
raise DependencyException("include_type may only be one of ['preserve', 'system', 'non-system']")
return kwargs['include_type']
def __init__(self, type_name: str, kwargs: T.Dict[str, T.Any]) -> None:
def __init__(self, type_name: DependencyTypeName, kwargs: T.Dict[str, T.Any]) -> None:
self.name = "null"
self.version: T.Optional[str] = None
self.language: T.Optional[str] = None # None means C-like
@ -220,7 +223,7 @@ class InternalDependency(Dependency):
link_args: T.List[str], libraries: T.List['BuildTarget'],
whole_libraries: T.List['BuildTarget'], sources: T.List['FileOrString'],
ext_deps: T.List[Dependency], variables: T.Dict[str, T.Any]):
super().__init__('internal', {})
super().__init__(DependencyTypeName('internal'), {})
self.version = version
self.is_found = True
self.include_directories = incdirs
@ -307,7 +310,7 @@ class HasNativeKwarg:
return MachineChoice.BUILD if kwargs.get('native', False) else MachineChoice.HOST
class ExternalDependency(Dependency, HasNativeKwarg):
def __init__(self, type_name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None):
def __init__(self, type_name: DependencyTypeName, environment: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None):
Dependency.__init__(self, type_name, kwargs)
self.env = environment
self.name = type_name # default
@ -392,7 +395,7 @@ class ExternalDependency(Dependency, HasNativeKwarg):
class NotFoundDependency(Dependency):
def __init__(self, environment: 'Environment') -> None:
super().__init__('not-found', {})
super().__init__(DependencyTypeName('not-found'), {})
self.env = environment
self.name = 'not-found'
self.is_found = False
@ -406,7 +409,7 @@ class NotFoundDependency(Dependency):
class ExternalLibrary(ExternalDependency):
def __init__(self, name: str, link_args: T.List[str], environment: 'Environment',
language: str, silent: bool = False) -> None:
super().__init__('library', environment, {}, language=language)
super().__init__(DependencyTypeName('library'), environment, {}, language=language)
self.name = name
self.language = language
self.is_found = False

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import ExternalDependency, DependencyException, DependencyMethods
from .base import ExternalDependency, DependencyException, DependencyMethods, DependencyTypeName
from ..mesonlib import is_windows, MesonException, OptionKey, PerMachine, stringlistify, extract_as_list
from ..mesondata import mesondata
from ..cmake import CMakeExecutor, CMakeTraceParser, CMakeException, CMakeToolchain, CMakeExecScope, check_cmake_args
@ -98,7 +98,7 @@ class CMakeDependency(ExternalDependency):
# Ensure that the list is unique
self.language_list = list(set(self.language_list))
super().__init__('cmake', environment, kwargs, language=language)
super().__init__(DependencyTypeName('cmake'), environment, kwargs, language=language)
self.name = name
self.is_libtool = False
# Store a copy of the CMake path on the object itself so it is

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import ExternalDependency, DependencyException, DependencyMethods
from .base import ExternalDependency, DependencyException, DependencyMethods, DependencyTypeName
from ..mesonlib import listify, Popen_safe, split_args, version_compare, version_compare_many
from ..programs import find_external_program
from .. import mlog
@ -41,7 +41,7 @@ class ConfigToolDependency(ExternalDependency):
__strip_version = re.compile(r'^[0-9][0-9.]+')
def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None):
super().__init__('config-tool', environment, kwargs, language=language)
super().__init__(DependencyTypeName('config-tool'), environment, kwargs, language=language)
self.name = name
# You may want to overwrite the class version in some cases
self.tools = listify(kwargs.get('tools', self.tools))

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import ExternalDependency, DependencyException, DependencyMethods
from .base import ExternalDependency, DependencyException, DependencyMethods, DependencyTypeName
from .pkgconfig import PkgConfigDependency
from ..mesonlib import Popen_safe
from ..programs import ExternalProgram
@ -32,7 +32,7 @@ class DubDependency(ExternalDependency):
class_dubbin = None
def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
super().__init__('dub', environment, kwargs, language='d')
super().__init__(DependencyTypeName('dub'), environment, kwargs, language='d')
self.name = name
self.module_path: T.Optional[str] = None

@ -1,4 +1,5 @@
# Copyright 2013-2021 The Meson development team
# Copyright © 2021 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -12,15 +13,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import Dependency, ExternalDependency
import functools
import typing as T
from ..mesonlib import MachineChoice
from .base import DependencyException, DependencyMethods
from .base import ExternalDependency
from .base import process_method_kw
from .cmake import CMakeDependency
from .framework import ExtraFrameworkDependency
from .pkgconfig import PkgConfigDependency
from ..mesonlib import MachineChoice
import functools
import typing as T
from .system import SystemDependency
if T.TYPE_CHECKING:
from ..environment import Environment
@ -78,7 +81,7 @@ class DependencyFactory:
configtool_class: 'T.Optional[T.Type[ConfigToolDependency]]' = None,
framework_name: T.Optional[str] = None,
framework_class: 'T.Type[ExtraFrameworkDependency]' = ExtraFrameworkDependency,
system_class: 'T.Type[ExternalDependency]' = ExternalDependency):
system_class: 'T.Type[SystemDependency]' = SystemDependency):
if DependencyMethods.CONFIG_TOOL in methods and not configtool_class:
raise DependencyException('A configtool must have a custom class')

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import ExternalDependency, DependencyException, DependencyMethods
from .base import DependencyTypeName, ExternalDependency, DependencyException, DependencyMethods
from ..mesonlib import MesonException, Version, stringlistify
from .. import mlog
from pathlib import Path
@ -26,7 +26,7 @@ class ExtraFrameworkDependency(ExternalDependency):
def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None) -> None:
paths = stringlistify(kwargs.get('paths', []))
super().__init__('extraframeworks', env, kwargs, language=language)
super().__init__(DependencyTypeName('extraframeworks'), env, kwargs, language=language)
self.name = name
# Full path to framework directory
self.framework_path: T.Optional[str] = None

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import ExternalDependency, DependencyException, DependencyMethods, sort_libpaths
from .base import ExternalDependency, DependencyException, DependencyMethods, sort_libpaths, DependencyTypeName
from ..mesonlib import LibType, MachineChoice, OptionKey, OrderedSet, PerMachine, Popen_safe
from ..programs import find_external_program, ExternalProgram
from .. import mlog
@ -36,7 +36,7 @@ class PkgConfigDependency(ExternalDependency):
] = {}
def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None) -> None:
super().__init__('pkgconfig', environment, kwargs, language=language)
super().__init__(DependencyTypeName('pkgconfig'), environment, kwargs, language=language)
self.name = name
self.is_libtool = False
# Store a copy of the pkg-config path on the object itself so it is

@ -15,7 +15,7 @@
# This file contains the detection logic for external dependencies that are
# platform-specific (generally speaking).
from .base import ExternalDependency, DependencyException
from .base import DependencyTypeName, ExternalDependency, DependencyException
from ..mesonlib import MesonException
import typing as T
@ -24,7 +24,7 @@ if T.TYPE_CHECKING:
class AppleFrameworks(ExternalDependency):
def __init__(self, env: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
super().__init__('appleframeworks', env, kwargs)
super().__init__(DependencyTypeName('appleframeworks'), env, kwargs)
modules = kwargs.get('modules', [])
if isinstance(modules, str):
modules = [modules]

@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Intel Corporation
from .base import ExternalDependency, DependencyMethods
from .base import DependencyTypeName, ExternalDependency, DependencyMethods
import typing as T
if T.TYPE_CHECKING:
@ -18,7 +18,7 @@ class SystemDependency(ExternalDependency):
def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any],
language: T.Optional[str] = None) -> None:
super().__init__('system', env, kwargs, language=language)
super().__init__(DependencyTypeName('system'), env, kwargs, language=language)
self.name = name
@staticmethod

@ -25,7 +25,7 @@ from ..mesonlib import (
)
from ..environment import detect_cpu_family
from .base import DependencyException, DependencyMethods
from .base import DependencyException, DependencyMethods, DependencyTypeName
from .configtool import ConfigToolDependency
from .factory import DependencyFactory
from .system import SystemDependency
@ -233,7 +233,8 @@ class VulkanDependencySystem(SystemDependency):
if not os.path.isfile(header):
raise DependencyException('VULKAN_SDK point to invalid directory (no include)')
self.type_name = 'vulkan_sdk'
# XXX: this is very odd, and may deserve being removed
self.type_name = DependencyTypeName('vulkan_sdk')
self.is_found = True
self.compile_args.append('-I' + inc_path)
self.link_args.append('-L' + lib_path)
@ -246,7 +247,6 @@ class VulkanDependencySystem(SystemDependency):
# simply try to guess it, usually works on linux
libs = self.clib_compiler.find_library('vulkan', environment, [])
if libs is not None and self.clib_compiler.has_header('vulkan/vulkan.h', '', environment, disable_cache=True)[0]:
self.type_name = 'system'
self.is_found = True
for lib in libs:
self.link_args.append(lib)

Loading…
Cancel
Save