Skip to content

Add Type[C] "implementation" #224

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

Merged
merged 4 commits into from
May 24, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Callable
from typing import Generic
from typing import cast
from typing import Type
from typing import NamedTuple
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
Expand Down Expand Up @@ -1110,6 +1111,36 @@ def __len__(self):
self.assertIsSubclass(MMC, typing.Mapping)


class TypeTests(BaseTestCase):

def test_type_basic(self):

class User(object): pass
class BasicUser(User): pass
class ProUser(User): pass

def new_user(user_class):
# type: (Type[User]) -> User
return user_class()

joe = new_user(BasicUser)

def test_type_typevar(self):

class User(object): pass
class BasicUser(User): pass
class ProUser(User): pass

global U
U = TypeVar('U', bound=User)

def new_user(user_class):
# type: (Type[U]) -> U
return user_class()

joe = new_user(BasicUser)


class NamedTupleTests(BaseTestCase):

def test_basics(self):
Expand Down
32 changes: 32 additions & 0 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ def __subclasscheck__(self, cls):


# Some unconstrained type variables. These are used by the container types.
# (These are not for export.)
T = TypeVar('T') # Any type.
KT = TypeVar('KT') # Key type.
VT = TypeVar('VT') # Value type.
Expand All @@ -463,6 +464,7 @@ def __subclasscheck__(self, cls):
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.

# A useful type variable with constraints. This represents string types.
# (This one *is* for export!)
AnyStr = TypeVar('AnyStr', bytes, unicode)


Expand Down Expand Up @@ -1518,6 +1520,36 @@ def __new__(cls, *args, **kwds):
return super(Generator, cls).__new__(cls, *args, **kwds)


# Internal type variable used for Type[].
CT = TypeVar('CT', covariant=True, bound=type)


class Type(type, Generic[CT]):
"""A generic type usable to annotate class objects.

For example, suppose we have the following classes::

class User: ... # Abstract base for User classes
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...

And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class::

U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U:
user = user_class()
# (Here we could write the user object to a database)
return user

joe = new_user(BasicUser)

At this point the type checker knows that joe has type BasicUser.
"""
__extra__ = type


def NamedTuple(typename, fields):
"""Typed version of namedtuple.

Expand Down
28 changes: 28 additions & 0 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import cast
from typing import get_type_hints
from typing import no_type_check, no_type_check_decorator
from typing import Type
from typing import NamedTuple
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
Expand Down Expand Up @@ -1373,6 +1374,33 @@ def manager():
self.assertNotIsInstance(42, typing.ContextManager)


class TypeTests(BaseTestCase):

def test_type_basic(self):

class User: pass
class BasicUser(User): pass
class ProUser(User): pass

def new_user(user_class: Type[User]) -> User:
return user_class()

joe = new_user(BasicUser)

def test_type_typevar(self):

class User: pass
class BasicUser(User): pass
class ProUser(User): pass

U = TypeVar('U', bound=User)

def new_user(user_class: Type[U]) -> U:
return user_class()

joe = new_user(BasicUser)


class NamedTupleTests(BaseTestCase):

def test_basics(self):
Expand Down
31 changes: 31 additions & 0 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ def __subclasscheck__(self, cls):


# Some unconstrained type variables. These are used by the container types.
# (These are not for export.)
T = TypeVar('T') # Any type.
KT = TypeVar('KT') # Key type.
VT = TypeVar('VT') # Value type.
Expand All @@ -456,6 +457,7 @@ def __subclasscheck__(self, cls):
T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.

# A useful type variable with constraints. This represents string types.
# (This one *is* for export!)
AnyStr = TypeVar('AnyStr', bytes, str)


Expand Down Expand Up @@ -1572,6 +1574,35 @@ def __new__(cls, *args, **kwds):
return super().__new__(cls, *args, **kwds)


# Internal type variable used for Type[].
CT = TypeVar('CT', covariant=True, bound=type)


class Type(type, Generic[CT], extra=type):
"""A generic type usable to annotate class objects.

For example, suppose we have the following classes::

class User: ... # Abstract base for User classes
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...

And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class::

U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U:
user = user_class()
# (Here we could write the user object to a database)
return user

joe = new_user(BasicUser)

At this point the type checker knows that joe has type BasicUser.
"""


def NamedTuple(typename, fields):
"""Typed version of namedtuple.

Expand Down