From 20a7e94fc3e3958e9ea9ae0d14894e4ea21e6e26 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 15 Feb 2023 17:50:41 +0000 Subject: [PATCH 1/5] Use a property to define `Jsonlibrary` This is suggested/recommended at https://github.com/python/mypy/issues/6002 Discovered as part of https://github.com/matrix-org/synapse/pull/15052 --- src/canonicaljson/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py index d481262..8337de1 100644 --- a/src/canonicaljson/__init__.py +++ b/src/canonicaljson/__init__.py @@ -52,7 +52,9 @@ def __call__(self, *args: Any, **kwargs: Any) -> "Encoder": class JsonLibrary(Protocol): - JSONEncoder: Encoder + @property + def JSONEncoder(self) -> Encoder: + pass # Declare these in the module scope, but they get configured in From 7def2cb3d8ac10003f1d9347e6a4cd66943b064a Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 15 Feb 2023 18:12:51 +0000 Subject: [PATCH 2/5] Okay fine, coverage --- src/canonicaljson/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py index 8337de1..da3484c 100644 --- a/src/canonicaljson/__init__.py +++ b/src/canonicaljson/__init__.py @@ -51,7 +51,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "Encoder": pass -class JsonLibrary(Protocol): +class JsonLibrary(Protocol): # pragma: no cover @property def JSONEncoder(self) -> Encoder: pass From c3ef1fcd3e7f46870971ad98281e87b0b0f3b11d Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 15 Feb 2023 19:37:12 +0000 Subject: [PATCH 3/5] Fix the JsonLibrary protocol This is fiddlier than I expected. --- src/canonicaljson/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py index da3484c..cf5474c 100644 --- a/src/canonicaljson/__init__.py +++ b/src/canonicaljson/__init__.py @@ -15,7 +15,7 @@ # limitations under the License. import platform -from typing import Any, Generator, Optional, Type +from typing import Any, Generator, Iterator, Optional, Type try: from typing import Protocol @@ -44,16 +44,16 @@ class Encoder(Protocol): # pragma: no cover def encode(self, data: object) -> str: pass - def iterencode(self, data: object) -> Generator[str, None, None]: + def iterencode(self, data: object) -> Iterator[str]: pass - def __call__(self, *args: Any, **kwargs: Any) -> "Encoder": + def __init__(self, *args: Any, **kwargs: Any) -> None: pass class JsonLibrary(Protocol): # pragma: no cover @property - def JSONEncoder(self) -> Encoder: + def JSONEncoder(self) -> Type[Encoder]: pass From 9370d26f75d41e4779b22a64885ce626ec2dfe1d Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 15 Feb 2023 19:37:45 +0000 Subject: [PATCH 4/5] Test under mypy 1.0 --- src/canonicaljson/__init__.py | 2 +- tox.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py index cf5474c..ef33332 100644 --- a/src/canonicaljson/__init__.py +++ b/src/canonicaljson/__init__.py @@ -20,7 +20,7 @@ try: from typing import Protocol except ImportError: # pragma: no cover - from typing_extensions import Protocol # type: ignore[misc] + from typing_extensions import Protocol # type: ignore[assignment] frozendict_type: Optional[Type[Any]] try: diff --git a/tox.ini b/tox.ini index bdbf91e..74dee33 100644 --- a/tox.ini +++ b/tox.ini @@ -32,7 +32,7 @@ commands = python -m black --check --diff src tests [testenv:mypy] deps = - mypy==0.942 + mypy==1.0 types-frozendict==2.0.8 types-simplejson==3.17.5 types-setuptools==57.4.14 From e3c4f6068e3d93aaa7ff99ab8d9664b34cf6ab13 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 15 Feb 2023 19:37:50 +0000 Subject: [PATCH 5/5] Include mypy in default `tox` jobs --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 74dee33..a893107 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = packaging, pep8, black, py37, py38, py39, py310, pypy3 +envlist = packaging, pep8, black, py37, py38, py39, py310, pypy3, mypy, isort isolated_build = True [testenv:py]