Skip to content

Commit

Permalink
Added missing inline types for public api
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 6, 2022
1 parent a54d89e commit cf27ffe
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 105 deletions.
7 changes: 4 additions & 3 deletions PyInstaller/__init__.py
Expand Up @@ -55,11 +55,12 @@
# Where to put all the temporary files; .log, .pyz, etc.
DEFAULT_WORKPATH = os.path.join(os.getcwd(), 'build')

PLATFORM = compat.system + '-' + compat.architecture
_PLATFORM = compat.system + '-' + compat.architecture
PLATFORM = _PLATFORM
# Include machine name in path to bootloader for some machines (e.g., 'arm'). Explicitly avoid doing this on macOS,
# where we keep universal2 bootloaders in Darwin-64bit folder regardless of whether we are on x86_64 or arm64.
if compat.machine and not compat.is_darwin:
PLATFORM += '-' + compat.machine
PLATFORM = _PLATFORM + '-' + compat.machine
# Similarly, disambiguate musl Linux from glibc Linux.
if compat.is_musl:
PLATFORM += '-musl'
PLATFORM = _PLATFORM + '-musl'
7 changes: 6 additions & 1 deletion PyInstaller/__main__.py
Expand Up @@ -11,17 +11,22 @@
"""
Main command-line interface to PyInstaller.
"""
from __future__ import annotations

import argparse
import os
import platform
from collections import defaultdict
from typing import Dict, Iterable, List, Tuple, Union

from PyInstaller import __version__
from PyInstaller import log as logging
# Note: do not import anything else until compat.check_requirements function is run!
from PyInstaller import compat

_PyIConfig = Union[Dict[str, Union[bool, str, List[str], None]], Iterable[Tuple[str, Union[bool, str, List[str],
None]]]]

logger = logging.getLogger(__name__)

# Taken from https://stackoverflow.com/a/22157136 to format args more flexibly: any help text which beings with ``R|``
Expand Down Expand Up @@ -146,7 +151,7 @@ def generate_parser() -> _PyiArgumentParser:
return parser


