Skip to content

Commit

Permalink
fix @does_not_alter_signature not working with methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Aran-Fey committed Dec 17, 2024
1 parent e6665db commit 3cb0144
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 75 deletions.
2 changes: 1 addition & 1 deletion introspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
New and improved introspection functions
"""

__version__ = "1.9.2"
__version__ = "1.9.3"

from .parameter import *
from .signature_ import *
Expand Down
15 changes: 8 additions & 7 deletions introspection/call_frame.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import types
import typing as t
from typing_extensions import Self
import typing_extensions as te

from . import errors

Expand Down Expand Up @@ -62,23 +62,24 @@ def __init__(self, frame: t.Union[types.FrameType, inspect.FrameInfo]):
self._frame = frame

@classmethod
def current(cls) -> Self:
def current(cls) -> te.Self:
"""
Retrieves the current call frame.
"""
return cls.up(1) # up(1) because we need to skip the implementation of `current`

@classmethod
def up(cls, n: int, /) -> Self:
def up(cls, n: int, /) -> te.Self:
"""
Retrieves the `n`th frame (from the bottom) from the call stack.
Retrieves the `n`th frame (from the bottom) from the call stack. In
other words, `up(0)` is equivalent to `CallFrame.current()`.
:raises IndexError: If `n` is larger than the call stack
"""
return cls(get_frame(n + 2))

@classmethod
def iter(cls) -> t.Iterator[Self]:
def iter(cls) -> t.Iterator[te.Self]:
call_frame = cls.up(1)

while call_frame is not None:
Expand All @@ -95,14 +96,14 @@ def __eq__(self, other: object) -> bool:
else:
return NotImplemented

def __enter__(self) -> Self:
def __enter__(self) -> te.Self:
return self

def __exit__(self, exc_type: object, exc_val: object, exc_tb: object) -> None:
del self._frame

@property
def parent(self) -> t.Optional[Self]:
def parent(self) -> t.Optional[te.Self]:
"""
Returns the next frame one level higher on the call stack.
"""
Expand Down
20 changes: 10 additions & 10 deletions introspection/call_stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import types
from typing import *
from typing_extensions import Self
import typing as t
import typing_extensions as te

from .call_frame import CallFrame

Expand All @@ -25,7 +25,7 @@ class CallStack:

__slots__ = ("_frames",)

def __init__(self, frames: Iterable[types.FrameType]):
def __init__(self, frames: t.Iterable[types.FrameType]):
"""
Creates a new ``CallStack`` from the given frame objects.
Expand All @@ -34,22 +34,22 @@ def __init__(self, frames: Iterable[types.FrameType]):
self._frames = [CallFrame(frame) for frame in frames]

@classmethod
def current(cls) -> Self:
def current(cls) -> te.Self:
"""
Get the current call stack.
"""
with CallFrame.current() as frame:
return cls.from_frame(cast(CallFrame, frame.parent))
return cls.from_frame(t.cast(CallFrame, frame.parent))

@classmethod
def from_frame(cls, frame: Union[types.FrameType, CallFrame]) -> Self:
def from_frame(cls, frame: t.Union[types.FrameType, CallFrame]) -> te.Self:
"""
Creates a ``CallStack`` containing ``frame`` and all its parents.
:param frame: The last frame in the call stack
:return: A new ``CallStack`` instance
"""
frame_: Optional[types.FrameType]
frame_: t.Optional[types.FrameType]
if isinstance(frame, CallFrame):
frame_ = frame._frame
else:
Expand All @@ -68,16 +68,16 @@ def from_frame(cls, frame: Union[types.FrameType, CallFrame]) -> Self:

return cls(frames)

def __enter__(self) -> Self:
def __enter__(self) -> te.Self:
return self

def __exit__(self, exc_type, exc_val, exc_tb):
del self._frames

def __iter__(self) -> Iterator[CallFrame]:
def __iter__(self) -> t.Iterator[CallFrame]:
return iter(self._frames)

def __reversed__(self) -> Iterator[CallFrame]:
def __reversed__(self) -> t.Iterator[CallFrame]:
return reversed(self._frames)

def __getitem__(self, index) -> CallFrame:
Expand Down
Loading

0 comments on commit 3cb0144

Please sign in to comment.