-
Notifications
You must be signed in to change notification settings - Fork 55
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
Refactor conditional raise assertion in tests #83
Conversation
Some parameterized tests have both values and exceptions as expected outcomes. With recent Pytest and Mypy versions, the type of the conditional context manager doesn't check when written using Python's ternary operator. Fix this by refactoring the conditional into a helper function which returns different context managers using an if..else instead. This also reduces duplication of code in tests.
097242d
to
cde5e0d
Compare
Happy to take a look at this after my shift today! |
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.
Looks nice
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.
Changes seem reasonable to me, especially given how much they've tightened up existing tests.
While continuing to experiment with different Python, Pytest and Mypy versions, I expanded the description of this PR with a simple way to reproduce the original situation. |
Also Mypy now fails on Python 3.6 and 3.7. Some clever solution is needed in Update: Solved, see reply below. |
With older versions, Mypy fails with: src/darker/tests/helpers.py:29: error: Returning Any from function declared to return "Union[RaisesContext[Any], ContextManager[None]]" Found 1 error in 1 file (checked 32 source files)
Sourcery Code Quality ReportThis PR has an average code quality of 71.86%
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Let us know what you think of it by mentioning @sourcery-ai in a comment. |
Fixed by updating Pytest to >6.1.0 which introduced type hints for the And by the way, I deem the import from private package |
Some parameterized tests have both values and exceptions as expected outcomes. With recent Pytest and Mypy versions, the type of the conditional context manager doesn't check when written using Python's ternary operator. We get these errors when Pytest runs Mypy checks for Darker:
The problem is that
pytest.raises
andcontextlib.nullcontext
in an... if ... else ...
ternary operator have no common ancestor classes, so Mypy assigns it the typeobject
. To reproduce, create this Python source file:Analyze it with Mypy in a Python 3.8 virtualenv:
python3.8 -m venv venv . venv/bin/activate pip install pytest==6.1.1 mypy==0.782 mypy ternary_context_manager.py
You'll get:
The
with
statement expects the expression to have__enter__
and__exit__
methods (i.e. to be a context manager), whichobject
of course doesn't have. At run time this isn't a problem, but for Mypy it is.Fix this by refactoring the conditional into a helper function which returns different context managers using an
if..else
instead. This also reduces duplication of code in tests.