Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing inline types for public api #7068

Merged
merged 4 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 | list | None = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyi_config isn't allowed to be a list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it can be an Iterable[tuple[str, object]] (as it's used to update a dict). But as you mentioned in another comment, doesn't mean that's what it's intended for. So I've updated it and other annotations meant to update CONF as well.

"""
pyi_args allows running PyInstaller programmatically without a subprocess
pyi_config allows checking configuration once when running multiple tests
Expand Down
46 changes: 27 additions & 19 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,9 +73,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
rokm marked this conversation as resolved.
Show resolved Hide resolved
is_macos_11 = bool(is_macos_11_compat or is_macos_11_native) # Big Sur or newer
rokm marked this conversation as resolved.
Show resolved Hide resolved

# 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 +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 | bytes | os.PathLike | int):
rokm marked this conversation as resolved.
Show resolved Hide resolved
"""
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,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 | None
rokm marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.

Expand Down Expand Up @@ -360,7 +366,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 +394,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 +412,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 +442,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 +541,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 +551,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 +564,7 @@ def exec_python_rc(*args, **kwargs):
# Path handling.


def expand_path(path):
def expand_path(path: str | bytes | 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 +573,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 +605,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