Add type stubs for BenchmarkFixture#309
Conversation
|
I am a bit unsure about some of the stats objects. Are they cProfile stats or pytest-benchmark stats? I think both of them implement The cProfile stats do use a tuple key (typed internally as |
Also added newline at end of each file
Moved all type tests under a `TYPE_CHECKING` block since they should only be run by mypy Additionally used old style TypeVar/ParamSpec pattern to allow use in pre 3.12 Python versions
Since stats are exposed by a public interface in `BenchmarkFixture`, we should probably add stubs for them too
|
Sorry for all the additional commits, I'm getting used to your tox setup and trying to make sure everything's compliant and doesn't add any warnings/errors to your current CI |
- Remove overloads for `as_dict` in favor of a union of the flat/nested TypedDicts - User can narrow the type by checking for keys (e.g. 'min' in m.as_dict() -> _FlatMetadataDict)
|
These could also go in the typeshed if you don't want to maintain them here, but I saw you added I will also be happy to maintain these stubs in the future. I added them to my local environment and it cleared all the There are also some existing hints in the modules, I added those to the stubs, but the stubs could also be marked partial by adding Since this extension still supports 3.10, it's probably best to keep the typing separate from the codebase due to the changes that have happened between 3.10 and 3.14. The stubs will be totally ignored at runtime so we only really need to make sure that they work with current type checkers without having to deal with version specific imports. |
Added type stub for
BenchmarkFixtureclass. The typing is currently relegated to afixture.pyifile to prevent modification of the actual fixture code.There is some existing work on this in #290, but this should be hinting for most of the public API.
I have also included a new
test_stubs.pyfile that tests different argument options to test that return types are properly inferred.Methods:
__call__: Infers*argsand**kwargstypes from ParamSpec offunction_to_benchmarkpedantic: Shows Type Error whensetupis passed withargsand orkwargs, infers return from Param Specweave: Probably not neccessary since**kwargsare not for the targetAttributes
name:strfullname:strdisabled:boolparam:str | Noneparams:tuple[str, ...] | Nonegroup:str | Nonehas_error:boolextra_info:dict[str, Any]skipped:boolcprofile:cProfile.Profilecprofile_loops:int | Nonecprofile_dump:str | Nonecprofile_stats:dict[Any, Any] | Nonestats:dict[Any, Any] | NoneI'm not sure if it's best to roll this into the actual codebase or keep it in a
pyisidecar, but if it's decided that the hints should be inlined, these headers can be directly copied over.resolves #212