8000 Use a property to define `JsonLibrary` by DMRobertson · Pull Request #57 · matrix-org/python-canonicaljson · GitHub
[go: up one dir, main page]

Skip to content

Use a property to define JsonLibrary #57

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

Merged
merged 5 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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":
< 8000 /td> 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]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would JSONEncoder: Type[Encoder] have fixed it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! Not AFAICS.

dmr on titan in python-canonicaljson on  dmr/typing-jsonmodule via 🐍 v3.11.1 (python-3.11.1) 
2023-02-15 20:17:14 ✔  $ git diff | cat
diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py
index ef33332..6f27b99 100644
--- a/src/canonicaljson/__init__.py
+++ b/src/canonicaljson/__init__.py
@@ -52,9 +52,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
 
 
 class JsonLibrary(Protocol):  # pragma: no cover
-    @property
-    def JSONEncoder(self) -> Type[Encoder]:
-        pass
+    JSONEncoder: Type[Encoder]

 
dmr on titan in python-canonicaljson on  dmr/typing-jsonmodule via 🐍 v3.11.1 (python-3.11.1) 
2023-02-15 20:17:54 ✔  $ mypy
src/canonicaljson/__init__.py:146: error: Argument 1 to "set_json_library" has incompatible type Module; expected "JsonLibrary"  [arg-type]
src/canonicaljson/__init__.py:146: note: Following member(s) of "Module json" have conflicts:
src/canonicaljson/__init__.py:146: note:     JSONEncoder: expected "Type[Encoder]", got "Type[JSONEncoder]"
tests/test_canonicaljson.py:169: error: Argument 1 to "set_json_library" has incompatible type Module; expected "JsonLibrary"  [arg-type]
tests/test_canonicaljson.py:169: note: Following member(s) of "Module json" have conflicts:
tests/test_canonicaljson.py:169: note:     JSONEncoder: expected "Type[Encoder]", got "Type[JSONEncoder]"
Found 2 errors in 2 files (checked 3 source files)

BB09 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