There's actually another issue with ForwardRefs. They don't work in the REPL. So this will work when run as a module:

  def foo() -> "Bar":
     return Bar()
But will throw an error if copy pasted into a REPL.

However, all of these issues should be fixed in 3.14 with PEP649 and PEP749:

> At compile time, if the definition of an object includes annotations, the Python compiler will write the expressions computing the annotations into its own function. When run, the function will return the annotations dict. The Python compiler then stores a reference to this function in __annotate__ on the object.

> This mechanism delays the evaluation of annotations expressions until the annotations are examined, which solves many circular reference problems.

It doesn't throw error in the REPL though. Surely you meant to share some other example?

Please ignore my first assertion that the behavior between REPL and module is different.

This would have been the case if the semantics of the original PEP649 spec had been implemented. But instead, PEP749 ensures that it is not [0]. My bad.

[0] https://peps.python.org/pep-0749/#behavior-of-the-repl