Skip to content

Commit

Permalink
Prefer unittest.mock but require mock for python < 3.3.
Browse files Browse the repository at this point in the history
The unit tests require mock but don't specify it, which causes
problems for any usages outside tox (e.g. testing that changes to
twisted don't break klein).  Use PEP508 to specify environment markers
and only install mock when required.  Tests can then conditionally
import unittest.mock and fall back to mock.

The conditional imports do raise a mypy error, discussed here:
    python/mypy#1153

So an annotation is required to silence this specific warning.
  • Loading branch information
jlitzingerdev committed Nov 17, 2017
1 parent 61c0661 commit adc6341
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock ; python_version <= '3.3'
5 changes: 4 additions & 1 deletion src/klein/test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import sys

from mock import Mock, patch
try:
from unittest.mock import Mock, patch
except Exception:
from mock import Mock, patch # type:ignore

from twisted.python.components import registerAdapter
from twisted.trial import unittest
Expand Down
5 changes: 4 additions & 1 deletion src/klein/test/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import os
from io import BytesIO

from mock import Mock, call
try:
from unittest.mock import Mock, call
except Exception:
from mock import Mock, call # type:ignore

from six.moves.urllib.parse import parse_qs

Expand Down

0 comments on commit adc6341

Please sign in to comment.