Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
70223d9b
Kaydet (Commit)
70223d9b
authored
May 24, 2016
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Added Type[C] implementation to typing.py. (Merge 3.5->3.6)
üst
80ac11d0
eb9aca3c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
1 deletion
+61
-1
test_typing.py
Lib/test/test_typing.py
+28
-0
typing.py
Lib/typing.py
+33
-1
No files found.
Lib/test/test_typing.py
Dosyayı görüntüle @
70223d9b
...
...
@@ -15,6 +15,7 @@ from typing import Generic
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
...
...
@@ -1373,6 +1374,33 @@ class OtherABCTests(BaseTestCase):
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
):
...
...
Lib/typing.py
Dosyayı görüntüle @
70223d9b
...
...
@@ -19,9 +19,10 @@ __all__ = [
'Callable'
,
'Generic'
,
'Optional'
,
'Tuple'
,
'Type'
,
'TypeVar'
,
'Union'
,
'Tuple'
,
# ABCs (from collections.abc).
'AbstractSet'
,
# collections.abc.Set.
...
...
@@ -447,6 +448,7 @@ class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
# 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.
...
...
@@ -456,6 +458,7 @@ VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
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
)
...
...
@@ -1572,6 +1575,35 @@ class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment