@@ -178,7 +178,7 @@ impl<'a> SemanticModel<'a> {
178178 pub fn resolve_reference ( & mut self , symbol : & str , range : TextRange ) -> ResolvedReference {
179179 // PEP 563 indicates that if a forward reference can be resolved in the module scope, we
180180 // should prefer it over local resolutions.
181- if self . in_deferred_type_definition ( ) {
181+ if self . in_forward_reference ( ) {
182182 if let Some ( binding_id) = self . scopes . global ( ) . get ( symbol) {
183183 // Mark the binding as used.
184184 let context = self . execution_context ( ) ;
@@ -239,9 +239,7 @@ impl<'a> SemanticModel<'a> {
239239 //
240240 // The `name` in `print(name)` should be treated as unresolved, but the `name` in
241241 // `name: str` should be treated as used.
242- if !self . in_deferred_type_definition ( )
243- && self . bindings [ binding_id] . kind . is_annotation ( )
244- {
242+ if !self . in_forward_reference ( ) && self . bindings [ binding_id] . kind . is_annotation ( ) {
245243 continue ;
246244 }
247245
@@ -756,6 +754,27 @@ impl<'a> SemanticModel<'a> {
756754 || self . in_future_type_definition ( )
757755 }
758756
757+ /// Return `true` if the context is in a forward type reference.
758+ ///
759+ /// Includes deferred string types, and future types in annotations.
760+ ///
761+ /// ## Examples
762+ /// ```python
763+ /// from __future__ import annotations
764+ ///
765+ /// from threading import Thread
766+ ///
767+ ///
768+ /// x: Thread # Forward reference
769+ /// cast("Thread", x) # Forward reference
770+ /// cast(Thread, x) # Non-forward reference
771+ /// ```
772+ pub const fn in_forward_reference ( & self ) -> bool {
773+ self . in_simple_string_type_definition ( )
774+ || self . in_complex_string_type_definition ( )
775+ || ( self . in_future_type_definition ( ) && self . in_annotation ( ) )
776+ }
777+
759778 /// Return `true` if the context is in an exception handler.
760779 pub const fn in_exception_handler ( & self ) -> bool {
761780 self . flags . contains ( SemanticModelFlags :: EXCEPTION_HANDLER )
0 commit comments