|
1 | 1 | import re
|
2 |
| -from typing import Any, Tuple |
| 2 | +from typing import Any, Dict, List, Tuple, Type, Union |
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 | from django_components import Component, SlotContent, types
|
@@ -427,3 +427,77 @@ def get_context_data(self, a, b, c, var1, var2, **attrs):
|
427 | 427 | "slot2": lambda ctx, data, ref: "abc",
|
428 | 428 | },
|
429 | 429 | )
|
| 430 | + |
| 431 | + @djc_test( |
| 432 | + components_settings={"extensions": [PydanticExtension]}, |
| 433 | + ) |
| 434 | + def test_handles_component_types(self): |
| 435 | + TestArgs = Tuple[Type[Component]] |
| 436 | + |
| 437 | + class TestKwargs(TypedDict): |
| 438 | + component: Type[Component] |
| 439 | + |
| 440 | + class TestComponent(Component[TestArgs, TestKwargs, Any, Any, Any, Any]): |
| 441 | + def get_context_data(self, a, component, **attrs): |
| 442 | + return { |
| 443 | + "component": component, |
| 444 | + } |
| 445 | + |
| 446 | + template: types.django_html = """ |
| 447 | + {% load component_tags %} |
| 448 | + Component: <strong>{{ component }}</strong> |
| 449 | + """ |
| 450 | + |
| 451 | + with pytest.raises( |
| 452 | + ValidationError, |
| 453 | + match=re.escape("Positional arguments of component 'TestComponent' failed validation"), |
| 454 | + ): |
| 455 | + TestComponent.render( |
| 456 | + args=[123], # type: ignore |
| 457 | + kwargs={"component": 1}, # type: ignore |
| 458 | + ) |
| 459 | + |
| 460 | + TestComponent.render( |
| 461 | + args=(TestComponent,), |
| 462 | + kwargs={"component": TestComponent}, |
| 463 | + ) |
| 464 | + |
| 465 | + def test_handles_typing_module(self): |
| 466 | + TodoArgs = Tuple[ |
| 467 | + Union[str, int], |
| 468 | + Dict[str, int], |
| 469 | + List[str], |
| 470 | + Tuple[int, Union[str, int]], |
| 471 | + ] |
| 472 | + |
| 473 | + class TodoKwargs(TypedDict): |
| 474 | + one: Union[str, int] |
| 475 | + two: Dict[str, int] |
| 476 | + three: List[str] |
| 477 | + four: Tuple[int, Union[str, int]] |
| 478 | + |
| 479 | + class TodoData(TypedDict): |
| 480 | + one: Union[str, int] |
| 481 | + two: Dict[str, int] |
| 482 | + three: List[str] |
| 483 | + four: Tuple[int, Union[str, int]] |
| 484 | + |
| 485 | + TodoComp = Component[TodoArgs, TodoKwargs, Any, TodoData, Any, Any] |
| 486 | + |
| 487 | + class TestComponent(TodoComp): |
| 488 | + def get_context_data(self, *args, **kwargs): |
| 489 | + return { |
| 490 | + **kwargs, |
| 491 | + } |
| 492 | + |
| 493 | + template = "" |
| 494 | + |
| 495 | + TestComponent.render( |
| 496 | + args=("str", {"str": 123}, ["a", "b", "c"], (123, "123")), |
| 497 | + kwargs={ |
| 498 | + "one": "str", |
| 499 | + "two": {"str": 123}, |
| 500 | + "three": ["a", "b", "c"], |
| 501 | + "four": (123, "123"), |
| 502 | + }, |
| 503 | + ) |
0 commit comments