|
| 1 | +- case: db_models_charfield_invalid_choices |
| 2 | + main: | |
| 3 | + from django.db import models |
| 4 | +
|
| 5 | + class MyModel(models.Model): |
| 6 | + char1 = models.CharField(max_length=200, choices='test') |
| 7 | + out: | |
| 8 | + main:4: error: Argument "choices" to "CharField" has incompatible type "str"; expected "Union[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]], Mapping[Any, Any], Type[Choices], Callable[[], Union[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]], Mapping[Any, Any]]], None]" [arg-type] |
| 9 | + main:4: note: Following member(s) of "str" have conflicts: |
| 10 | + main:4: note: Expected: |
| 11 | + main:4: note: def __iter__(self) -> Iterator[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]] |
| 12 | + main:4: note: Got: |
| 13 | + main:4: note: def __iter__(self) -> Iterator[str] |
| 14 | +
|
| 15 | +- case: db_models_integerfield_invalid_choices |
| 16 | + main: | |
| 17 | + from django.db import models |
| 18 | +
|
| 19 | + class MyModel(models.Model): |
| 20 | + int1 = models.IntegerField(choices='test') |
| 21 | + out: | |
| 22 | + main:4: error: Argument "choices" to "IntegerField" has incompatible type "str"; expected "Union[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]], Mapping[Any, Any], Type[Choices], Callable[[], Union[Iterable[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]], Mapping[Any, Any]]], None]" [arg-type] |
| 23 | + main:4: note: Following member(s) of "str" have conflicts: |
| 24 | + main:4: note: Expected: |
| 25 | + main:4: note: def __iter__(self) -> Iterator[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]] |
| 26 | + main:4: note: Got: |
| 27 | + main:4: note: def __iter__(self) -> Iterator[str] |
| 28 | +
|
| 29 | +- case: db_models_valid_choices |
| 30 | + main: | |
| 31 | + from collections.abc import Callable, Mapping, Sequence |
| 32 | + from typing import TypeVar |
| 33 | +
|
| 34 | + from django.db import models |
| 35 | + from typing_extensions import assert_type |
| 36 | +
|
| 37 | + _T = TypeVar("_T") |
| 38 | +
|
| 39 | +
|
| 40 | + def to_named_seq(func: Callable[[], _T]) -> Callable[[], Sequence[tuple[str, _T]]]: |
| 41 | + def inner() -> Sequence[tuple[str, _T]]: |
| 42 | + return [("title", func())] |
| 43 | +
|
| 44 | + return inner |
| 45 | +
|
| 46 | +
|
| 47 | + def to_named_mapping(func: Callable[[], _T]) -> Callable[[], Mapping[str, _T]]: |
| 48 | + def inner() -> Mapping[str, _T]: |
| 49 | + return {"title": func()} |
| 50 | +
|
| 51 | + return inner |
| 52 | +
|
| 53 | +
|
| 54 | + def str_tuple() -> Sequence[tuple[str, str]]: |
| 55 | + return (("foo", "bar"), ("fuzz", "bazz")) |
| 56 | +
|
| 57 | +
|
| 58 | + def str_mapping() -> Mapping[str, str]: |
| 59 | + return {"foo": "bar", "fuzz": "bazz"} |
| 60 | +
|
| 61 | +
|
| 62 | + def int_tuple() -> Sequence[tuple[int, str]]: |
| 63 | + return ((1, "bar"), (2, "bazz")) |
| 64 | +
|
| 65 | +
|
| 66 | + def int_mapping() -> Mapping[int, str]: |
| 67 | + return {3: "bar", 4: "bazz"} |
| 68 | +
|
| 69 | +
|
| 70 | + class TestModel(models.Model): |
| 71 | + class TextChoices(models.TextChoices): |
| 72 | + FIRST = "foo", "bar" |
| 73 | + SECOND = "foo2", "bar" |
| 74 | +
|
| 75 | + class IntegerChoices(models.IntegerChoices): |
| 76 | + FIRST = 1, "bar" |
| 77 | + SECOND = 2, "bar" |
| 78 | +
|
| 79 | + char1 = models.CharField(max_length=5, choices=TextChoices, default="foo") |
| 80 | + char2 = models.CharField(max_length=5, choices=str_tuple, default="foo") |
| 81 | + char3 = models.CharField(max_length=5, choices=str_mapping, default="foo") |
| 82 | + char4 = models.CharField(max_length=5, choices=str_tuple(), default="foo") |
| 83 | + char5 = models.CharField(max_length=5, choices=str_mapping(), default="foo") |
| 84 | + char6 = models.CharField(max_length=5, choices=to_named_seq(str_tuple), default="foo") |
| 85 | + char7 = models.CharField(max_length=5, choices=to_named_mapping(str_mapping), default="foo") |
| 86 | + char8 = models.CharField(max_length=5, choices=to_named_seq(str_tuple)(), default="foo") |
| 87 | + char9 = models.CharField(max_length=5, choices=to_named_mapping(str_mapping)(), default="foo") |
| 88 | +
|
| 89 | + int1 = models.IntegerField(choices=IntegerChoices, default=1) |
| 90 | + int2 = models.IntegerField(choices=int_tuple, default=1) |
| 91 | + int3 = models.IntegerField(choices=int_mapping, default=1) |
| 92 | + int4 = models.IntegerField(choices=int_tuple(), default=1) |
| 93 | + int5 = models.IntegerField(choices=int_mapping(), default=1) |
| 94 | + int6 = models.IntegerField(choices=to_named_seq(int_tuple), default=1) |
| 95 | + int7 = models.IntegerField(choices=to_named_seq(int_mapping), default=1) |
| 96 | + int8 = models.IntegerField(choices=to_named_seq(int_tuple)(), default=1) |
| 97 | + int9 = models.IntegerField(choices=to_named_seq(int_mapping)(), default=1) |
0 commit comments