Skip to content

Commit

Permalink
Merge pull request #1666 from pre-commit/mutable_mapping
Browse files Browse the repository at this point in the history
Replace EnvironT with MutableMapping[str, str]
  • Loading branch information
asottile committed Oct 28, 2020
2 parents a0c0870 + 24dfeed commit 2779bde
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions pre_commit/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand All @@ -28,7 +29,6 @@
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')
Expand Down Expand Up @@ -116,7 +116,7 @@ def from_config(
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()}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions pre_commit/envcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
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
Expand All @@ -27,7 +26,7 @@ class Var(NamedTuple):
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
Expand All @@ -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`.
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions pre_commit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import sys
from typing import Dict
from typing import List
from typing import MutableMapping
from typing import Optional
from typing import Set

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__)
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions pre_commit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Union

import yaml

Expand All @@ -29,8 +28,6 @@
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)
Expand Down
4 changes: 2 additions & 2 deletions pre_commit/xargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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
Expand All @@ -17,13 +18,12 @@
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():
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shlex
import sys
import time
from typing import MutableMapping
from unittest import mock

import pytest
Expand All @@ -18,7 +19,6 @@
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
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 2779bde

Please sign in to comment.