From 8e7d55a77f1d1912db655f4e8603a95310189712 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 30 Jun 2021 12:11:24 +0100 Subject: [PATCH] [mypyc] Fix class-based named tuples These only worked with the simplified stubs used in tests. --- mypyc/irbuild/classdef.py | 6 +++++- mypyc/test-data/commandline.test | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 5c478a012c50e..12b8bba894541 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -219,7 +219,11 @@ def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value: for cls in cdef.info.mro[1:]: if cls.fullname == 'builtins.object': continue - if is_named_tuple and cls.fullname in ('typing.Sequence', 'typing.Iterable'): + if is_named_tuple and cls.fullname in ('typing.Sequence', + 'typing.Iterable', + 'typing.Collection', + 'typing.Reversible', + 'typing.Container'): # HAX: Synthesized base classes added by mypy don't exist at runtime, so skip them. # This could break if they were added explicitly, though... continue diff --git a/mypyc/test-data/commandline.test b/mypyc/test-data/commandline.test index 6d2d1539aeadf..0485a90fe66c9 100644 --- a/mypyc/test-data/commandline.test +++ b/mypyc/test-data/commandline.test @@ -24,6 +24,7 @@ for x in [a, b, p, p.q]: import b import c from p import s +from typing import NamedTuple print('', ord('A') == 65) # Test full builtins @@ -42,6 +43,11 @@ print('', f(5).x) print('', c.foo()) assert s.bar(10) == 20 +class NT(NamedTuple): + x: int + +print(NT(2)) + [file b.py] import a import p.q @@ -79,6 +85,7 @@ def bar(x: int) -> int: True 5 10 +NT(x=2)
16 -- This test is here so we can turn it on when we get nervous about