Skip to content

Commit

Permalink
Fix display of inherited docstring in VS Code tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Aug 19, 2022
1 parent 974f60e commit c75a080
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
56 changes: 33 additions & 23 deletions galois/_fields/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
__all__ = ["FieldArray"]


DOCSTRING_MAP = {
"Array": "FieldArray"
}


@set_module("galois")
class FieldArray(Array, metaclass=FieldArrayMeta):
r"""
Expand Down Expand Up @@ -183,8 +188,7 @@ def _convert_iterable_to_elements(cls, iterable: IterableLike) -> np.ndarray:
###############################################################################

@classmethod
@extend_docstring(Array.Zeros, {"Array": "FieldArray"})
def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
@extend_docstring(Array.Zeros, DOCSTRING_MAP,
"""
Examples
--------
Expand All @@ -193,11 +197,12 @@ def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArra
GF = galois.GF(31)
GF.Zeros((2, 5))
"""
)
def Zeros(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
return super().Zeros(shape, dtype=dtype)

@classmethod
@extend_docstring(Array.Ones, {"Array": "FieldArray"})
def Ones(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
@extend_docstring(Array.Ones, DOCSTRING_MAP,
"""
Examples
--------
Expand All @@ -206,17 +211,12 @@ def Ones(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray
GF = galois.GF(31)
GF.Ones((2, 5))
"""
)
def Ones(cls, shape: ShapeLike, dtype: Optional[DTypeLike] = None) -> FieldArray:
return super().Ones(shape, dtype=dtype)

@classmethod
@extend_docstring(Array.Range, {"Array": "FieldArray"})
def Range(
cls,
start: ElementLike,
stop: ElementLike,
step: int = 1,
dtype: Optional[DTypeLike] = None
) -> FieldArray:
@extend_docstring(Array.Range, DOCSTRING_MAP,
"""
Examples
--------
Expand All @@ -238,18 +238,18 @@ def Range(
@suppress
GF.display()
"""
return super().Range(start, stop, step=step, dtype=dtype)

@classmethod
@extend_docstring(Array.Random, {"Array": "FieldArray"})
def Random(
)
def Range(
cls,
shape: ShapeLike = (),
low: ElementLike = 0,
high: Optional[ElementLike] = None,
seed: Optional[Union[int, np.random.Generator]] = None,
start: ElementLike,
stop: ElementLike,
step: int = 1,
dtype: Optional[DTypeLike] = None
) -> FieldArray:
return super().Range(start, stop, step=step, dtype=dtype)

@classmethod
@extend_docstring(Array.Random, DOCSTRING_MAP,
"""
Examples
--------
Expand All @@ -275,11 +275,19 @@ def Random(
GF.Random(10, seed=rng)
GF.Random(10, seed=rng)
"""
)
def Random(
cls,
shape: ShapeLike = (),
low: ElementLike = 0,
high: Optional[ElementLike] = None,
seed: Optional[Union[int, np.random.Generator]] = None,
dtype: Optional[DTypeLike] = None
) -> FieldArray:
return super().Random(shape=shape, low=low, high=high, seed=seed, dtype=dtype)

@classmethod
@extend_docstring(Array.Identity, {"Array": "FieldArray"})
def Identity(cls, size: int, dtype: Optional[DTypeLike] = None) -> FieldArray:
@extend_docstring(Array.Identity, DOCSTRING_MAP,
"""
Examples
--------
Expand All @@ -288,6 +296,8 @@ def Identity(cls, size: int, dtype: Optional[DTypeLike] = None) -> FieldArray:
GF = galois.GF(31)
GF.Identity(4)
"""
)
def Identity(cls, size: int, dtype: Optional[DTypeLike] = None) -> FieldArray:
return super().Identity(size, dtype=dtype)

@classmethod
Expand Down
9 changes: 4 additions & 5 deletions galois/_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ def decorator(obj):
return decorator


def extend_docstring(method, replace={}): # pylint: disable=dangerous-default-value
def extend_docstring(method, replace={}, docstring=""): # pylint: disable=dangerous-default-value
"""
A decorator to append the docstring of a `method` with the docstring the the decorated method.
"""
def decorator(obj):
doc_1 = getattr(method, "__doc__", "")
doc_2 = getattr(obj, "__doc__", "")
parent_docstring = getattr(method, "__doc__", "")
for from_str, to_str in replace.items():
doc_1 = doc_1.replace(from_str, to_str)
obj.__doc__ = doc_1 + "\n" + doc_2
parent_docstring = parent_docstring.replace(from_str, to_str)
obj.__doc__ = parent_docstring + "\n" + docstring

return obj

Expand Down

0 comments on commit c75a080

Please sign in to comment.