forked from typeddjango/django-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix signature of Choices member creation, add
assert_type
test case…
…s, run `pyright` (typeddjango#2162) * Fix signature of Choices member creation * Add comment regarding overloads * Add pyright to CI, add test * Run mypy on the new test cases * Add more assertions, rename test folder * Update to `pyright==1.1.364` * Add `.gitattributes` for correct syntax highlighting * Python compat * [pre-commit.ci] auto fixes from pre-commit.com hooks * type ignore comments compatibility between pyright and mypy --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d03eaf1
commit e196985
Showing
8 changed files
with
121 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
scripts/allowlist_*.txt linguist-language=ini | ||
pyrightconfig*.json linguist-language=jsonc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", | ||
"include": [ | ||
"django-stubs", | ||
"ext/django_stubs_ext", | ||
"mypy_django_plugin", | ||
"scripts", | ||
], | ||
"exclude": [ | ||
".github", | ||
".mypy_cache", | ||
"build", | ||
// test cases use a custom config file | ||
"tests/", | ||
], | ||
"typeCheckingMode": "strict", | ||
"reportMissingTypeArgument": "warning", | ||
// Stubs are allowed to use private variables | ||
"reportPrivateUsage": "none", | ||
"stubPath": ".", | ||
"pythonVersion": "3.8", | ||
"pythonPlatform": "All", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/microsoft/pyright/main/packages/vscode-pyright/schemas/pyrightconfig.schema.json", | ||
"include": [ | ||
"tests/assert_type/" | ||
], | ||
"typeCheckingMode": "strict", | ||
// Extra strict settings | ||
"reportShadowedImports": "error", // Don't accidentally name a file something that shadows stdlib | ||
"reportImplicitStringConcatenation": "error", | ||
"reportUninitializedInstanceVariable": "error", | ||
"reportUnnecessaryTypeIgnoreComment": "error", | ||
// Don't use '# type: ignore' to suppress with pyright | ||
"enableTypeIgnoreComments": false, | ||
// If a test case uses this anti-pattern, there's likely a reason and annoying to `type: ignore`. | ||
// Let Ruff flag it (B006) | ||
"reportCallInDefaultInitializer": "none", | ||
// Too strict and not needed for type testing | ||
"reportMissingSuperCall": "none", | ||
// Stubs are allowed to use private variables. We may want to test those. | ||
"reportPrivateUsage": "none", | ||
// Stubs don't need the actual modules to be installed | ||
"reportMissingModuleSource": "none", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ Django==5.0.6; python_version >= '3.10' | |
|
||
# Overrides: | ||
mypy==1.10.0 | ||
pyright==1.1.364 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import List, Literal, Tuple | ||
|
||
from django.db.models import IntegerChoices, TextChoices | ||
from django.utils.translation import gettext_lazy as _ | ||
from typing_extensions import assert_type | ||
|
||
|
||
class MyIntegerChoices(IntegerChoices): | ||
A = 1 | ||
B = 2, "B" | ||
C = 3, "B", "..." # pyright: ignore[reportCallIssue] | ||
D = 4, _("D") | ||
E = 5, 1 # pyright: ignore[reportArgumentType] | ||
F = "1" | ||
|
||
|
||
assert_type(MyIntegerChoices.A, Literal[MyIntegerChoices.A]) | ||
assert_type(MyIntegerChoices.A.label, str) | ||
|
||
# For standard enums, type checkers may infer the type of a member's value | ||
# (e.g. `MyIntegerChoices.A.value` inferred as `Literal[1]`). | ||
# However, Django choices metaclass is using the last value for the label. | ||
# Type checkers relies on the stub definition of the `value` property, typed | ||
# as `int`/`str` for `IntegerChoices`/`TextChoices`. | ||
assert_type(MyIntegerChoices.A.value, int) | ||
|
||
|
||
class MyTextChoices(TextChoices): | ||
A = "a" | ||
B = "b", "B" | ||
C = "c", _("C") | ||
D = 1 # pyright: ignore[reportArgumentType] | ||
E = "e", 1 # pyright: ignore[reportArgumentType] | ||
|
||
|
||
assert_type(MyTextChoices.A, Literal[MyTextChoices.A]) | ||
assert_type(MyTextChoices.A.label, str) | ||
assert_type(MyTextChoices.A.value, str) | ||
|
||
|
||
# Assertions related to the metaclass: | ||
|
||
assert_type(MyIntegerChoices.values, List[int]) | ||
assert_type(MyIntegerChoices.choices, List[Tuple[int, str]]) | ||
assert_type(MyTextChoices.values, List[str]) | ||
assert_type(MyTextChoices.choices, List[Tuple[str, str]]) |