@@ -1275,3 +1275,109 @@ def external_dep2():
12751275 assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
12761276 assert file3 .content .strip () == EXPECTED_FILE_3_CONTENT .strip ()
12771277 assert file4 .content .strip () == EXPECTED_FILE_4_CONTENT .strip ()
1278+
1279+
1280+ def test_move_to_file_with_dataclass_dependencies (tmpdir ) -> None :
1281+ # ========== [ BEFORE ] ==========
1282+ # language=python
1283+ FILE_1_CONTENT = """
1284+ from dataclasses import dataclass
1285+
1286+ @dataclass
1287+ class Config:
1288+ '''Base config class'''
1289+ name: str
1290+ value: int
1291+
1292+ def foo():
1293+ return 1
1294+ """
1295+
1296+ # language=python
1297+ FILE_2_CONTENT = """
1298+ from dataclasses import dataclass
1299+ from file1 import Config
1300+
1301+ @dataclass
1302+ class ExtendedConfig(Config):
1303+ '''Extended config that depends on Config'''
1304+ extra: str = "default"
1305+
1306+ def bar(config: ExtendedConfig):
1307+ '''Function that uses the dataclass'''
1308+ return config.value + 1
1309+ """
1310+
1311+ # ========== [ AFTER ] ==========
1312+ # language=python
1313+ EXPECTED_FILE_1_CONTENT = """
1314+ from dataclasses import dataclass
1315+
1316+ def foo():
1317+ return 1
1318+ """
1319+
1320+ # language=python
1321+ EXPECTED_FILE_1_TYPES_CONTENT = """
1322+ from dataclasses import dataclass
1323+
1324+
1325+ @dataclass
1326+ class Config:
1327+ '''Base config class'''
1328+ name: str
1329+ value: int
1330+ """
1331+
1332+ # language=python
1333+ EXPECTED_FILE_2_CONTENT = """
1334+ from file2.types import ExtendedConfig
1335+ from file1.types import Config
1336+ from dataclasses import dataclass
1337+
1338+ def bar(config: ExtendedConfig):
1339+ '''Function that uses the dataclass'''
1340+ return config.value + 1
1341+ """
1342+
1343+ # language=python
1344+ EXPECTED_FILE_2_TYPES_CONTENT = """
1345+ from file1.types import Config
1346+ from dataclasses import dataclass
1347+
1348+
1349+ @dataclass
1350+ class ExtendedConfig(Config):
1351+ '''Extended config that depends on Config'''
1352+ extra: str = "default"
1353+ """
1354+
1355+ # ===============================
1356+
1357+ with get_codebase_session (
1358+ tmpdir = tmpdir ,
1359+ files = {
1360+ "file1.py" : FILE_1_CONTENT ,
1361+ "file2.py" : FILE_2_CONTENT ,
1362+ },
1363+ ) as codebase :
1364+ file1 = codebase .get_file ("file1.py" )
1365+ file2 = codebase .get_file ("file2.py" )
1366+
1367+ # Create types.py files
1368+ file1_types = codebase .create_file ("file1/types.py" , "" )
1369+ file2_types = codebase .create_file ("file2/types.py" , "" )
1370+
1371+ # Move Config dataclass first since ExtendedConfig depends on it
1372+ config_class = file1 .get_class ("Config" )
1373+ config_class .move_to_file (file1_types , strategy = "update_all_imports" , include_dependencies = True )
1374+ codebase .commit ()
1375+
1376+ # Then move ExtendedConfig
1377+ extended_config_class = file2 .get_class ("ExtendedConfig" )
1378+ extended_config_class .move_to_file (file2_types , strategy = "update_all_imports" , include_dependencies = True )
1379+
1380+ assert file1 .content .strip () == EXPECTED_FILE_1_CONTENT .strip ()
1381+ assert file1_types .content .strip () == EXPECTED_FILE_1_TYPES_CONTENT .strip ()
1382+ assert file2 .content .strip () == EXPECTED_FILE_2_CONTENT .strip ()
1383+ assert file2_types .content .strip () == EXPECTED_FILE_2_TYPES_CONTENT .strip ()
0 commit comments