Skip to content

Commit f7aa9b3

Browse files
authored
Merge pull request #280 from eric-wieser/move-kwarg-parser
Deprecate galgebra.utils
2 parents d9b9f14 + bf59dda commit f7aa9b3

File tree

6 files changed

+58
-43
lines changed

6 files changed

+58
-43
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Changelog
33
=========
44

5+
- :support:`280` The ``galgebra.utils`` module, which provided Python 2 compatibility helpers, has been deprecated.
6+
57
- :support:`245` (also :issue:`248`) The following attributes have been deprecated to reduce the number of similar members in :class:`~galgebra.ga.Ga`.
68

79
* Unified into :attr:`galgebra.ga.Ga.blade_expansion_dict`:

galgebra/_utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
""" Internal helpers for use in galgebra """
22

33
from .cached_property import cached_property
4+
from .kwarg_parser import KwargParser
45

56
__all__ = [
67
'cached_property',
8+
'KwargParser',
79
]

galgebra/_utils/kwarg_parser.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class KwargParser:
2+
"""
3+
Helper function to emulate Python 3 keyword-only arguments.
4+
5+
Use as::
6+
7+
def func(x1, **kwargs):
8+
kw = KwargParser('func', kwargs)
9+
a = kw.pop('a')
10+
b = kw.pop('b', 2)
11+
kw.reject_remaining()
12+
...
13+
14+
To emulate the Python 3 syntax::
15+
16+
def func(x1, *, a, b=2):
17+
...
18+
19+
This is also useful for functions with overloaded signatures, for which
20+
a single Python 3 signature cannot be written.
21+
"""
22+
def __init__(self, func_name, kwargs):
23+
self._func_name = func_name
24+
self._kwargs = kwargs
25+
26+
def pop(self, arg_name, *default):
27+
try:
28+
return self._kwargs.pop(arg_name, *default)
29+
except KeyError:
30+
pass
31+
raise TypeError(
32+
'{}() missing required keyword-only argument {!r}'
33+
.format(self._func_name, arg_name)
34+
)
35+
36+
def reject_remaining(self):
37+
if self._kwargs:
38+
# match the error message to what Python 3 produces
39+
bad_arg = next(iter(self._kwargs))
40+
raise TypeError(
41+
'{}() got an unexpected keyword argument {!r}'
42+
.format(self._func_name, bad_arg)
43+
)

galgebra/mv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import printer
2020
from . import metric
2121
from .printer import ZERO_STR
22-
from .utils import _KwargParser
22+
from ._utils import KwargParser as _KwargParser
2323
from . import dop
2424

2525
ONE = S(1)

galgebra/utils.py

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44

55
import sys
66
import collections
7+
import warnings
78

89
from io import StringIO # noqa: F401
910

11+
# galgebra 0.5.0
12+
warnings.warn(
13+
"galgebra.utils is deprecated and will be removed. "
14+
"If you need python 2 compatibility helpers, use a decdicated module like "
15+
"`six`.", DeprecationWarning, stacklevel=2)
16+
1017
# From https://github.com/benjaminp/six/blob/master/six.py
1118
PY2 = sys.version_info[0] == 2
1219
PY3 = sys.version_info[0] == 3
@@ -29,45 +36,3 @@ def flatten(x):
2936

3037
def isstr(s):
3138
return isinstance(s, str)
32-
33-
34-
class _KwargParser:
35-
"""
36-
Helper function to emulate Python 3 keyword-only arguments.
37-
38-
Use as::
39-
40-
def func(x1, **kwargs):
41-
kw = KwargParser('func', kwargs)
42-
a = kw.pop('a')
43-
b = kw.pop('b', 2)
44-
kw.reject_remaining()
45-
...
46-
47-
To emulate the Python 3 syntax::
48-
49-
def func(x1, *, a, b=2):
50-
...
51-
"""
52-
def __init__(self, func_name, kwargs):
53-
self._func_name = func_name
54-
self._kwargs = kwargs
55-
56-
def pop(self, arg_name, *default):
57-
try:
58-
return self._kwargs.pop(arg_name, *default)
59-
except KeyError:
60-
pass
61-
raise TypeError(
62-
'{}() missing required keyword-only argument {!r}'
63-
.format(self._func_name, arg_name)
64-
)
65-
66-
def reject_remaining(self):
67-
if self._kwargs:
68-
# match the error message to what Python 3 produces
69-
bad_arg = next(iter(self._kwargs))
70-
raise TypeError(
71-
'{}() got an unexpected keyword argument {!r}'
72-
.format(self._func_name, bad_arg)
73-
)

test/test_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,6 @@ def test_deprecations(self):
475475
# all derived from
476476
ga.blade_expansion_dict
477477
ga.base_expansion_dict
478+
479+
with pytest.warns(DeprecationWarning):
480+
import galgebra.utils

0 commit comments

Comments
 (0)