def run(pyi_args=None, pyi_config=None):
def run(pyi_args: Iterable[str] | None = None, pyi_config: _PyIConfig | None = None):
"""
pyi_args allows running PyInstaller programmatically without a subprocess
pyi_config allows checking configuration once when running multiple tests
Expand Down
75 changes: 51 additions & 24 deletions PyInstaller/compat.py
Expand Up @@ -11,6 +11,7 @@
"""
Various classes and functions to provide some backwards-compatibility with previous versions of Python onward.
"""
from __future__ import annotations

import errno

Expand All @@ -22,12 +23,29 @@
import subprocess
import sys
import shutil
from typing import Iterable, List, Tuple, Union

from PyInstaller._shared_with_waf import _pyi_machine
from PyInstaller.exceptions import ExecCommandFailed

if sys.version_info >= (3, 9):
_StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
else:
_StrOrBytesPath = Union[str, bytes, os.PathLike]
_OpenFile = Union[_StrOrBytesPath, int]

if sys.version_info >= (3, 8):
from typing import Literal
_Architecture = Literal['64bit', 'n32bit', '32bit']
_System = Literal['Cygwin', 'Linux', 'Darwin', 'Java', 'Windows']
_Machine = Literal['sw_64', 'loongarch64', 'arm', 'intel', 'ppc', 'mips', 'riscv', 's390x', 'unknown']
else:
_Architecture = str
_System = str
_Machine = str

# Copied from https://docs.python.org/3/library/platform.html#cross-platform.
is_64bits = sys.maxsize > 2**32
is_64bits: bool = sys.maxsize > 2**32

# Distinguish specific code for various Python versions. Variables 'is_pyXY' mean that Python X.Y and up is supported.
# Keep even unsupported versions here to keep 3rd-party hooks working.
Expand Down Expand Up @@ -72,9 +90,9 @@
# disabling the compatibility mode and using python that does not properly support Big Sur still leaves find_library()
# broken (which is a scenario that we ignore at the moment).
# The same logic applies to macOS 12 (Monterey).
is_macos_11_compat = _macos_ver and _macos_ver[0:2] == (10, 16) # Big Sur or newer in compat mode
is_macos_11_native = _macos_ver and _macos_ver[0:2] >= (11, 0) # Big Sur or newer in native mode
is_macos_11 = is_macos_11_compat or is_macos_11_native # Big Sur or newer
is_macos_11_compat = bool(_macos_ver and _macos_ver[0:2] == (10, 16)) # Big Sur or newer in compat mode
is_macos_11_native = bool(_macos_ver and _macos_ver[0:2] >= (11, 0)) # Big Sur or newer in native mode
is_macos_11 = bool(is_macos_11_compat or is_macos_11_native) # Big Sur or newer

# On different platforms is different file for dynamic python library.
# TODO: When removing support for is_py37, the "m" variants can be
Expand Down Expand Up @@ -140,7 +158,7 @@
# The following code creates compat.is_venv and is.virtualenv that are True when running a virtual environment, and also
# compat.base_prefix with the path to the base Python installation.

base_prefix = os.path.abspath(getattr(sys, 'real_prefix', getattr(sys, 'base_prefix', sys.prefix)))
base_prefix: str = os.path.abspath(getattr(sys, 'real_prefix', getattr(sys, 'base_prefix', sys.prefix)))
# Ensure `base_prefix` is not containing any relative parts.
is_venv = is_virtualenv = base_prefix != os.path.abspath(sys.prefix)

Expand Down Expand Up @@ -202,20 +220,20 @@
# macOS's platform.architecture() can be buggy, so we do this manually here. Based off the python documentation:
# https://docs.python.org/3/library/platform.html#platform.architecture
if is_darwin:
architecture = '64bit' if sys.maxsize > 2**32 else '32bit'
architecture: _Architecture = '64bit' if sys.maxsize > 2**32 else '32bit'
else:
architecture = platform.architecture()[0]
architecture: _Architecture = platform.architecture()[0]

# Cygwin needs special handling, because platform.system() contains identifiers such as MSYS_NT-10.0-19042 and
# CYGWIN_NT-10.0-19042 that do not fit PyInstaller's OS naming scheme. Explicitly set `system` to 'Cygwin'.
system = 'Cygwin' if is_cygwin else platform.system()
system: _System = 'Cygwin' if is_cygwin else platform.system()

# Machine suffix for bootloader.
machine = _pyi_machine(platform.machine(), platform.system())
machine: _Machine | None = _pyi_machine(platform.machine(), platform.system())


# Wine detection and support
def is_wine_dll(filename):
def is_wine_dll(filename: _OpenFile):
"""
Check if the given PE file is a Wine DLL (PE-converted built-in, or fake/placeholder one).
Expand Down Expand Up @@ -254,21 +272,21 @@ def is_wine_dll(filename):
# better to modify os.environ." (Same for unsetenv.)


def getenv(name, default=None) -> str:
def getenv(name: str, default: str | None = None):
"""
Returns unicode string containing value of environment variable 'name'.
"""
return os.environ.get(name, default)


def setenv(name, value):
def setenv(name: str, value: str):
"""
Accepts unicode string and set it as environment variable 'name' containing value 'value'.
"""
os.environ[name] = value


def unsetenv(name):
def unsetenv(name: str):
"""
Delete the environment variable 'name'.
"""
Expand All @@ -281,7 +299,12 @@ def unsetenv(name):
# Exec commands in subprocesses.


