It is OK to wear shorts in Italy. However, some religious buildings, such as St Peter's Basilica at the Vatican, have a strict dress code and will usually not admit visitors wearing shorts. Wear long pants that day or take them with you and change beforehand. Sometimes it can depend on the official on duty, but you don't want to risk getting turned away having come all that way.