mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #1666 from pre-commit/mutable_mapping
Replace EnvironT with MutableMapping[str, str]
This commit is contained in:
commit
2779bde2a2
6 changed files with 17 additions and 19 deletions
|
|
@ -11,6 +11,7 @@ from typing import Any
|
||||||
from typing import Collection
|
from typing import Collection
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import MutableMapping
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
from typing import Set
|
from typing import Set
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
@ -28,7 +29,6 @@ from pre_commit.repository import install_hook_envs
|
||||||
from pre_commit.staged_files_only import staged_files_only
|
from pre_commit.staged_files_only import staged_files_only
|
||||||
from pre_commit.store import Store
|
from pre_commit.store import Store
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import EnvironT
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
@ -116,7 +116,7 @@ class Classifier:
|
||||||
return Classifier(filenames)
|
return Classifier(filenames)
|
||||||
|
|
||||||
|
|
||||||
def _get_skips(environ: EnvironT) -> Set[str]:
|
def _get_skips(environ: MutableMapping[str, str]) -> Set[str]:
|
||||||
skips = environ.get('SKIP', '')
|
skips = environ.get('SKIP', '')
|
||||||
return {skip.strip() for skip in skips.split(',') if skip.strip()}
|
return {skip.strip() for skip in skips.split(',') if skip.strip()}
|
||||||
|
|
||||||
|
|
@ -258,7 +258,7 @@ def _run_hooks(
|
||||||
config: Dict[str, Any],
|
config: Dict[str, Any],
|
||||||
hooks: Sequence[Hook],
|
hooks: Sequence[Hook],
|
||||||
args: argparse.Namespace,
|
args: argparse.Namespace,
|
||||||
environ: EnvironT,
|
environ: MutableMapping[str, str],
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Actually run the hooks."""
|
"""Actually run the hooks."""
|
||||||
skips = _get_skips(environ)
|
skips = _get_skips(environ)
|
||||||
|
|
@ -315,7 +315,7 @@ def run(
|
||||||
config_file: str,
|
config_file: str,
|
||||||
store: Store,
|
store: Store,
|
||||||
args: argparse.Namespace,
|
args: argparse.Namespace,
|
||||||
environ: EnvironT = os.environ,
|
environ: MutableMapping[str, str] = os.environ,
|
||||||
) -> int:
|
) -> int:
|
||||||
stash = not args.all_files and not args.files
|
stash = not args.all_files and not args.files
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,12 @@ import contextlib
|
||||||
import enum
|
import enum
|
||||||
import os
|
import os
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
|
from typing import MutableMapping
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from pre_commit.util import EnvironT
|
|
||||||
|
|
||||||
|
|
||||||
class _Unset(enum.Enum):
|
class _Unset(enum.Enum):
|
||||||
UNSET = 1
|
UNSET = 1
|
||||||
|
|
@ -27,7 +26,7 @@ ValueT = Union[str, _Unset, SubstitutionT]
|
||||||
PatchesT = Tuple[Tuple[str, ValueT], ...]
|
PatchesT = Tuple[Tuple[str, ValueT], ...]
|
||||||
|
|
||||||
|
|
||||||
def format_env(parts: SubstitutionT, env: EnvironT) -> str:
|
def format_env(parts: SubstitutionT, env: MutableMapping[str, str]) -> str:
|
||||||
return ''.join(
|
return ''.join(
|
||||||
env.get(part.name, part.default) if isinstance(part, Var) else part
|
env.get(part.name, part.default) if isinstance(part, Var) else part
|
||||||
for part in parts
|
for part in parts
|
||||||
|
|
@ -37,7 +36,7 @@ def format_env(parts: SubstitutionT, env: EnvironT) -> str:
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def envcontext(
|
def envcontext(
|
||||||
patch: PatchesT,
|
patch: PatchesT,
|
||||||
_env: Optional[EnvironT] = None,
|
_env: Optional[MutableMapping[str, str]] = None,
|
||||||
) -> Generator[None, None, None]:
|
) -> Generator[None, None, None]:
|
||||||
"""In this context, `os.environ` is modified according to `patch`.
|
"""In this context, `os.environ` is modified according to `patch`.
|
||||||
|
|
||||||
|
|
@ -50,7 +49,7 @@ def envcontext(
|
||||||
replaced with the previous environment
|
replaced with the previous environment
|
||||||
"""
|
"""
|
||||||
env = os.environ if _env is None else _env
|
env = os.environ if _env is None else _env
|
||||||
before = env.copy()
|
before = dict(env)
|
||||||
|
|
||||||
for k, v in patch:
|
for k, v in patch:
|
||||||
if v is UNSET:
|
if v is UNSET:
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import os.path
|
||||||
import sys
|
import sys
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import MutableMapping
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Set
|
from typing import Set
|
||||||
|
|
||||||
|
|
@ -10,7 +11,6 @@ from pre_commit.errors import FatalError
|
||||||
from pre_commit.util import CalledProcessError
|
from pre_commit.util import CalledProcessError
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import EnvironT
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
@ -24,7 +24,9 @@ def zsplit(s: str) -> List[str]:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def no_git_env(_env: Optional[EnvironT] = None) -> Dict[str, str]:
|
def no_git_env(
|
||||||
|
_env: Optional[MutableMapping[str, str]] = None,
|
||||||
|
) -> Dict[str, str]:
|
||||||
# Too many bugs dealing with environment variables and GIT:
|
# Too many bugs dealing with environment variables and GIT:
|
||||||
# https://github.com/pre-commit/pre-commit/issues/300
|
# https://github.com/pre-commit/pre-commit/issues/300
|
||||||
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
|
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ from typing import IO
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
from typing import Type
|
from typing import Type
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
|
@ -29,8 +28,6 @@ else: # pragma: no cover (<PY37)
|
||||||
from importlib_resources import open_binary
|
from importlib_resources import open_binary
|
||||||
from importlib_resources import read_text
|
from importlib_resources import read_text
|
||||||
|
|
||||||
EnvironT = Union[Dict[str, str], 'os._Environ']
|
|
||||||
|
|
||||||
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
|
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
|
||||||
yaml_load = functools.partial(yaml.load, Loader=Loader)
|
yaml_load = functools.partial(yaml.load, Loader=Loader)
|
||||||
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
|
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from typing import Callable
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import MutableMapping
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
@ -17,13 +18,12 @@ from typing import TypeVar
|
||||||
from pre_commit import parse_shebang
|
from pre_commit import parse_shebang
|
||||||
from pre_commit.util import cmd_output_b
|
from pre_commit.util import cmd_output_b
|
||||||
from pre_commit.util import cmd_output_p
|
from pre_commit.util import cmd_output_p
|
||||||
from pre_commit.util import EnvironT
|
|
||||||
|
|
||||||
TArg = TypeVar('TArg')
|
TArg = TypeVar('TArg')
|
||||||
TRet = TypeVar('TRet')
|
TRet = TypeVar('TRet')
|
||||||
|
|
||||||
|
|
||||||
def _environ_size(_env: Optional[EnvironT] = None) -> int:
|
def _environ_size(_env: Optional[MutableMapping[str, str]] = None) -> int:
|
||||||
environ = _env if _env is not None else getattr(os, 'environb', os.environ)
|
environ = _env if _env is not None else getattr(os, 'environb', os.environ)
|
||||||
size = 8 * len(environ) # number of pointers in `envp`
|
size = 8 * len(environ) # number of pointers in `envp`
|
||||||
for k, v in environ.items():
|
for k, v in environ.items():
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import os.path
|
||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
from typing import MutableMapping
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -18,7 +19,6 @@ from pre_commit.commands.run import Classifier
|
||||||
from pre_commit.commands.run import filter_by_include_exclude
|
from pre_commit.commands.run import filter_by_include_exclude
|
||||||
from pre_commit.commands.run import run
|
from pre_commit.commands.run import run
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import EnvironT
|
|
||||||
from pre_commit.util import make_executable
|
from pre_commit.util import make_executable
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
from testing.fixtures import add_config_to_repo
|
from testing.fixtures import add_config_to_repo
|
||||||
|
|
@ -482,7 +482,7 @@ def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
|
||||||
|
|
||||||
def test_checkout_type(cap_out, store, repo_with_passing_hook):
|
def test_checkout_type(cap_out, store, repo_with_passing_hook):
|
||||||
args = run_opts(from_ref='', to_ref='', checkout_type='1')
|
args = run_opts(from_ref='', to_ref='', checkout_type='1')
|
||||||
environ: EnvironT = {}
|
environ: MutableMapping[str, str] = {}
|
||||||
ret, printed = _do_run(
|
ret, printed = _do_run(
|
||||||
cap_out, store, repo_with_passing_hook, args, environ,
|
cap_out, store, repo_with_passing_hook, args, environ,
|
||||||
)
|
)
|
||||||
|
|
@ -1032,7 +1032,7 @@ def test_skipped_without_any_setup_for_post_checkout(in_git_dir, store):
|
||||||
|
|
||||||
def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
|
def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
|
||||||
args = run_opts()
|
args = run_opts()
|
||||||
environ: EnvironT = {}
|
environ: MutableMapping[str, str] = {}
|
||||||
ret, printed = _do_run(
|
ret, printed = _do_run(
|
||||||
cap_out, store, repo_with_passing_hook, args, environ,
|
cap_out, store, repo_with_passing_hook, args, environ,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue