Why is the driver code for different lines (F2, F4, F7, etc) duplicated by ST
If you compare the code from e.g. the STM32CubeF2 and STM32CubeF4 archives you'll see that its almost identical. The only differences seem to be the name of the files and defines (have F2 or F4 in them) and the F4 code containing some additional functions compared to the F2 code.
We work a lot with STM32 microcontrollers at my company, even using multple chips within a single project. We're trying to maintain a single reusable codebase for infrastructure, utilities, etc. Obviously this codebase also contains the STM32Cube code so we can readily start a new project if needed.
The fact that each line within the STM32 family has it's own driver code makes it a difficult job to write (correct) generic code which we can reuse for different types of microcontrollers. This has nothing to do with real differences between those controllers, it's just that headerfiles have different names or defines need to be set differently.