@@ -1837,16 +1837,62 @@ TEST(lrp_tsx_usage_import_type_with_decoy) {
18371837 PASS ();
18381838}
18391839
1840- /* Python: constructor syntax is an exact call to the materialized Class node. */
1840+ /* Python: ClassName() with a user __init__ is an exact CALLS edge to that
1841+ * method. The Class node no longer receives the instantiation edge. */
18411842TEST (lrp_python_usage_instantiation ) {
18421843 static const LRP_File f [] = {
18431844 {"model.py" , "class User:\n def __init__(self, name):\n self.name = name\n" },
18441845 {"main.py" , "from .model import User\n\n\ndef create(name):\n return User(name)\n" }};
18451846 LRP_Proj lp ;
18461847 cbm_store_t * store = lrp_index (& lp , f , 2 );
1847- int exact = store ? lrp_exact_calls_by_name (store , lp .project , "create" , "User" ) : -1 ;
1848+ int exact = store ? lrp_exact_calls_by_name (store , lp .project , "create" , "__init__" ) : -1 ;
1849+ int class_edge = store ? lrp_exact_calls_by_name (store , lp .project , "create" , "User" ) : -1 ;
18481850 lrp_cleanup (& lp , store );
18491851 ASSERT_EQ (exact , 1 );
1852+ ASSERT_EQ (class_edge , 0 );
1853+ PASS ();
1854+ }
1855+
1856+ /* Python same-file subclasses in the graph: ClassName() binds only to an
1857+ * __init__ the class declares itself. B(A) declares none, so make_b keeps
1858+ * its CALLS edge on the class B and the inherited A.__init__ gets none.
1859+ * C(A) declares its own __init__, so make_c calls C.__init__, not C and not
1860+ * the inherited A.__init__. */
1861+ TEST (lrp_python_subclass_constructor_own_init_only ) {
1862+ static const LRP_File f [] = {{"main.py" , "class A:\n"
1863+ " def __init__(self):\n"
1864+ " self.ready = True\n"
1865+ "\n"
1866+ "class B(A):\n"
1867+ " def go(self):\n"
1868+ " return 1\n"
1869+ "\n"
1870+ "class C(A):\n"
1871+ " def __init__(self):\n"
1872+ " self.ready = False\n"
1873+ "\n"
1874+ "def make_b():\n"
1875+ " return B()\n"
1876+ "\n"
1877+ "def make_c():\n"
1878+ " return C()\n" }};
1879+ LRP_Proj lp ;
1880+ cbm_store_t * store = lrp_index (& lp , f , 1 );
1881+ const char * p = store ? lp .project : NULL ;
1882+ int b_class = lrp_exact_edge_by_qn_suffix (store , p , "CALLS" , "main.make_b" , "main.B" );
1883+ int b_base_init =
1884+ lrp_exact_edge_by_qn_suffix (store , p , "CALLS" , "main.make_b" , "main.A.__init__" );
1885+ int c_own_init =
1886+ lrp_exact_edge_by_qn_suffix (store , p , "CALLS" , "main.make_c" , "main.C.__init__" );
1887+ int c_class = lrp_exact_edge_by_qn_suffix (store , p , "CALLS" , "main.make_c" , "main.C" );
1888+ int c_base_init =
1889+ lrp_exact_edge_by_qn_suffix (store , p , "CALLS" , "main.make_c" , "main.A.__init__" );
1890+ lrp_cleanup (& lp , store );
1891+ ASSERT_EQ (b_class , 1 );
1892+ ASSERT_EQ (b_base_init , 0 );
1893+ ASSERT_EQ (c_own_init , 1 );
1894+ ASSERT_EQ (c_class , 0 );
1895+ ASSERT_EQ (c_base_init , 0 );
18501896 PASS ();
18511897}
18521898
@@ -1967,4 +2013,5 @@ SUITE(lsp_resolution_probe) {
19672013 RUN_TEST (lrp_ts_usage_import_type_with_decoy );
19682014 RUN_TEST (lrp_tsx_usage_import_type_with_decoy );
19692015 RUN_TEST (lrp_python_usage_instantiation );
2016+ RUN_TEST (lrp_python_subclass_constructor_own_init_only );
19702017}
0 commit comments