Skip to content

Commit ebab5ec

Browse files
authored
Use a property to define JsonLibrary (#57)
* Use a property to define `Jsonlibrary` This is suggested/recommended at python/mypy#6002 Discovered as part of matrix-org/synapse#15052 * Okay fine, coverage * Fix the JsonLibrary protocol This is fiddlier than I expected. * Test under mypy 1.0 * Include mypy in default `tox` jobs
1 parent 93eb113 commit ebab5ec

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/canonicaljson/__init__.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
# limitations under the License.
1616

1717
import platform
18-
from typing import Any, Generator, Optional, Type
18+
from typing import Any, Generator, Iterator, Optional, Type
1919

2020
try:
2121
from typing import Protocol
2222
except ImportError: # pragma: no cover
23-
from typing_extensions import Protocol # type: ignore[misc]
23+
from typing_extensions import Protocol # type: ignore[assignment]
2424

2525
frozendict_type: Optional[Type[Any]]
2626
try:
@@ -44,15 +44,17 @@ class Encoder(Protocol): # pragma: no cover
4444
def encode(self, data: object) -> str:
4545
pass
4646

47-
def iterencode(self, data: object) -> Generator[str, None, None]:
47+
def iterencode(self, data: object) -> Iterator[str]:
4848
pass
4949

50-
def __call__(self, *args: Any, **kwargs: Any) -> "Encoder":
50+
def __init__(self, *args: Any, **kwargs: Any) -> None:
5151
pass
5252

5353

54-
class JsonLibrary(Protocol):
55-
JSONEncoder: Encoder
54+
class JsonLibrary(Protocol): # pragma: no cover
55+
@property
56+
def JSONEncoder(self) -> Type[Encoder]:
57+
pass
5658

5759

5860
# Declare these in the module scope, but they get configured in

tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = packaging, pep8, black, py37, py38, py39, py310, pypy3
2+
envlist = packaging, pep8, black, py37, py38, py39, py310, pypy3, mypy, isort
33
isolated_build = True
44

55
[testenv:py]
@@ -32,7 +32,7 @@ commands = python -m black --check --diff src tests
3232

3333
[testenv:mypy]
3434
deps =
35-
mypy==0.942
35+
mypy==1.0
3636
types-frozendict==2.0.8
3737
types-simplejson==3.17.5
3838
types-setuptools==57.4.14

0 commit comments

Comments
 (0)