The concept of being "perfect" is not biblical. This is just Christian readers imposing the idea that "imperfect" characters are narrated so they can diagnose the "failings" and see the "sin."
David is perfectly himself. He is a being on a journey. The Bible describes stages of growth, not "morality tales" whose only hero is the judging reader.