-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/watchpoints #13248
base: main
Are you sure you want to change the base?
Feat/watchpoints #13248
Conversation
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
_Identifier: TypeAlias = str | ||
|
||
class Watch: | ||
custom_printer: Callable[[Any], None] | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several callback types this package have Any
as argument type(s), which are assigned with user-defined callbacks. I avoided using object
here to be compatible with user-defined callbacks, which may annotate the parameters with more precise types.
- Printer callbacks: accepts any object and writes to a stream
- Compare (
cmp
) callbacks: Accepts any 2 objects and compare their equality copy
: Makes a copy of any objectwhen
callbacks: Inspect/trace/watch an objectwhen
a condition is fulfilled; takes any object and outputs abool
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, but please add a comment to the code explaining this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in fcb9691
|
||
__version__: Final[LiteralString] | ||
|
||
all: Final[list[Literal["watch", "unwatch"]]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is a typo for __all__
, worth reporting as a bug to the runtime package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 7ae5b5c - PR is also merged in the runtime package to change it to __all__
, but I think that needs a new release before it can be added to typeshed.
from types import FrameType | ||
|
||
def getline(frame: FrameType) -> str: ... | ||
def getargnodes(frame: FrameType) -> zip[tuple[ast.expr, str]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def getargnodes(frame: FrameType) -> zip[tuple[ast.expr, str]]: ... | |
def getargnodes(frame: FrameType) -> Iterable[tuple[ast.expr, str]]: ... |
It does return a zip object at runtime, but that's a bit too much of an implementation detail for my taste.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in ec50c22
def __init__(self) -> None: ... | ||
def __call__( | ||
self, | ||
*args: object, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like these are unused, consider using _typeshed.Unused
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding of the library is that it inspects the expressions of positional-only arguments passed to __call__
(using frames and AST), so while the runtime values don't seem to be used, the expressions passed as *args
to __call__
actually do matter.
Passing nothing will cause nothing to be watched, so I'm also tempted to enforce at least 1 argument to be passed here, but let me know if it's still desirable to use _typeshed.Unused
.
_Identifier: TypeAlias = str | ||
|
||
class Watch: | ||
custom_printer: Callable[[Any], None] | None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, but please add a comment to the code explaining this.
def __call__( | ||
self, | ||
*args: object, | ||
alias: str = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one and most of the others support None, since the runtime does kwargs.get("alias", None)
. Add None
to the allowed type of these parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike in watch_element.WatchElement.__init__
and watch_print.WatchPrint.__init__
, which receives arguments from watch.Watch
which must handle None
, I avoided doing this in watch.Watch.__call__
because
watch.Watch
is the only public-facing object/method given in the documentation;- The README documentation doesn't describe
None
as valid arguments to most ofWatch.__call__
- the implementation uses it as a sentinel for unpassed arguments; - As per the README,
Watch.__call__(stack_limit=None)
andWatch.config(stack_limit=None)
is where aNone
actually means something if it is passed.
Let me know if we'd still prefer None
to be valid in watch.Watch.__call__
as I understand that it does match the runtime better.
deepcopy: bool = False, | ||
file: str | SupportsWrite[str] = ..., | ||
stack_limit: int | None = 5, | ||
track: Literal["object", "variable"] = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
track: Literal["object", "variable"] = ..., | |
track: list[Literal["object", "variable"]] = ..., |
Or does it also support other kinds of Sequences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation describes track
to expect only "object"
or "variable"
, and to not pass anything to track
if the user wants both "object"
and "variable"
. It's handled internally as a list[Literal["object", "variable"]]
if the user doesn't pass anything (see watchpoints.watch_element.WatchElement.track
) but I interpret this as an implementation detail.
def unwatch(self, *args: object) -> None: ... | ||
|
||
class _TraceFunc(Protocol): | ||
def __call__(self, frame: FrameType, event: str, arg: object) -> _TraceFunc: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these parameters be positional-only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The watchpoints.watch.Watch.tracefunc
method returns itself, so I kept the callback's signature the same as the method.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This is a complete set of stubs for the watchpoints package (GitHub: https://github.com/gaogaotiantian/watchpoints).
Even though it seems like activity is low, it still gets ~800 downloads a week, and its lowish numbers are due to it being a debugging tool rather than a dependency library.
Its stated Python support ranges (3.6-3.10) are modest - I've run the tests locally on Python 3.13-dev and they pass without any modifications.
The intended API and types were inferred through the implementation and the README.