Skip to content
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

use type aliases for shorthand constraints #6

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions annotated_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
'Regex',
'Timezone',
'Predicate',
'IsLower',
'IsUpper',
'IsDigit',
'LowerCase',
'UpperCase',
'IsDigits',
'__version__',
)

Expand Down Expand Up @@ -146,7 +146,9 @@ class Predicate(BaseMetadata):
func: Callable[[Any], bool]


IsLower = Predicate(str.islower)
IsUpper = Predicate(str.isupper)
IsDigit = Predicate(str.isdigit)
IsAscii = Predicate(str.isascii)
StrType = TypeVar("StrType", bound=str)

LowerCase = Annotated[StrType, Predicate(str.islower)]
UpperCase = Annotated[StrType, Predicate(str.isupper)]
IsDigits = Annotated[StrType, Predicate(str.isdigit)]
IsAscii = Annotated[StrType, Predicate(str.isascii)]
8 changes: 4 additions & 4 deletions annotated_types/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def cases() -> Iterable[Case]:

# predicate types

yield Case(Annotated[str, at.IsLower], ['abc', 'foobar'], ['', 'A', 'Boom'])
yield Case(Annotated[str, at.IsUpper], ['ABC', 'DEFO'], ['', 'a', 'abc', 'AbC'])
yield Case(Annotated[str, at.IsDigit], ['123'], ['', 'ab', 'a1b2'])
yield Case(Annotated[str, at.IsAscii], ['123', 'foo bar'], ['£100', '😊', 'whatever 👀'])
yield Case(at.LowerCase[str], ['abc', 'foobar'], ['', 'A', 'Boom'])
yield Case(at.UpperCase[str], ['ABC', 'DEFO'], ['', 'a', 'abc', 'AbC'])
yield Case(at.IsDigits[str], ['123'], ['', 'ab', 'a1b2'])
yield Case(at.IsAscii[str], ['123', 'foo bar'], ['£100', '😊', 'whatever 👀'])

yield Case(Annotated[int, at.Predicate(lambda x: x % 2 == 0)], [0, 2, 4], [1, 3, 5])