(Reuters) - The U.S. Constitution’s prohibition against cruel and unusual punishment bars cities from prosecuting the homeless for sleeping outside on public property when they cannot obtain shelter, a federal appeals court ruled on Tuesday.

Reversing a lower court ruling in the Boise, Idaho case, the 9th U.S. Circuit Court of Appeals ruled 2-1 that Robert Anderson and Robert Martin could seek an injunction against enforcement of that city’s “anti-camping” and disorderly conduct ordinances.

Circuit Judge Marsha Berzon said sitting, lying and sleeping on the streets was an “unavoidable consequence” of homelessness, and that it would be an Eighth Amendment violation for cities to punish that conduct when their shelters had too few beds.

“As long as there is no option of sleeping indoors, the government cannot criminalize indigent, homeless people for sleeping outdoors, on public property, on the false premise they had a choice in the matter,” Berzon wrote.

Boise, which is Idaho’s capital, in 2014 barred enforcement of the ordinances when its shelters were full.

But the appeals court said shelters could still turn away homeless people who exceeded their stay limits or refused mandatory religious programs.

Lawyers for the plaintiffs had no immediate comment. The office of Boise City Attorney Robert Luce did not immediately respond to requests for comment.

Tuesday’s decision by the Portland, Oregon-based appeals court overturned a September 2015 ruling by U.S. Magistrate Judge Ronald Bush in Boise, and returned the case to him.

The lawsuit had been brought by Anderson, Martin, Janet Bell, Pamela Hawkes, Basil Humphrey and Lawrence Lee Smith, who were convicted of violating one or both Boise ordinances.

All were sentenced to time served except for Hawkes, who was sentenced twice to one day in jail.

Martin and Hawkes were allowed to pursue damages claims.

Berzon noted that the 11th U.S. Circuit Court of Appeals had in 2000 upheld a similar anti-camping ordinance from Orlando, Florida, against an Eighth Amendment challenge.

But she said that case was different, because Orlando’s shelters had never reached capacity.

Circuit Judge John Owens partially dissented from Tuesday’s decision, saying injunctive relief was not warranted.

The case is Martin et al v City of Boise, 9th U.S. Circuit Court of Appeals, No. 15-35845.