David Koch serves as chairman of the Americans for Prosperity Foundation, which will not have to turn over its donor list to the California attorney general. (Chip Somodevilla/Getty Images)

A charitable group backed by the Koch brothers has won the right to withhold a list of its donors from the California attorney general in a case that could test the ability of government agencies to compel nonprofits to disclose information about their supporters.

In his Thursday ruling, U.S. District Judge Manuel L. Real wrote that forcing the Americans for Prosperity Foundation to submit a list of its contributors to a state registry would chill its donors’ rights to anonymous speech, even though the information is supposed to be kept confidential.

Real’s decision did not invalidate the state rule requiring nonprofit groups to submit donor lists, which has been upheld by the U.S. Court of Appeals for the 9th Circuit in a separate case. But he wrote that it was unconstitutional as applied to the AFP Foundation, noting that the organization’s officials and donors — including billionaire conservative benefactors Charles and David Koch and their families — have been subject to harassment and death threats.

The possibility that the foundation’s filing with the state could be made public “places donors in fear of exercising their First Amendment right to support AFP’s expressive activity,” Real wrote.

California Attorney General Kamala Harris plans to appeal the ruling to the 9th Circuit, and legal experts say the case could eventually reach the Supreme Court.

“We are disappointed,” Harris spokesman Kristin Ford said in a statement, adding that the donor list that nonprofits must file with the office “is a long-standing requirement that has helped Attorneys General for more than a decade protect taxpayers against fraud.”

Mark Holden, general counsel for Koch Industries, called the decision a victory for First Amendment rights.

“It reaffirms what I’ve always believed, that there is a right to anonymous free speech and free association and that officials like Attorney General Harris don’t have the right to demand this information absent a compelling interest,” he said.

The ongoing legal fight could end up affecting states that have sought more information about the backers of politically active nonprofits, which have proliferated since the Supreme Court’s Citizens United decision in 2010.

[How ‘ghost corporations’ are funding the 2016 election]

Supporters of more-robust disclosure argue it is needed to give the public information about who is trying to influence campaigns. Conservatives have resisted such efforts, saying that donors to nonprofit groups often fear retaliation.

“I think this has fairly large implications for the ability of states to keep expanding disclosure laws,” said Bradley Smith, chairman of the Center for Competitive Politics, which has challenged the constitutionality of California’s rule.

He’s hopeful that the 9th Circuit may now reconsider his organization’s case.

“The big holding here is you’ve got to show a real legislative reason for wanting this,” Smith said. “It really does go to this idea of, how much does privacy matter? Is it enough that government wants the information and might have some need for it for some indeterminate reason in the future?”

Erwin Chemerinsky, a constitutional expert and dean of the University of California at Irvine School of Law, said the case may end up before the Supreme Court. He noted that while the First Amendment allows for anonymous speech, “it’s not an absolute right.”

“I think the district court underestimated the importance of the government having this information and overestimated the harm,” Chemerinsky said.

The AFP Foundation had filed its annual paperwork with the state of California for nearly a decade without providing its donor list. But in 2013, the attorney general’s office told the group that its filing was incomplete because it did not include an unredacted copy of its Schedule B, a list of all of its donors who gave more than $5,000 the previous year.

Holden said the organization believed the move was political, aimed at the Kochs because of their support for conservative causes.

“We felt we were being singled out and targeted,” he said.

[Americans for Prosperity plows millions into a conservative ground force]

State officials rejected that, saying the rule applies to thousands of charities that register in California. But Real concluded that the attorney general could not show how Schedule B forms assist in its investigations.

The judge also pointedly chided Harris for her office’s handling of the confidential documents. He noted that AFP found 1,400 Schedule Bs publicly available on the attorney general’s website, including a list of donors to Planned Parenthood.

“While human error can sometimes be unavoidable, the amount of careless mistakes made by the Attorney General’s Registry is shocking,” he wrote.

Real also said there was “ample evidence” that AFP’s employees and donors have been subject to threats and intimidation. A security contractor working inside AFP’s headquarters in 2013 posted online that he was “inside the belly of the beast” and could walk into the office of chief executive Lucas Hilgemann and slit his throat, Hilgemann testified. North Carolina retail executive Art Pope, a major donor to the group, told the court that he considering halting his funding for the groups after his stores were picketed and boycotted. And Holden testified that there had been death threats made against both the Kochs, as well as their grandchildren.