Any shape can be used, but optimal shaped spaceships are more likely to be used than not if engineers in verse care about expense and effort.
Sphere: Best at holding air in. No pressure cracks that would occur if you had air pressure inside a blocky shaped ship. Really any streamlining is better than a hard edge when it comes to holding pressure. In that sense, spaceships are really like glorified balloons that house crew that need to live in the balloon.
Saucer: It is good for spin gravity. Although you could also do the same with a spaceship shaped like the letter H. Only with a LONG bridge between the sides.
Blocky: Pressure cracks at corners will stress hull sooner or later if you put air pressure within the blocky hull. If you ony put air pressure in spherical or cylinderal roons inside the blocky ship you avoid that problem. Thus blocky ships would be great for drones or robot ships that do not need air to breath and thus won't have lots of air inside.
Cylinder: You get most of the benefits of the sphere plus it is better for bulk storage of plenty things.
Inside the ship: Although the captain and chairs on the bridge is popular, spanning many imitators, it is is really a sub-optimal design.
They could likely save the energy they put into gravity generators by simply accelerating up instead of sideways. I know weightlessness is bad, and for times the ship is not accelerating the saucer is optimal for centrifuge spinning for 1g. It would be cheaper even if gravity generators were possible, since anything we already have a. grasp of is not considered very advanced in a scifi setting and should be thus easier to come by if advanced tech like gravity generators are available.
Ship purpose: What a spaceship is made to do also has a bearing on optimal designs. Astetoid mining ships, if they do any processing of their own, practically beg for a shape that will allow spin gravity. Since processing and refining ore is easier with gravity than without.
For ships that are SSTO's, compact designs have less drag, but if fuel is not a concern because the setting has super tech propulsion, suboptimal designs would have other reasons for existing.
Sure the ship creates more drag than it needs to, but if it has antigravity to fall up, then it could reach space in about 2 min just falling upward from earth at 1g. Which is a huge improvement. The ship would still need to use some other propulsion to maintain an orbit, but at least there would be little to no air resistance in LEO, thus increasing fuel efficiency of rockets. And that is if rockets are even used as a main source of sublight propulsion in a scifi setting. They certainly do not have to be, as the only limitations are the ones a writer imposes on their work and self.
Personally, I love saucer spaceship design, because antigravity makes it at least practical, and while it may never be as optimal as a cylinder or a sphere for holding pressure, it is optimal for spin gravity at least.