Skip to content

Commit 2779bde

Browse files
authored
Merge pull request pre-commit#1666 from pre-commit/mutable_mapping
Replace EnvironT with MutableMapping[str, str]
2 parents a0c0870 + 24dfeed commit 2779bde

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

pre_commit/commands/run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Collection
1212
from typing import Dict
1313
from typing import List
14+
from typing import MutableMapping
1415
from typing import Sequence
1516
from typing import Set
1617
from typing import Tuple
@@ -28,7 +29,6 @@
2829
from pre_commit.staged_files_only import staged_files_only
2930
from pre_commit.store import Store
3031
from pre_commit.util import cmd_output_b
31-
from pre_commit.util import EnvironT
3232

3333

3434
logger = logging.getLogger('pre_commit')
@@ -116,7 +116,7 @@ def from_config(
116116
return Classifier(filenames)
117117

118118

119-
def _get_skips(environ: EnvironT) -> Set[str]:
119+
def _get_skips(environ: MutableMapping[str, str]) -> Set[str]:
120120
skips = environ.get('SKIP', '')
121121
return {skip.strip() for skip in skips.split(',') if skip.strip()}
122122

@@ -258,7 +258,7 @@ def _run_hooks(
258258
config: Dict[str, Any],
259259
hooks: Sequence[Hook],
260260
args: argparse.Namespace,
261-
environ: EnvironT,
261+
environ: MutableMapping[str, str],
262262
) -> int:
263263
"""Actually run the hooks."""
264264
skips = _get_skips(environ)
@@ -315,7 +315,7 @@ def run(
315315
config_file: str,
316316
store: Store,
317317
args: argparse.Namespace,
318-
environ: EnvironT = os.environ,
318+
environ: MutableMapping[str, str] = os.environ,
319319
) -> int:
320320
stash = not args.all_files and not args.files
321321

pre_commit/envcontext.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import enum
33
import os
44
from typing import Generator
5+
from typing import MutableMapping
56
from typing import NamedTuple
67
from typing import Optional
78
from typing import Tuple
89
from typing import Union
910

10-
from pre_commit.util import EnvironT
11-
1211

1312
class _Unset(enum.Enum):
1413
UNSET = 1
@@ -27,7 +26,7 @@ class Var(NamedTuple):
2726
PatchesT = Tuple[Tuple[str, ValueT], ...]
2827

2928

30-
def format_env(parts: SubstitutionT, env: EnvironT) -> str:
29+
def format_env(parts: SubstitutionT, env: MutableMapping[str, str]) -> str:
3130
return ''.join(
3231
env.get(part.name, part.default) if isinstance(part, Var) else part
3332
for part in parts
@@ -37,7 +36,7 @@ def format_env(parts: SubstitutionT, env: EnvironT) -> str:
3736
@contextlib.contextmanager
3837
def envcontext(
3938
patch: PatchesT,
40-
_env: Optional[EnvironT] = None,
39+
_env: Optional[MutableMapping[str, str]] = None,
4140
) -> Generator[None, None, None]:
4241
"""In this context, `os.environ` is modified according to `patch`.
4342
@@ -50,7 +49,7 @@ def envcontext(
5049
replaced with the previous environment
5150
"""
5251
env = os.environ if _env is None else _env
53-
before = env.copy()
52+
before = dict(env)
5453

5554
for k, v in patch:
5655
if v is UNSET:

pre_commit/git.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import sys
44
from typing import Dict
55
from typing import List
6+
from typing import MutableMapping
67
from typing import Optional
78
from typing import Set
89

910
from pre_commit.errors import FatalError
1011
from pre_commit.util import CalledProcessError
1112
from pre_commit.util import cmd_output
1213
from pre_commit.util import cmd_output_b
13-
from pre_commit.util import EnvironT
1414

1515

1616
logger = logging.getLogger(__name__)
@@ -24,7 +24,9 @@ def zsplit(s: str) -> List[str]:
2424
return []
2525

2626

27-
def no_git_env(_env: Optional[EnvironT] = None) -> Dict[str, str]:
27+
def no_git_env(
28+
_env: Optional[MutableMapping[str, str]] = None,
29+
) -> Dict[str, str]:
2830
# Too many bugs dealing with environment variables and GIT:
2931
# https://github.com/pre-commit/pre-commit/issues/300
3032
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running

pre_commit/util.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from typing import Optional
1717
from typing import Tuple
1818
from typing import Type
19-
from typing import Union
2019

2120
import yaml
2221

@@ -29,8 +28,6 @@
2928
from importlib_resources import open_binary
3029
from importlib_resources import read_text
3130

32-
EnvironT = Union[Dict[str, str], 'os._Environ']
33-
3431
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
3532
yaml_load = functools.partial(yaml.load, Loader=Loader)
3633
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)

pre_commit/xargs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Generator
1010
from typing import Iterable
1111
from typing import List
12+
from typing import MutableMapping
1213
from typing import Optional
1314
from typing import Sequence
1415
from typing import Tuple
@@ -17,13 +18,12 @@
1718
from pre_commit import parse_shebang
1819
from pre_commit.util import cmd_output_b
1920
from pre_commit.util import cmd_output_p
20-
from pre_commit.util import EnvironT
2121

2222
TArg = TypeVar('TArg')
2323
TRet = TypeVar('TRet')
2424

2525

26-
def _environ_size(_env: Optional[EnvironT] = None) -> int:
26+
def _environ_size(_env: Optional[MutableMapping[str, str]] = None) -> int:
2727
environ = _env if _env is not None else getattr(os, 'environb', os.environ)
2828
size = 8 * len(environ) # number of pointers in `envp`
2929
for k, v in environ.items():

tests/commands/run_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shlex
33
import sys
44
import time
5+
from typing import MutableMapping
56
from unittest import mock
67

78
import pytest
@@ -18,7 +19,6 @@
1819
from pre_commit.commands.run import filter_by_include_exclude
1920
from pre_commit.commands.run import run
2021
from pre_commit.util import cmd_output
21-
from pre_commit.util import EnvironT
2222
from pre_commit.util import make_executable
2323
from testing.auto_namedtuple import auto_namedtuple
2424
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):
482482

483483
def test_checkout_type(cap_out, store, repo_with_passing_hook):
484484
args = run_opts(from_ref='', to_ref='', checkout_type='1')
485-
environ: EnvironT = {}
485+
environ: MutableMapping[str, str] = {}
486486
ret, printed = _do_run(
487487
cap_out, store, repo_with_passing_hook, args, environ,
488488
)
@@ -1032,7 +1032,7 @@ def test_skipped_without_any_setup_for_post_checkout(in_git_dir, store):
10321032

10331033
def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
10341034
args = run_opts()
1035-
environ: EnvironT = {}
1035+
environ: MutableMapping[str, str] = {}
10361036
ret, printed = _do_run(
10371037
cap_out, store, repo_with_passing_hook, args, environ,
10381038
)

0 commit comments

Comments
 (0)