BOISE, Idaho — Cities can't prosecute people for sleeping on the streets if they have nowhere else to go because it amounts to cruel and unusual punishment, a federal appeals court said Tuesday.

The 9th U.S. Circuit Court of Appeals sided with six homeless Boise, Idaho, residents who sued the city in 2009 over a local ordinance that banned sleeping in public spaces. The ruling could affect several other cities across the U.S. West that have similar laws.

It comes as many places across the West Coast are struggling with homelessness brought on by rising housing costs and income inequality.

When the Boise lawsuit was filed, attorneys for the homeless residents said as many as 4,500 people didn't have a place to sleep in Idaho's capital city and only about 700 available beds or mats were in homeless shelters. The case bounced back and forth in the courts for years, and Boise modified its rules in 2014 to say homeless residents couldn't be prosecuted for sleeping outside on nights when the local shelters were full.

But that didn't solve the problem, the attorneys said, because Boise's shelters limit the number of days that homeless residents can stay. Two of the city's three shelters require some form of religious participation for some programs, making those shelters unsuitable for people with different beliefs, the homeless residents said.

The three-judge panel for the 9th Circuit found that the shelter rules meant homeless people would still be at risk of prosecution even on days when beds were left open. The judges also said the religious programming woven into some shelter programs was a problem.

"A city cannot, via the threat of prosecution, coerce an individual to attend religion-based treatment programs consistently with the Establishment Clause of the First Amendment," Judge Marsha Berzon wrote.

The biggest issue was that the city's rule violated the U.S. Constitution's Eighth Amendment against cruel and unusual punishment, the court found.

Berzon wrote that in essence, the Eighth Amendment does three things: It limits the type of punishment the government can impose, it prohibits punishments that are grossly disproportionate to the severity of the crime, and it limits what the government can criminalize.

That third limitation is pertinent in the Boise case, Berzon wrote.

"As a result, just as the state may not criminalize the state of being 'homeless in public places,' the state may not 'criminalize conduct that is an unavoidable consequence of being homeless — namely sitting, lying, or sleeping on the streets,'" Berzon wrote.

In other words, as long as there isn't an option for sleeping indoors, the government can't criminalize homeless people for sleeping outside, the court found.

Neither attorneys for the homeless residents nor for the city immediately returned calls seeking comment.

Other cities have faced similar lawsuits, with varying results.

In 2007, the 9th Circuit ruled in favor of homeless residents of Los Angeles, finding that as long as there are more homeless residents than there are shelter beds, a law outlawing sleeping outside was unconstitutional. Both sides later reached an agreement and the entire case was eventually thrown out.

In 2009, a federal judge said a Portland policy designed to prevent people from sitting or lying on public sidewalks was unconstitutional. Portland officials now must also give campers at least 24 hours' notice before cleaning up or moving unsanctioned camps.

A state judge rejected a similar anti-camping law in Everett, Washington.

--The Associated Press