Although this certainly depends on how one defines the word "heretic" and the phrase "middle ages", a good case can be made that there is historical precident. Two good articles on the topic are below. The second article provides the best description of the current situation I have read.
In a conference given after his return from the council, Archbishop John Baptist Purcell of Cincinnati related the following:
The question was also raised by a Cardinal, What is to be done with the Pope if he becomes a heretic? It was answered that there has never been such a case; the Council of Bishops could depose him for heresy, for from the moment he becomes a heretic he is not the head or even a member of the Church. The Church would not be, for a moment, obliged to listen to him when he begins to teach a doctrine the Church knows to be a false doctrine, and he would cease to be Pope, being deposed by God Himself.
The examples that are often provided to prove that a pope can be a manifest, public heretic and still retain his office as pope are wrong. Sadly, it is often Catholics who use these examples as proof that such a manifest heretic can still be pope.