mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 13:00:10 -06:00
Merge pull request #1666 from pre-commit/mutable_mapping
Replace EnvironT with MutableMapping[str, str]
This commit is contained in:
@@ -11,6 +11,7 @@ from typing import Any
|
||||
from typing import Collection
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import MutableMapping
|
||||
from typing import Sequence
|
||||
from typing import Set
|
||||
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.store import Store
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.util import EnvironT
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
@@ -116,7 +116,7 @@ class Classifier:
|
||||
return Classifier(filenames)
|
||||
|
||||
|
||||
def _get_skips(environ: EnvironT) -> Set[str]:
|
||||
def _get_skips(environ: MutableMapping[str, str]) -> Set[str]:
|
||||
skips = environ.get('SKIP', '')
|
||||
return {skip.strip() for skip in skips.split(',') if skip.strip()}
|
||||
|
||||
@@ -258,7 +258,7 @@ def _run_hooks(
|
||||
config: Dict[str, Any],
|
||||
hooks: Sequence[Hook],
|
||||
args: argparse.Namespace,
|
||||
environ: EnvironT,
|
||||
environ: MutableMapping[str, str],
|
||||
) -> int:
|
||||
"""Actually run the hooks."""
|
||||
skips = _get_skips(environ)
|
||||
@@ -315,7 +315,7 @@ def run(
|
||||
config_file: str,
|
||||
store: Store,
|
||||
args: argparse.Namespace,
|
||||
environ: EnvironT = os.environ,
|
||||
environ: MutableMapping[str, str] = os.environ,
|
||||
) -> int:
|
||||
stash = not args.all_files and not args.files
|
||||
|
||||
|
||||
@@ -2,13 +2,12 @@ import contextlib
|
||||
import enum
|
||||
import os
|
||||
from typing import Generator
|
||||
from typing import MutableMapping
|
||||
from typing import NamedTuple
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
from typing import Union
|
||||
|
||||
from pre_commit.util import EnvironT
|
||||
|
||||
|
||||
class _Unset(enum.Enum):
|
||||
UNSET = 1
|
||||
@@ -27,7 +26,7 @@ ValueT = Union[str, _Unset, SubstitutionT]
|
||||
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(
|
||||
env.get(part.name, part.default) if isinstance(part, Var) else part
|
||||
for part in parts
|
||||
@@ -37,7 +36,7 @@ def format_env(parts: SubstitutionT, env: EnvironT) -> str:
|
||||
@contextlib.contextmanager
|
||||
def envcontext(
|
||||
patch: PatchesT,
|
||||
_env: Optional[EnvironT] = None,
|
||||
_env: Optional[MutableMapping[str, str]] = None,
|
||||
) -> Generator[None, None, None]:
|
||||
"""In this context, `os.environ` is modified according to `patch`.
|
||||
|
||||
@@ -50,7 +49,7 @@ def envcontext(
|
||||
replaced with the previous environment
|
||||
"""
|
||||
env = os.environ if _env is None else _env
|
||||
before = env.copy()
|
||||
before = dict(env)
|
||||
|
||||
for k, v in patch:
|
||||
if v is UNSET:
|
||||
|
||||
@@ -3,6 +3,7 @@ import os.path
|
||||
import sys
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import MutableMapping
|
||||
from typing import Optional
|
||||
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 cmd_output
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.util import EnvironT
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -24,7 +24,9 @@ def zsplit(s: str) -> List[str]:
|
||||
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:
|
||||
# https://github.com/pre-commit/pre-commit/issues/300
|
||||
# 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 Tuple
|
||||
from typing import Type
|
||||
from typing import Union
|
||||
|
||||
import yaml
|
||||
|
||||
@@ -29,8 +28,6 @@ else: # pragma: no cover (<PY37)
|
||||
from importlib_resources import open_binary
|
||||
from importlib_resources import read_text
|
||||
|
||||
EnvironT = Union[Dict[str, str], 'os._Environ']
|
||||
|
||||
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
|
||||
yaml_load = functools.partial(yaml.load, Loader=Loader)
|
||||
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
|
||||
|
||||
@@ -9,6 +9,7 @@ from typing import Callable
|
||||
from typing import Generator
|
||||
from typing import Iterable
|
||||
from typing import List
|
||||
from typing import MutableMapping
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
from typing import Tuple
|
||||
@@ -17,13 +18,12 @@ from typing import TypeVar
|
||||
from pre_commit import parse_shebang
|
||||
from pre_commit.util import cmd_output_b
|
||||
from pre_commit.util import cmd_output_p
|
||||
from pre_commit.util import EnvironT
|
||||
|
||||
TArg = TypeVar('TArg')
|
||||
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)
|
||||
size = 8 * len(environ) # number of pointers in `envp`
|
||||
for k, v in environ.items():
|
||||
|
||||
@@ -2,6 +2,7 @@ import os.path
|
||||
import shlex
|
||||
import sys
|
||||
import time
|
||||
from typing import MutableMapping
|
||||
from unittest import mock
|
||||
|
||||
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 run
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import EnvironT
|
||||
from pre_commit.util import make_executable
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
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):
|
||||
args = run_opts(from_ref='', to_ref='', checkout_type='1')
|
||||
environ: EnvironT = {}
|
||||
environ: MutableMapping[str, str] = {}
|
||||
ret, printed = _do_run(
|
||||
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):
|
||||
args = run_opts()
|
||||
environ: EnvironT = {}
|
||||
environ: MutableMapping[str, str] = {}
|
||||
ret, printed = _do_run(
|
||||
cap_out, store, repo_with_passing_hook, args, environ,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user