Congress passed the Hatch Act, or the Act to Prevent Pernicious Political Activities, in 1939.
The court had deemed this as a balance between not restricting speech and making sure that public employees do not over step the boundaries of the office due to political affiliation.