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
2f841442
Kaydet (Commit)
2f841442
authored
Kas 15, 2016
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #28556: Allow keyword syntax for NamedTuple (Ivan Levkivskyi) (upstream #321)
üst
49e8f2d2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
37 deletions
+53
-37
test_typing.py
Lib/test/test_typing.py
+14
-0
typing.py
Lib/typing.py
+39
-37
No files found.
Lib/test/test_typing.py
Dosyayı görüntüle @
2f841442
...
...
@@ -1865,6 +1865,20 @@ class NamedTupleTests(BaseTestCase):
self
.
assertEqual
(
CoolEmployee
.
_fields
,
(
'name'
,
'cool'
))
self
.
assertEqual
(
CoolEmployee
.
_field_types
,
dict
(
name
=
str
,
cool
=
int
))
@skipUnless
(
PY36
,
'Python 3.6 required'
)
def
test_namedtuple_keyword_usage
(
self
):
LocalEmployee
=
NamedTuple
(
"LocalEmployee"
,
name
=
str
,
age
=
int
)
nick
=
LocalEmployee
(
'Nick'
,
25
)
self
.
assertIsInstance
(
nick
,
tuple
)
self
.
assertEqual
(
nick
.
name
,
'Nick'
)
self
.
assertEqual
(
LocalEmployee
.
__name__
,
'LocalEmployee'
)
self
.
assertEqual
(
LocalEmployee
.
_fields
,
(
'name'
,
'age'
))
self
.
assertEqual
(
LocalEmployee
.
_field_types
,
dict
(
name
=
str
,
age
=
int
))
with
self
.
assertRaises
(
TypeError
):
NamedTuple
(
'Name'
,
[(
'x'
,
int
)],
y
=
str
)
with
self
.
assertRaises
(
TypeError
):
NamedTuple
(
'Name'
,
x
=
1
,
y
=
'a'
)
def
test_pickle
(
self
):
global
Emp
# pickle wants to reference the class by name
Emp
=
NamedTuple
(
'Emp'
,
[(
'name'
,
str
),
(
'id'
,
int
)])
...
...
Lib/typing.py
Dosyayı görüntüle @
2f841442
...
...
@@ -1875,6 +1875,8 @@ class Type(Generic[CT_co], extra=type):
def
_make_nmtuple
(
name
,
types
):
msg
=
"NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
types
=
[(
n
,
_type_check
(
t
,
msg
))
for
n
,
t
in
types
]
nm_tpl
=
collections
.
namedtuple
(
name
,
[
n
for
n
,
t
in
types
])
nm_tpl
.
_field_types
=
dict
(
types
)
try
:
...
...
@@ -1884,55 +1886,55 @@ def _make_nmtuple(name, types):
return
nm_tpl
if
sys
.
version_info
[:
2
]
>=
(
3
,
6
):
class
NamedTupleMeta
(
type
):
_PY36
=
sys
.
version_info
[:
2
]
>=
(
3
,
6
)
def
__new__
(
cls
,
typename
,
bases
,
ns
,
*
,
_root
=
False
):
if
_root
:
return
super
()
.
__new__
(
cls
,
typename
,
bases
,
ns
)
types
=
ns
.
get
(
'__annotations__'
,
{})
return
_make_nmtuple
(
typename
,
types
.
items
())
class
NamedTuple
(
metaclass
=
NamedTupleMeta
,
_root
=
True
):
"""Typed version of namedtuple.
class
NamedTupleMeta
(
type
):
Usage::
def
__new__
(
cls
,
typename
,
bases
,
ns
):
if
ns
.
get
(
'_root'
,
False
):
return
super
()
.
__new__
(
cls
,
typename
,
bases
,
ns
)
if
not
_PY36
:
raise
TypeError
(
"Class syntax for NamedTuple is only supported"
" in Python 3.6+"
)
types
=
ns
.
get
(
'__annotations__'
,
{})
return
_make_nmtuple
(
typename
,
types
.
items
())
class Employee(NamedTuple):
name: str
id: int
class
NamedTuple
(
metaclass
=
NamedTupleMeta
):
"""Typed version of namedtuple.
This is equivalent to
::
Usage in Python versions >= 3.6
::
Employee = collections.namedtuple('Employee', ['name', 'id'])
class Employee(NamedTuple):
name: str
id: int
The resulting class has one extra attribute: _field_types,
giving a dict mapping field names to types. (The field names
are in the _fields attribute, which is part of the namedtuple
API.) Backward-compatible usage::
This is equivalent to::
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
"""
def
__new__
(
self
,
typename
,
fields
):
return
_make_nmtuple
(
typename
,
fields
)
else
:
def
NamedTuple
(
typename
,
fields
):
"""Typed version of namedtuple.
Employee = collections.namedtuple('Employee', ['name', 'id'])
Usage::
The resulting class has one extra attribute: _field_types,
giving a dict mapping field names to types. (The field names
are in the _fields attribute, which is part of the namedtuple
API.) Alternative equivalent keyword syntax is also accepted::
Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)]
)
Employee = NamedTuple('Employee', name=str, id=int
)
This is equivalent to
::
In Python versions <= 3.5 use
::
Employee = collections.namedtuple('Employee', ['name', 'id'])
The resulting class has one extra attribute: _field_types,
giving a dict mapping field names to types. (The field names
are in the _fields attribute, which is part of the namedtuple
API.)
"""
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
"""
_root
=
True
def
__new__
(
self
,
typename
,
fields
=
None
,
**
kwargs
):
if
kwargs
and
not
_PY36
:
raise
TypeError
(
"Keyword syntax for NamedTuple is only supported"
" in Python 3.6+"
)
if
fields
is
None
:
fields
=
kwargs
.
items
()
elif
kwargs
:
raise
TypeError
(
"Either list of fields or keywords"
" can be provided to NamedTuple, not both"
)
return
_make_nmtuple
(
typename
,
fields
)
...
...
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