Skip to content

Commit

Permalink
Use a property to define JsonLibrary (#57)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DMRobertson authored Feb 15, 2023
1 parent 93eb113 commit ebab5ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/canonicaljson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# 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
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:
Expand All @@ -44,15 +44,17 @@ 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):
JSONEncoder: Encoder
class JsonLibrary(Protocol): # pragma: no cover
@property
def JSONEncoder(self) -> Type[Encoder]:
pass


# Declare these in the module scope, but they get configured in
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ebab5ec

Please sign in to comment.