Skip to content

Commit b05388c

Browse files
update unit tests for databind.core
1 parent eebdc06 commit b05388c

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

databind.core/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ isort = "isort --check-only src/"
4848
flake8 = "flake8 src/"
4949

5050
[tool.slap.run]
51-
fmt = "black src/ tests/ && isort src/ tests/"
51+
fmt = "black src/ && isort src/"
5252

5353
[tool.mypy]
5454
python_version = "3.6"

databind.core/src/databind/core/schema.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
from types import GenericAlias
2222

2323
if t.TYPE_CHECKING:
24-
25-
class Constructor(t.Protocol):
26-
def __call__(self, **kwargs: t.Any) -> t.Any:
27-
...
24+
Constructor = t.Callable[..., t.Any]
2825

2926

3027
__all__ = [
@@ -257,7 +254,7 @@ class A(Generic[T]):
257254
return Schema(fields, t.cast("Constructor", dataclass_type), dataclass_type)
258255

259256

260-
def convert_typed_dict_to_schema(typed_dict: TypedDictProtocol) -> Schema:
257+
def convert_typed_dict_to_schema(typed_dict: t.Union[TypedDictProtocol, t.Type[t.Any], TypeHint]) -> Schema:
261258
"""Converts the definition of a #typing.TypedDict to a #Schema.
262259
263260
!!! note
@@ -292,6 +289,11 @@ class Movie(typing.TypedDict):
292289
```
293290
"""
294291

292+
if isinstance(typed_dict, TypeHint):
293+
if not isinstance(typed_dict, ClassTypeHint):
294+
raise TypeError(f"expected ClassTypeHint, got {typed_dict}")
295+
typed_dict = typed_dict.type
296+
295297
assert is_typed_dict(typed_dict), typed_dict
296298

297299
eval_context = vars(sys.modules[typed_dict.__module__])

databind.core/src/databind/core/tests/context_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from databind.core.settings import Settings
77

88

9-
def test_format_context_trace():
9+
def test_format_context_trace() -> None:
1010
settings = Settings()
1111
location = Location.EMPTY
1212

13-
def no_convert(*a):
13+
def no_convert(*a: t.Any) -> None:
1414
raise NotImplementedError
1515

1616
ctx1 = Context(

databind.core/src/databind/core/tests/schema_docspec_example_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Module(HasMembers):
2222
members: t.List["Module"]
2323

2424

25-
def test__convert_dataclass_to_schema__multiple_levels_of_inheritance():
25+
def test__convert_dataclass_to_schema__multiple_levels_of_inheritance() -> None:
2626
assert convert_dataclass_to_schema(ApiObject) == Schema(
2727
{
2828
"location": Field(TypeHint(str), True),

databind.core/src/databind/core/tests/schema_test.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from databind.core.utils import T, U
1717

1818

19-
def test_convert_to_schema_dataclass():
19+
def test_convert_to_schema_dataclass() -> None:
2020
# Test dataclass detection
2121
@dataclasses.dataclass
2222
class A:
@@ -58,7 +58,7 @@ class Movie(te.TypedDict):
5858
)
5959

6060

61-
def test_get_fields_expanded():
61+
def test_get_fields_expanded() -> None:
6262
class Dict1(te.TypedDict):
6363
a: int
6464
b: str
@@ -105,7 +105,7 @@ class Class4:
105105
}
106106

107107

108-
def test_convert_dataclass_to_schema_simple():
108+
def test_convert_dataclass_to_schema_simple() -> None:
109109
@dataclasses.dataclass
110110
class A:
111111
a: int
@@ -144,7 +144,7 @@ class A:
144144
# )
145145

146146

147-
def test_convert_dataclass_to_schema_with_defaults():
147+
def test_convert_dataclass_to_schema_with_defaults() -> None:
148148
@dataclasses.dataclass
149149
class A:
150150
a: int = 42
@@ -162,7 +162,7 @@ class A:
162162
)
163163

164164

165-
def test_convert_dataclass_with_optional_field_has_none_as_default():
165+
def test_convert_dataclass_with_optional_field_has_none_as_default() -> None:
166166
@dataclasses.dataclass
167167
class A:
168168
a: t.Optional[int]
@@ -187,7 +187,7 @@ class A:
187187
)
188188

189189

190-
def test_convert_dataclass_to_schema_nested():
190+
def test_convert_dataclass_to_schema_nested() -> None:
191191
@dataclasses.dataclass
192192
class A:
193193
a: int
@@ -207,7 +207,7 @@ class B:
207207
)
208208

209209

210-
def test_convert_dataclass_to_schema_inheritance():
210+
def test_convert_dataclass_to_schema_inheritance() -> None:
211211
@dataclasses.dataclass
212212
class A:
213213
a: int
@@ -226,7 +226,7 @@ class B(A):
226226
)
227227

228228

