A lifetime ago I took a political science course. The professor wanted us to look at 20th century politics from “the other person’s” point of view.
One thing he discussed was the Molotov-Ribbentrop Pact. Stalin had been shut out of all previous Allied discussions with Hitler. In particular, the Soviets were not invited to the 1938 Munich conference, even though Czechoslovakia was right on the USSR’s doorstep.
So maybe Stalin thought he was next on Germany’s list. So better to make a deal now.
This does NOT mean Stalin was a good guy, trying to do the right thing. It just gives some insight into his thinking.
By the way, the professor was a fierce anticommunist. Probably none of them left at universities today
Did your professor ask you to look at the Ribbentrop-Molotov Pact from the Poland point of view, too?
BTTT
Stalin was not naïve, he understood that destiny almost certainly meant a clash between fascism and communism. He miscalculated, he thought the Ribbentrop/Molotov pact would precipitate the war but a war between capitalists that would exhaust both sides.
He did not anticipate that Nazi Germany was capable of defeating France and everybody else in 6 weeks. He foresaw a a grinding war of attrition like the First World War that would weaken contending capitalists.
Stalin was to pay for his crime with more than 10 million Russian lives in due time. The cynicism of Russia extends from Czars to commissars and Vladimir Putin.

Right after the signing of the treaty US professors and activists were all gooey eyed over Adolf and Stalin, both. Domestic commies were instructed (yes, instructed — USSR docs came out after the wall fell proving this) to speak nicely about Adolf.
Then, Germany invaded Russia. Oopsie. Now, new instructions were out to domestic commies to attack Nazis at every opportunity.
The point is, domestic political forces were owned by foreign powers.