@@ -397,7 +397,10 @@ impl<'a> SemanticModel<'a> {
397
397
//
398
398
// The `name` in `print(name)` should be treated as unresolved, but the `name` in
399
399
// `name: str` should be treated as used.
400
- BindingKind :: Annotation => continue ,
400
+ //
401
+ // Stub files are an exception. In a stub file, it _is_ considered valid to
402
+ // resolve to a type annotation.
403
+ BindingKind :: Annotation if !self . in_stub_file ( ) => continue ,
401
404
402
405
// If it's a deletion, don't treat it as resolved, since the name is now
403
406
// unbound. For example, given:
@@ -1570,6 +1573,11 @@ impl<'a> SemanticModel<'a> {
1570
1573
. intersects ( SemanticModelFlags :: FUTURE_ANNOTATIONS )
1571
1574
}
1572
1575
1576
+ /// Return `true` if the model is in a stub file (i.e., a file with a `.pyi` extension).
1577
+ pub const fn in_stub_file ( & self ) -> bool {
1578
+ self . flags . intersects ( SemanticModelFlags :: STUB_FILE )
1579
+ }
1580
+
1573
1581
/// Return `true` if the model is in a named expression assignment (e.g., `x := 1`).
1574
1582
pub const fn in_named_expression_assignment ( & self ) -> bool {
1575
1583
self . flags
@@ -1675,7 +1683,7 @@ bitflags! {
1675
1683
/// Flags indicating the current model state.
1676
1684
#[ derive( Debug , Default , Copy , Clone , Eq , PartialEq ) ]
1677
1685
pub struct SemanticModelFlags : u32 {
1678
- /// The model is in a type annotation that will only be evaluated when running a type
1686
+ /// The model is in a type annotation that will only be evaluated when running a type
1679
1687
/// checker.
1680
1688
///
1681
1689
/// For example, the model could be visiting `int` in:
@@ -1875,6 +1883,9 @@ bitflags! {
1875
1883
/// ```
1876
1884
const FUTURE_ANNOTATIONS = 1 << 15 ;
1877
1885
1886
+ /// The model is in a Python stub file (i.e., a `.pyi` file).
1887
+ const STUB_FILE = 1 << 16 ;
1888
+
1878
1889
/// The model has traversed past the module docstring.
1879
1890
///
1880
1891
/// For example, the model could be visiting `x` in:
@@ -1883,7 +1894,7 @@ bitflags! {
1883
1894
///
1884
1895
/// x: int = 1
1885
1896
/// ```
1886
- const MODULE_DOCSTRING_BOUNDARY = 1 << 16 ;
1897
+ const MODULE_DOCSTRING_BOUNDARY = 1 << 17 ;
1887
1898
1888
1899
/// The model is in a type parameter definition.
1889
1900
///
@@ -1893,23 +1904,23 @@ bitflags! {
1893
1904
///
1894
1905
/// Record = TypeVar("Record")
1895
1906
///
1896
- const TYPE_PARAM_DEFINITION = 1 << 17 ;
1907
+ const TYPE_PARAM_DEFINITION = 1 << 18 ;
1897
1908
1898
1909
/// The model is in a named expression assignment.
1899
1910
///
1900
1911
/// For example, the model could be visiting `x` in:
1901
1912
/// ```python
1902
1913
/// if (x := 1): ...
1903
1914
/// ```
1904
- const NAMED_EXPRESSION_ASSIGNMENT = 1 << 18 ;
1915
+ const NAMED_EXPRESSION_ASSIGNMENT = 1 << 19 ;
1905
1916
1906
1917
/// The model is in a comprehension variable assignment.
1907
1918
///
1908
1919
/// For example, the model could be visiting `x` in:
1909
1920
/// ```python
1910
1921
/// [_ for x in range(10)]
1911
1922
/// ```
1912
- const COMPREHENSION_ASSIGNMENT = 1 << 19 ;
1923
+ const COMPREHENSION_ASSIGNMENT = 1 << 20 ;
1913
1924
1914
1925
/// The model is in a module / class / function docstring.
1915
1926
///
@@ -1928,7 +1939,7 @@ bitflags! {
1928
1939
/// """Function docstring."""
1929
1940
/// pass
1930
1941
/// ```
1931
- const DOCSTRING = 1 << 20 ;
1942
+ const DOCSTRING = 1 << 21 ;
1932
1943
1933
1944
/// The context is in any type annotation.
1934
1945
const ANNOTATION = Self :: TYPING_ONLY_ANNOTATION . bits( ) | Self :: RUNTIME_EVALUATED_ANNOTATION . bits( ) | Self :: RUNTIME_REQUIRED_ANNOTATION . bits( ) ;
@@ -1953,6 +1964,7 @@ impl SemanticModelFlags {
1953
1964
pub fn new ( path : & Path ) -> Self {
1954
1965
let mut flags = Self :: default ( ) ;
1955
1966
if is_python_stub_file ( path) {
1967
+ flags |= Self :: STUB_FILE ;
1956
1968
flags |= Self :: FUTURE_ANNOTATIONS ;
1957
1969
}
1958
1970
flags
0 commit comments