-
Notifications
You must be signed in to change notification settings - Fork 12
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
Support static type checking #59
base: master
Are you sure you want to change the base?
Support static type checking #59
Conversation
I came here to see if there was already an effort to provide typehints for this package, great to find a PR! Have you considered adding a github workflow step and / or a tox environment to verify the hints? Otherwise it'll be hard to maintain the annotations long term. |
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.
Can you also add a type hint for pytest_addoption()
?
if TYPE_CHECKING:
from _pytest.config.argparsing import Parser
def pytest_addoption(parser: Parser) -> None:
and for the snapshot()
fixture:
def snapshot(request: pytest.FixtureRequest) -> Iterator["Snapshot"]:
(import typing.Iterator
). This, incidentally, shows that there is a bug here; it makes the assumption FixtureRequest.node
is always a Function
node. It could do with an assertion or check to validate that assumption!
The __exit__
method also needs type hints; just replace the signature with:
def __exit__(self, *_: Any) -> None:
as the exception info is ignored.
For _snapshot_path
:
def _snapshot_path(self, snapshot_name: Union[str, os.PathLike[str]]) -> Path:
If you were to apply all my review notes, then pyright
gives this project a full 100% score on type completeness:
pyright --verifytypes pytest_snapshot --ignoreexternal
Module name: "pytest_snapshot"
Package directory: ...
Module directory: ...
Path of py.typed file: ...
Public modules: 2
pytest_snapshot
pytest_snapshot.plugin
Symbols used in public interface:
Symbols exported by "pytest_snapshot": 10
With known type: 10
With ambiguous type: 0
With unknown type: 0
(Ignoring unknown types imported from other packages)
Functions without docstring: 3
Functions without default param: 0
Classes without docstring: 1
Other symbols referenced but not exported by "pytest_snapshot": 0
With known type: 0
With ambiguous type: 0
With unknown type: 0
Type completeness score: 100%
--ignoreexternal
is needed because pytest types would otherwise affect the score.
There are some other things mypy
, using the default config, will complain about; inside assert_match
the snapshot_diff_msg
needs a typehint to silence one of those:
if encoded_expected_value is not None:
expected_value = decode(encoded_expected_value)
+ snapshot_diff_msg: Optional[str]
try:
That lets mypy know assigning None
first doesn't mean you can't make it a str
later on.
The remaining three I'd probably just silence with ignores. Or investigate if they could be a problem.
Thanks for the review! I'll try to look into it within a week. |
Ready for review. Description updated. Here's some comments:
I was hoping this would be a quick small improvement to the library. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #59 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 8
Lines 468 485 +17
Branches 53 53
=========================================
+ Hits 468 485 +17
☔ View full report in Codecov by Sentry. |
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.
Hello, thank you for the great PR!
Sorry for the late review. I fixed a few small issues and this can be merged. Could you squash everything into a single commit?
Once it is squashed, I'll merge the PR.
It will be really great to have this (I was thinking to implement this myself before I saw @lonelyteapot already did the work). @joseph-roitman Perhaps you can use GitHub's "Squash and merge" button, then you don't need to wait for @lonelyteapot to do it. |
This pull request improves type annotations in the package, making it PEP 561-compatible.
In details:
py.typed
marker.mypy
andpyright
tox environments to ensure full compatibility with those tools.Thanks to @mjpieters for contributing some of the code and ideas.
If you're unfamiliar with mypy, check these materials:
https://dev.to/whtsky/don-t-forget-py-typed-for-your-typed-python-package-2aa3
https://mypy.readthedocs.io/en/latest/installed_packages.html#creating-pep-561-compatible-packages