Skip to content

Commit 046899e

Browse files
committed
change config back to file, point imports to git.types
1 parent 1f90e3b commit 046899e

File tree

6 files changed

+122
-19
lines changed

6 files changed

+122
-19
lines changed

git/compat.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -*- coding: utf-8 -*-
2+
# config.py
3+
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
4+
#
5+
# This module is part of GitPython and is released under
6+
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7+
"""utilities to help provide compatibility with python 3"""
8+
# flake8: noqa
9+
10+
import locale
11+
import os
12+
import sys
13+
14+
from gitdb.utils.encoding import (
15+
force_bytes, # @UnusedImport
16+
force_text # @UnusedImport
17+
)
18+
19+
# typing --------------------------------------------------------------------
20+
21+
from typing import (
22+
Any,
23+
AnyStr,
24+
Dict,
25+
IO,
26+
Optional,
27+
Tuple,
28+
Type,
29+
Union,
30+
overload,
31+
)
32+
from git.types import TBD
33+
34+
# ---------------------------------------------------------------------------
35+
36+
37+
is_win = (os.name == 'nt') # type: bool
38+
is_posix = (os.name == 'posix')
39+
is_darwin = (os.name == 'darwin')
40+
defenc = sys.getfilesystemencoding()
41+
42+
43+
@overload
44+
def safe_decode(s: None) -> None: ...
45+
46+
@overload
47+
def safe_decode(s: Union[IO[str], AnyStr]) -> str: ...
48+
49+
def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]:
50+
"""Safely decodes a binary string to unicode"""
51+
if isinstance(s, str):
52+
return s
53+
elif isinstance(s, bytes):
54+
return s.decode(defenc, 'surrogateescape')
55+
elif s is None:
56+
return None
57+
else:
58+
raise TypeError('Expected bytes or text, but got %r' % (s,))
59+
60+
61+
@overload
62+
def safe_encode(s: None) -> None: ...
63+
64+
@overload
65+
def safe_encode(s: AnyStr) -> bytes: ...
66+
67+
def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
68+
"""Safely encodes a binary string to unicode"""
69+
if isinstance(s, str):
70+
return s.encode(defenc)
71+
elif isinstance(s, bytes):
72+
return s
73+
elif s is None:
74+
return None
75+
else:
76+
raise TypeError('Expected bytes or text, but got %r' % (s,))
77+
78+
79+
@overload
80+
def win_encode(s: None) -> None: ...
81+
82+
@overload
83+
def win_encode(s: AnyStr) -> bytes: ...
84+
85+
def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
86+
"""Encode unicodes for process arguments on Windows."""
87+
if isinstance(s, str):
88+
return s.encode(locale.getpreferredencoding(False))
89+
elif isinstance(s, bytes):
90+
return s
91+
elif s is not None:
92+
raise TypeError('Expected bytes or text, but got %r' % (s,))
93+
return None
94+
95+
96+
def with_metaclass(meta: Type[Any], *bases: Any) -> TBD: # type: ignore ## mypy cannot understand dynamic class creation
97+
"""copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15"""
98+
99+
class metaclass(meta): # type: ignore
100+
__call__ = type.__call__
101+
__init__ = type.__init__ # type: ignore
102+
103+
def __new__(cls, name: str, nbases: Optional[Tuple[int, ...]], d: Dict[str, Any]) -> TBD:
104+
if nbases is None:
105+
return type.__new__(cls, name, (), d)
106+
return meta(name, bases, d)
107+
108+
return metaclass(meta.__name__ + 'Helper', None, {}) # type: ignore
File renamed without changes.

git/compat/typing.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

git/config.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@
2222
with_metaclass,
2323
is_win,
2424
)
25-
from git.compat.typing import Literal
25+
2626
from git.util import LockFile
2727

2828
import os.path as osp
2929

3030
import configparser as cp
3131

32+
# typing-------------------------------------------------------
33+
34+
from typing import TYPE_CHECKING, Tuple
35+
36+
from git.types import Literal
37+
38+
if TYPE_CHECKING:
39+
pass
40+
41+
# -------------------------------------------------------------
3242

3343
__all__ = ('GitConfigParser', 'SectionConstraint')
3444

@@ -38,7 +48,7 @@
3848

3949
# invariants
4050
# represents the configuration level of a configuration file
41-
CONFIG_LEVELS = ("system", "user", "global", "repository")
51+
CONFIG_LEVELS = ("system", "user", "global", "repository") # type: Tuple[Literal['system'], Literal['user'], Literal['global'], Literal['repository']]
4252

4353
# Section pattern to detect conditional includes.
4454
# https://git-scm.com/docs/git-config#_conditional_includes

git/diff.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# typing ------------------------------------------------------------------
1717

1818
from typing import Any, Iterator, List, Match, Optional, Tuple, Type, Union, TYPE_CHECKING
19-
from git.compat.typing import Final, Literal
20-
from git.types import TBD
19+
from git.types import TBD, Final, Literal
2120

2221
if TYPE_CHECKING:
2322
from .objects.tree import Tree

git/repo/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535

3636
# typing ------------------------------------------------------
3737

38-
from git.compat.typing import Literal
39-
from git.types import TBD, PathLike
38+
from git.types import TBD, PathLike, Literal
4039
from typing import (Any, BinaryIO, Callable, Dict,
4140
Iterator, List, Mapping, Optional,
4241
TextIO, Tuple, Type, Union,

0 commit comments

Comments
 (0)