229-
def test_convert_dataclass_to_schema_generic():
229+
def test_convert_dataclass_to_schema_generic() -> None:
230230
@dataclasses.dataclass
231231
class A(t.Generic[T]):
232232
a: T
@@ -247,20 +247,20 @@ class A(t.Generic[T]):
247247
)
248248

249249

250-
def test_convert_dataclass_overriden_field_type():
250+
def test_convert_dataclass_overriden_field_type() -> None:
251251
@dataclasses.dataclass
252252
class A:
253253
a: int
254254
b: str
255255

256256
@dataclasses.dataclass
257257
class B(A):
258-
a: str
258+
a: str # type: ignore[assignment]
259259

260260
assert convert_dataclass_to_schema(B) == Schema({"a": Field(TypeHint(str)), "b": Field(TypeHint(str))}, B, B)
261261

262262

263-
def test_convert_dataclass_to_schema_type_var_without_generic():
263+
def test_convert_dataclass_to_schema_type_var_without_generic() -> None:
264264
@dataclasses.dataclass
265265
class A:
266266
a: T # type: ignore[valid-type]
@@ -287,7 +287,7 @@ class B(A, t.Generic[T]):
287287
)
288288

289289

290-
def test_convert_dataclass_to_schema_generic_nested():
290+
def test_convert_dataclass_to_schema_generic_nested() -> None:
291291
@dataclasses.dataclass
292292
class A(t.Generic[T]):
293293
a: T
@@ -312,7 +312,7 @@ class B2(t.Generic[U]):
312312
)
313313
assert convert_dataclass_to_schema(B2) == Schema(
314314
{
315-
"a": Field(TypeHint(A[U])),
315+
"a": Field(TypeHint(A[U])), # type: ignore[valid-type] # Type variable U is unbound
316316
"b": Field(TypeHint(str)),
317317
},
318318
B2,
@@ -328,7 +328,7 @@ class B2(t.Generic[U]):
328328
)
329329

330330

331-
def test_convert_dataclass_to_schema_generic_inheritance():
331+
def test_convert_dataclass_to_schema_generic_inheritance() -> None:
332332
@dataclasses.dataclass
333333
class A(t.Generic[T]):
334334
a: T
@@ -368,7 +368,7 @@ class B2(A[U], t.Generic[U]):
368368
)
369369

370370

371-
def test_convert_dataclass_with_mapping_member():
371+
def test_convert_dataclass_with_mapping_member() -> None:
372372
@dataclasses.dataclass
373373
class A:
374374
a: int
@@ -384,10 +384,10 @@ class A:
384384
)
385385

386386

387-
def test_convert_typed_dict_to_schema_total():
387+
def test_convert_typed_dict_to_schema_total() -> None:
388388
class Movie(te.TypedDict):
389389
name: str
390-
year: int = 42 # type: ignore[misc]
390+
year: int = 42 # type: ignore[misc] # Right hand side values are not supported in TypedDict
391391

392392
assert convert_typed_dict_to_schema(Movie) == Schema(
393393
{
@@ -399,9 +399,9 @@ class Movie(te.TypedDict):
399399
)
400400

401401

402-
def test_convert_typed_dict_to_schema_functional():
402+
def test_convert_typed_dict_to_schema_functional() -> None:
403403
Movie = te.TypedDict("Movie", {"name": str, "year": int})
404-
Movie.year = 42
404+
Movie.year = 42 # type: ignore[attr-defined] # Type[Movie] has no attribute 'year'
405405
assert convert_typed_dict_to_schema(Movie) == Schema(
406406
{
407407
"name": Field(TypeHint(str)),
@@ -412,7 +412,7 @@ def test_convert_typed_dict_to_schema_functional():
412412
)
413413

414414

415-
def test_convert_typed_dict_to_schema_not_total():
415+
def test_convert_typed_dict_to_schema_not_total() -> None:
416416
class Movie(te.TypedDict, total=False):
417417
name: str
418418
year: int
@@ -427,7 +427,7 @@ class Movie(te.TypedDict, total=False):
427427
)
428428

429429

430-
def test_convert_typed_dict_with_optional_field_has_none_as_default():
430+
def test_convert_typed_dict_with_optional_field_has_none_as_default() -> None:
431431
class A(te.TypedDict, total=False):
432432
a: t.Optional[int]
433433
c: te.Annotated[t.Optional[int], "foo"]
@@ -450,7 +450,7 @@ class ClassWithForwardRef:
450450
b: t.List["int"]
451451

452452

453-
def test_parse_dataclass_with_forward_ref():
453+
def test_parse_dataclass_with_forward_ref() -> None:
454454
assert convert_dataclass_to_schema(ClassWithForwardRef) == Schema(
455455
{"a": Field(TypeHint(int), True), "b": Field(TypeHint(t.List[int]), True)},
456456
ClassWithForwardRef,

0 commit comments

Comments
 (0)