Skip to content

Add type hints to tests #50

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

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed

- Add type hints to tests ([#50])
- Drop explicit support for Python 3.6 ([#48])

## [1.4.0] - 2022-11-08
Expand Down Expand Up @@ -77,6 +78,7 @@ Initial version of `codetiming`. Version 1.0.0 corresponds to the code in the tu
[1.1.0]: https://github.com/realpython/codetiming/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/realpython/codetiming/releases/tag/v1.0.0

[#50]: https://github.com/realpython/codetiming/pull/50
[#48]: https://github.com/realpython/codetiming/pull/48
[#47]: https://github.com/realpython/codetiming/pull/47
[#46]: https://github.com/realpython/codetiming/pull/46
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Run tests using [`tox`](https://tox.readthedocs.io/). You can also run individua
- Static type hinting using [`mypy`](http://mypy-lang.org/). Test your type hints as follows:

```console
$ mypy --strict codetiming/
$ mypy --strict codetiming/ tests/
```

See Real Python's [Python Type Checking guide](https://realpython.com/python-type-checking/) for more information.
Expand Down
30 changes: 25 additions & 5 deletions codetiming/_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@
import time
from contextlib import ContextDecorator
from dataclasses import dataclass, field
from typing import Any, Callable, ClassVar, Optional, Union
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, Union

# Codetiming imports
from codetiming._timers import Timers

# Special types, Protocol only works for Python >= 3.8
if TYPE_CHECKING: # pragma: nocover
# Standard library imports
from typing import Protocol, TypeVar

T = TypeVar("T")

class FloatArg(Protocol):
"""Protocol type that allows classes that take one float argument"""

def __call__(self: T, __seconds: float) -> T:
"""Callable signature"""
... # pragma: nocover

else:

class FloatArg:
"""Dummy runtime class"""


# Timer code
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class."""

Expand All @@ -24,12 +44,12 @@ class Timer(ContextDecorator):
"""Time your code using a class, context manager, or decorator."""

timers: ClassVar[Timers] = Timers()
_start_time: Optional[float] = field(default=None, init=False, repr=False)
name: Optional[str] = None
text: Union[str, Callable[[float], str]] = "Elapsed time: {:0.4f} seconds"
initial_text: Union[bool, str] = False
text: Union[str, FloatArg, Callable[[float], str]] = "Elapsed time: {:0.4f} seconds"
initial_text: Union[bool, str, FloatArg] = False
logger: Optional[Callable[[str], None]] = print
last: float = field(default=math.nan, init=False, repr=False)
_start_time: Optional[float] = field(default=None, init=False, repr=False)

def start(self) -> None:
"""Start a new timer."""
Expand Down Expand Up @@ -69,7 +89,7 @@ def stop(self) -> float:
"minutes": self.last / 60,
}
text = self.text.format(self.last, **attributes)
self.logger(text)
self.logger(str(text))
if self.name:
self.timers.add(self.name, self.last)

Expand Down
Loading