Skip to content

Commit

Permalink
Added missing inline types for public api (#7068)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 8, 2022
1 parent b80740e commit bf2a4ad
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 73 deletions.
3 changes: 2 additions & 1 deletion PyInstaller/__main__.py
Expand Up @@ -11,6 +11,7 @@
"""
Main command-line interface to PyInstaller.
"""
from __future__ import annotations

import argparse
import os
Expand Down Expand Up @@ -146,7 +147,7 @@ def generate_parser() -> _PyiArgumentParser:
return parser


def run(pyi_args=None, pyi_config=None):
def run(pyi_args: list | None = None, pyi_config: dict | None = None):
"""
pyi_args allows running PyInstaller programmatically without a subprocess
pyi_config allows checking configuration once when running multiple tests
Expand Down
41 changes: 23 additions & 18 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 @@ -27,7 +28,7 @@
from PyInstaller.exceptions import ExecCommandFailed

# 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,8 +73,8 @@
# 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_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 = is_macos_11_compat or is_macos_11_native # Big Sur or newer

# On different platforms is different file for dynamic python library.
Expand Down Expand Up @@ -140,7 +141,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 @@ -215,7 +216,7 @@


# Wine detection and support
def is_wine_dll(filename):
def is_wine_dll(filename: str | os.PathLike):
"""
Check if the given PE file is a Wine DLL (PE-converted built-in, or fake/placeholder one).
Expand Down Expand Up @@ -254,21 +255,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 +282,9 @@ 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 | list | None
):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -360,7 +363,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 | list | 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 +391,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 | list | 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 +409,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 +439,7 @@ 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 | list | None):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -533,7 +538,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 +548,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 +561,7 @@ def exec_python_rc(*args, **kwargs):
# Path handling.


def expand_path(path):
def expand_path(path: str | os.PathLike):
"""
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,7 +570,7 @@ def expand_path(path):


# Site-packages functions - use native function if available.
def getsitepackages(prefixes=None):
def getsitepackages(prefixes: list | None = None):
"""
Returns a list containing all global site-packages directories.
Expand Down Expand Up @@ -597,7 +602,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: 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 bf2a4ad

Please sign in to comment.