FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-43923: Allow NamedTuple multiple inheritance by juliusgeo · Pull Request #31779 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4947,9 +4947,10 @@ def _source(self):
def test_multiple_inheritance(self):
class A:
pass
with self.assertRaises(TypeError):
class X(NamedTuple, A):
x: int
class X(NamedTuple, A):
x: int
class Y(X, A):
y: int

def test_namedtuple_keyword_usage(self):
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
Expand Down Expand Up @@ -5282,6 +5283,11 @@ def test_get_type_hints(self):
{'a': typing.Optional[int], 'b': int}
)

def test_generic_subclasses(self):
T = TypeVar("T")
class GenericNamedTuple(NamedTuple, Generic[T]):
pass


class IOTests(BaseTestCase):

Expand Down
35 changes: 16 additions & 19 deletions Lib/typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2601,7 +2601,9 @@ def _make_nmtuple(name, types, module, defaults = ()):
for n, t in types}
nm_tpl = collections.namedtuple(name, fields,
defaults=defaults, module=module)
nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
nm_tpl.__annotations__ = types
nm_tpl.__new__.__annotations__ = types
nm_tpl.__new__.__defaults__ = tuple(defaults)
return nm_tpl


Expand All @@ -2616,7 +2618,9 @@ def _make_nmtuple(name, types, module, defaults = ()):
class NamedTupleMeta(type):

def __new__(cls, typename, bases, ns):
assert bases[0] is _NamedTuple
if ns.get('_root', False):
return super().__new__(cls, typename, bases, ns)
assert NamedTuple in bases
types = ns.get('__annotations__', {})
default_names = []
for field_name in types:
Expand All @@ -2639,7 +2643,7 @@ def __new__(cls, typename, bases, ns):
return nm_tpl


def NamedTuple(typename, fields=None, /, **kwargs):
class NamedTuple(metaclass=NamedTupleMeta):
"""Typed version of namedtuple.

Usage in Python versions >= 3.6::
Expand All @@ -2663,22 +2667,15 @@ class Employee(NamedTuple):

Employee = NamedTuple('Employee', [('name', str), ('id', int)])
"""
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, module=_caller())

_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})

def _namedtuple_mro_entries(bases):
if len(bases) > 1:
raise TypeError("Multiple inheritance with NamedTuple is not supported")
assert bases[0] is NamedTuple
return (_NamedTuple,)

NamedTuple.__mro_entries__ = _namedtuple_mro_entries
_root = True

def __new__(cls, typename, fields=None, /, **kwargs):
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, module=_caller())


class _TypedDictMeta(type):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Multiple inheritance with :class:`typing.NamedTuple` now no longer raises an
error.

Back | FazBrowse Home | New Git URL