Commit dcc9641
committed
[mypyc] Preserve inherited attribute defaults under separate=True
Under `separate=True`, when a subclass is recompiled while its parent
is loaded from mypy's incremental cache, parent default-attribute
assignments are silently dropped from the subclass's
`__mypyc_defaults_setup`. The first read of an inherited default-attr
then raises:
AttributeError: attribute '<name>' of '<Parent>' undefined
`find_attr_initializers` walks `cdef.info.mro` and reads
`info.defn.defs.body` for `AssignmentStmt`s. `ClassDef.serialize`
(mypy/nodes.py) does not serialize `defs`, so a cache-loaded parent
has `defs = Block([])`; the MRO walk collects no parent assignments
and the subclass's emitted setup leaves inherited slots in the
undefined-sentinel state.
Fix: under `separate=True`, scope `find_attr_initializers` to the
subclass's own body and have `generate_attr_defaults_init` emit a
chained call to the nearest ancestor with `__mypyc_defaults_setup`
before setting own defaults. Each class's setup is responsible only
for its own attributes; the chain runs ancestors first, mirroring
the `__init__` chain. The ancestor's return value is propagated so
a parent default that raised still aborts instance creation. Without
`separate=True`, the MRO AST walk is unaffected (all modules parsed
in the same pass), preserving the existing inline-all behavior.
Adds `testIncrementalCrossModuleInheritedAttrDefaultsWithOverride`
to `run-multimodule.test`, which triggers the cache-load path: clean
build of a two-module Parent/Child hierarchy, then a `.2` revision of
the subclass module to force an incremental rebuild while the parent
is served from the cache. The child overrides one inherited default
so it emits its own `__mypyc_defaults_setup` (the case the chain
composition is required for). The test fails on master without this
patch.1 parent 938dbe2 commit dcc9641
2 files changed
Lines changed: 106 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
745 | 746 | | |
746 | 747 | | |
747 | 748 | | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
748 | 763 | | |
749 | 764 | | |
750 | 765 | | |
| |||
758 | 773 | | |
759 | 774 | | |
760 | 775 | | |
761 | | - | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
762 | 782 | | |
763 | 783 | | |
764 | 784 | | |
| |||
800 | 820 | | |
801 | 821 | | |
802 | 822 | | |
803 | | - | |
804 | | - | |
805 | | - | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
806 | 832 | | |
807 | 833 | | |
808 | 834 | | |
809 | 835 | | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
810 | 846 | | |
811 | 847 | | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
812 | 861 | | |
813 | 862 | | |
814 | 863 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1778 | 1778 | | |
1779 | 1779 | | |
1780 | 1780 | | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
0 commit comments