-
-
Couldn't load subscription status.
- Fork 516
Narrow RegexValidator.regex instance level type to Pattern[str]
#2650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
03f7ea8
d7f9a54
e86f563
4647a3e
af7948e
ec1a145
0ca8419
31b6939
1d4eff0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| from collections.abc import Callable, Collection, Sequence, Sized | ||
| from decimal import Decimal | ||
| from re import Pattern, RegexFlag | ||
| from typing import Any, TypeAlias | ||
| from typing import Any, Generic, TypeAlias, TypeVar, overload, type_check_only | ||
|
|
||
| from django.core.files.base import File | ||
| from django.utils.deconstruct import _Deconstructible | ||
|
|
@@ -13,8 +13,19 @@ _Regex: TypeAlias = str | Pattern[str] | |
|
|
||
| _ValidatorCallable: TypeAlias = Callable[[Any], None] # noqa: PYI047 | ||
|
|
||
| _ClassT = TypeVar("_ClassT") | ||
| _InstanceT = TypeVar("_InstanceT") | ||
|
|
||
| @type_check_only | ||
| class ClassOrInstanceAttribute(Generic[_ClassT, _InstanceT]): | ||
| @overload | ||
| def __get__(self, obj: None, owner: type[object]) -> _ClassT: ... | ||
| @overload | ||
| def __get__(self, obj: object, owner: type[object]) -> _InstanceT: ... | ||
| def __set__(self, obj: object, value: _InstanceT) -> None: ... | ||
|
||
|
|
||
| class RegexValidator(_Deconstructible): | ||
| regex: _Regex # Pattern[str] on instance, but may be str on class definition | ||
| regex: ClassOrInstanceAttribute[_Regex, Pattern[str]] | ||
UnknownPlatypus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| message: _StrOrPromise | ||
| code: str | ||
| inverse_match: bool | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from re import Pattern | ||
| from typing import assert_type | ||
|
|
||
| from django.contrib.auth.validators import UnicodeUsernameValidator | ||
| from django.core.validators import RegexValidator | ||
|
|
||
| assert_type(RegexValidator.regex, str | Pattern[str]) | ||
| assert_type(RegexValidator().regex, Pattern[str]) | ||
UnknownPlatypus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert_type(UnicodeUsernameValidator.regex, str | Pattern[str]) | ||
| assert_type(UnicodeUsernameValidator().regex, Pattern[str]) | ||
Uh oh!
There was an error while loading. Please reload this page.