@@ -691,8 +691,13 @@ def test_special_dunders(self) -> Iterator[Case]:
691
691
)
692
692
if sys .version_info >= (3 , 6 ):
693
693
yield Case (
694
- stub = "class C:\n def __init_subclass__(cls, e: int, **kwargs: int) -> None: ..." ,
695
- runtime = "class C:\n def __init_subclass__(cls, e, **kwargs): pass" ,
694
+ stub = (
695
+ "class C:\n "
696
+ " def __init_subclass__(\n "
697
+ " cls, e: int = ..., **kwargs: int\n "
698
+ " ) -> None: ...\n "
699
+ ),
700
+ runtime = "class C:\n def __init_subclass__(cls, e=1, **kwargs): pass" ,
696
701
error = None ,
697
702
)
698
703
if sys .version_info >= (3 , 9 ):
@@ -702,6 +707,28 @@ def test_special_dunders(self) -> Iterator[Case]:
702
707
error = None ,
703
708
)
704
709
710
+ def test_not_subclassable (self ) -> None :
711
+ output = run_stubtest (
712
+ stub = (
713
+ "class CanBeSubclassed: ...\n "
714
+ "class CanNotBeSubclassed:\n "
715
+ " def __init_subclass__(cls) -> None: ...\n "
716
+ ),
717
+ runtime = (
718
+ "class CanNotBeSubclassed:\n "
719
+ " def __init_subclass__(cls): raise TypeError('nope')\n "
720
+ # ctypes.Array can be subclassed, but subclasses must define a few
721
+ # special attributes, e.g. _length_
722
+ "from ctypes import Array as CanBeSubclassed\n "
723
+ ),
724
+ options = [],
725
+ )
726
+ assert (
727
+ "CanNotBeSubclassed cannot be subclassed at runtime,"
728
+ " but isn't marked with @final in the stub"
729
+ ) in output
730
+ assert "CanBeSubclassed cannot be subclassed" not in output
731
+
705
732
@collect_cases
706
733
def test_name_mangling (self ) -> Iterator [Case ]:
707
734
yield Case (
0 commit comments