Skip to content

Commit

Permalink
Fix error message for the json schemas of non str-keyed mappings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchanial committed May 15, 2022
1 parent 99e6b23 commit c8ad816
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- id: black
args: [-C]
Expand Down
4 changes: 2 additions & 2 deletions apischema/json_schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ def mapping(
with context_setter(self):
self._ignore_first_ref = True
key = self.visit(key_type)
if key["type"] != JsonType.STRING:
raise ValueError("Mapping types must string-convertible key")
if "type" not in key or key["type"] != JsonType.STRING:
raise ValueError("Mapping types must have string-convertible keys")
value = self.visit(value_type)
if "pattern" in key:
return json_schema(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date
from typing import Any, Dict, Mapping

import pytest

Expand All @@ -8,6 +9,20 @@
from apischema.typing import Annotated, TypedDict


class MyDict(dict):
pass


@pytest.mark.parametrize(
"tp", [dict, Dict[int, Any], pytest.param(MyDict, marks=pytest.mark.xfail), Mapping]
)
def test_dict(tp):
with pytest.raises(ValueError, match="string-convertible keys"):
deserialization_schema(tp)
with pytest.raises(ValueError, match="string-convertible keys"):
serialization_schema(tp)


class TD1(TypedDict, total=False):
key1: str

Expand Down

0 comments on commit c8ad816

Please sign in to comment.