A federal judge ruled on Thursday that the Trump administration cannot bar healthcare providers receiving certain federal HIV-treatment funds from acknowledging transgender patients’ gender identity or providing gender-affirming care.

US district judge William Young in Boston ruled for national HIV medical associations and healthcare providers who challenged grant conditions imposed last year after Trump signed an executive order prohibiting federal funding for what he calls “gender ideology”.

The conditions applied to funding from the Ryan White HIV/Aids Program, created by Congress in 1990 to provide medical care for low-income and uninsured people living with HIV. They were adopted by a division of the US Department of Health and Human Services.

Young said the restrictions sought to suppress what the administration considered “dangerous ideas”, violating the constitution’s first amendment by penalizing doctors for acknowledging transgender people and using their preferred pronouns. He also said the restrictions constituted unconstitutional discrimination on the basis of sex, in violation of the fifth amendment.

“They are based in animus, loathing, and discrimination,” Young wrote. “They do not stand for justice – in fact, they defy it. They are contrary to the laws of this nation and cannot continue to stand.”

Young, appointed by Republican president Ronald Reagan, issued the 165-page ruling after a non-jury trial in September. HHS and lawyers for the plaintiffs, including the American Academy of HIV Medicine, did not immediately respond to requests for comment.

Beginning in March, the Health Resources and Services Administration, which administers the Ryan White program, added conditions intended to implement Trump’s executive order directing the US government to recognize only two sexes, male and female.

The lawsuit said the grant conditions required providers to ensure funding supported activities using “sex-based definitions grounded in biological reality” and not “social or medical experimentation”. The plaintiffs argued the policy was unconstitutional and conflicted with the program’s mandate to provide comprehensive HIV medical services, which has historically included hormone therapy for transgender people.