Improve inheritance detection logic for generic types.