def exec_command(*cmdargs: str, encoding: str = None, raise_enoent: bool = None, **kwargs):
def exec_command(
*cmdargs: str,
encoding: str | None = None,
raise_enoent: bool | None = None,
**kwargs: int | bool | Iterable[int] | None
):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -360,7 +383,7 @@ def exec_command(*cmdargs: str, encoding: str = None, raise_enoent: bool = None,
return out


def exec_command_rc(*cmdargs: str, **kwargs) -> int:
def exec_command_rc(*cmdargs: str, **kwargs: float | bool | Iterable[int] | None):
"""
Return the exit code of the command specified by the passed positional arguments, optionally configured by the
passed keyword arguments.
Expand Down Expand Up @@ -388,7 +411,9 @@ def exec_command_rc(*cmdargs: str, **kwargs) -> int:
return subprocess.call(cmdargs, **kwargs)


def exec_command_stdout(*command_args: str, encoding: str = None, **kwargs) -> str:
def exec_command_stdout(
*command_args: str, encoding: str | None = None, **kwargs: float | str | bytes | bool | Iterable[int] | None
):
"""
Capture and return the standard output of the command specified by the passed positional arguments, optionally
configured by the passed keyword arguments.
Expand All @@ -404,7 +429,7 @@ def exec_command_stdout(*command_args: str, encoding: str = None, **kwargs) -> s
Parameters
----------
command_args : list[str]
command_args : List[str]
Variadic list whose:
1. Mandatory first element is the absolute path, relative path, or basename in the current `${PATH}` of the
command to run.
Expand Down Expand Up @@ -434,7 +459,9 @@ def exec_command_stdout(*command_args: str, encoding: str = None, **kwargs) -> s
return stdout if encoding is None else stdout.decode(encoding)


def exec_command_all(*cmdargs: str, encoding: str = None, **kwargs):
def exec_command_all(*cmdargs: str,
encoding: str | None = None,
**kwargs: int | bool | Iterable[int] | None) -> Tuple[int, str, str]:
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -533,7 +560,7 @@ def __wrap_python(args, kwargs):
return cmdargs, kwargs


def exec_python(*args, **kwargs):
def exec_python(*args: str, **kwargs: str | None):
"""
Wrap running python script in a subprocess.
Expand All @@ -543,7 +570,7 @@ def exec_python(*args, **kwargs):
return exec_command(*cmdargs, **kwargs)


def exec_python_rc(*args, **kwargs):
def exec_python_rc(*args: str, **kwargs: str | None):
"""
Wrap running python script in a subprocess.
Expand All @@ -556,7 +583,7 @@ def exec_python_rc(*args, **kwargs):
# Path handling.


def expand_path(path):
def expand_path(path: _StrOrBytesPath):
"""
Replace initial tilde '~' in path with user's home directory, and also expand environment variables
(i.e., ${VARNAME} on Unix, %VARNAME% on Windows).
Expand All @@ -565,15 +592,15 @@ def expand_path(path):


# Site-packages functions - use native function if available.
def getsitepackages(prefixes=None):
def getsitepackages(prefixes: Iterable[str] | None = None):
"""
Returns a list containing all global site-packages directories.
For each directory present in ``prefixes`` (or the global ``PREFIXES``), this function finds its `site-packages`
subdirectory depending on the system environment, and returns a list of full paths.
"""
# This implementation was copied from the ``site`` module, python 3.7.3.
sitepackages = []
sitepackages: List[str] = []
seen = set()

if prefixes is None:
Expand All @@ -597,7 +624,7 @@ def getsitepackages(prefixes=None):


# Wrapper to load a module from a Python source file. This function loads import hooks when processing them.
def importlib_load_source(name, pathname):
def importlib_load_source(name: str, pathname: bytes | str):
# Import module from a file.
mod_loader = importlib.machinery.SourceFileLoader(name, pathname)
return mod_loader.load_module()
Expand Down
2 changes: 1 addition & 1 deletion PyInstaller/fake-modules/pyi_splash.py
Expand Up @@ -172,7 +172,7 @@ def is_alive():


@_check_connection
def update_text(msg):
def update_text(msg: str):
"""
Updates the text on the splash screen window.
Expand Down

0 comments on commit cf27ffe

Please sign in to comment.