8000 Decorator syntax for type casting in decorators by emmeowzing · Pull Request #490 · python/typing · GitHub
[go: up one dir, main page]

Skip to content

Decorator syntax for type casting in decorators #490

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Tuple, List, MutableMapping
from typing import Callable
from typing import Generic, ClassVar, GenericMeta
from typing import cast
from typing import cast, castto
from typing import get_type_hints
from typing import no_type_check, no_type_check_decorator
from typing import Type
Expand Down Expand Up @@ -1321,6 +1321,61 @@ def test_errors(self):
cast('hello', 42)


class CasttoTests(BaseTestCase):

def test_basics(self):

@castto(int)
def send_to_float(x: int):
return float(x)

@castto(float)
def send_to_int(x: float):
return int(x)

@castto(Any)
def send_to_any(x: T) -> T:
return x

@castto(list)
def send_to_list(x: int) -> int:
return x

@castto(Union[str, float])
def send_to_Union_str_float(x: int) -> int:
return x

@castto(AnyStr)
def send_to_AnyStr(x: int) -> int:
return x

@castto(None)
def send_to_None(x: int) -> int:
return x

self.assertEqual(send_to_float(42), 42)
self.assertEqual(send_to_int(42.0), 42)
self.assertIs(type(send_to_int(42.0)), int)
self.assertEqual(send_to_any(42), 42)
self.assertEqual(send_to_list(42), 42)
self.assertEqual(send_to_Union_str_float(42), 42)
self.assertEqual(send_to_AnyStr(42), 42)
self.assertEqual(send_to_None(42), 42)

def test_errors(self):

@castto(42)
def bogus_one(x: int) -> int:
return x

@castto('hello')
def bogus_two(x: int) -> int:
return x

bogus_one(42)
bogus_two(42)


class ForwardRefTests(BaseTestCase):

def test_basics(self):
Expand Down
12 changes: 12 additions & 0 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
# One-off things.
'AnyStr',
'cast',
'castto',
'get_type_hints',
'NewType',
'no_type_check',
Expand Down Expand Up @@ -1436,6 +1437,17 @@ def cast(typ, val):
return val


def castto(typ):
""" Type cast decorator.

Same as cast, except written as a decorator. For use in decorators
with functools.wraps.
"""
def _cast(val):
return cast(typ, val)
return _cast


def _get_defaults(func):
"""Internal helper to extract the default arguments, by name."""
try:
Expand Down
0