PHILADELPHIA — Bill Cosby’s attorney is asking a judge to throw out the rape case against him because of a supposed agreement to give him immunity in exchange for his testimony in a civil lawsuit by Andrea Constand, the accuser.

At a hearing Tuesday, attorney Brian McMonagle will argue that Montgomery County’s district attorney is “playing ‘gotcha” with Cosby’s rights, as he put it in a motion last month. The argument is that Cosby was told by a previous D.A. that potentially self-incriminating testimony would not be used to charge him as it is now. Steele will counter that Cosby is asking for the celebrity treatment in expecting the court to honor what amounts to a gentleman’s agreement.

Both attorneys will get to question former district attorney Bruce Castor, who declined to charge Cosby in 2005 and today says there was at least a reassurance that Cosby would be safe from self-incrimination.

Yet there’s no clear evidence of such an agreement. In fact, the only thing the defense or Castor have pointed to was a 2005 press release that expressly reserved the possibility of future prosecution.

Castor would “reconsider this decision should the need arise,” he wrote, adding a caution to both parties against speaking publicly about the case. “Much exists in this investigation that could be used (by others) to portray persons on both sides of the issue in a less than flattering light.”

Steele argued in a motion that it was not in Castor’s power to “essentially grant immunity via a press release by agreeing not to prosecute,” because Pennsylvania law reserves the power to grant immunity solely to judges. Steele also notes that Castor’s own press release leaves room to reconsider charges.

Last September, Castor was quoted by the Bucks County Intelligencer as saying he had “signed off” on a “written declaration” that his office would not use Cosby’s deposition where he admitted giving women drugs before sex to criminally charge him.

On that same day, District Attorney Rose Vetri Ferman, who reopened the investigation in July 2015 based in part on Cosby’s civil deposition, sent a courier to Castor with a formal request for a copy of that “written declaration.” Castor told Ferman that he had been referring to the press release, according to Steele’s response to the defense motion to dismiss.

Constand’s lawyer Dolores Troiani says her client was neither consulted nor notified of any non-prosecution agreement.

“He said that he talked to us about it. That’s a lie. It never happened.”

Cosby’s lawyer may also argue that the delay in proceedings deprives Cosby of his right to due process. McMonagle said in his motion that the D.A.’s office lost key evidence of the non-prosecution agreement by waiting 12 years to bring charges. That’s because Cosby’s longtime attorney, Walter M. Phillips Jr., died in 2015.

If the Cosby case withstands this first test, the defense has further claimed that the judge should at least disqualify Steele’s office from prosecuting a case which Steele politicized while seeking office. According to McMonagle, Steele must be removed immediately in order for his prejudice not to taint every proceeding in which he is involved, including the hearing on this motion itself.

Should the Montgomery County district attorney be disqualified, Pennsylvania’s attorney general could still prosecute Cosby. That scenario gets even stranger given that Attorney General Kathleen Kane is being prosecuted by Steele’s office for allegedly leaking confidential grand jury info and lying about it.