SACRAMENTO—California judges and their spouses should not hold a financial interest in marijuana enterprises, even though medical and recreational cannabis use is legal in California, a state Supreme Court ethics committee said in a formal opinion Wednesday.

Marijuana remains illegal under federal law, the opinion said, and a judge who has a stake in a related business or nonprofit risks violating canons that require compliance with all laws and avoidance of the appearance of impropriety. Financial ties to marijuana, even indirect ones, may also cast doubt on a judge’s ability to act impartially, the committee said.