@@ -2752,7 +2752,8 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
2752
2752
2753
2753
pep_613 = False
2754
2754
if s .unanalyzed_type is not None and isinstance (s .unanalyzed_type , UnboundType ):
2755
- lookup = self .lookup_qualified (s .unanalyzed_type .name , s , suppress_errors = True )
2755
+ lookup = self .lookup_qualified (s .unanalyzed_type .name , s , suppress_errors = True ,
2756
+ annotation = True )
2756
2757
if lookup and lookup .fullname in TYPE_ALIAS_NAMES :
2757
2758
pep_613 = True
2758
2759
if not pep_613 and s .unanalyzed_type is not None :
@@ -3533,15 +3534,15 @@ def check_classvar(self, s: AssignmentStmt) -> None:
3533
3534
def is_classvar (self , typ : Type ) -> bool :
3534
3535
if not isinstance (typ , UnboundType ):
3535
3536
return False
3536
- sym = self .lookup_qualified (typ .name , typ )
3537
+ sym = self .lookup_qualified (typ .name , typ , annotation = True )
3537
3538
if not sym or not sym .node :
3538
3539
return False
3539
3540
return sym .node .fullname == 'typing.ClassVar'
3540
3541
3541
3542
def is_final_type (self , typ : Optional [Type ]) -> bool :
3542
3543
if not isinstance (typ , UnboundType ):
3543
3544
return False
3544
- sym = self .lookup_qualified (typ .name , typ )
3545
+ sym = self .lookup_qualified (typ .name , typ , annotation = True )
3545
3546
if not sym or not sym .node :
3546
3547
return False
3547
3548
return sym .node .fullname in FINAL_TYPE_NAMES
@@ -4512,7 +4513,8 @@ def visit_class_pattern(self, p: ClassPattern) -> None:
4512
4513
#
4513
4514
4514
4515
def lookup (self , name : str , ctx : Context ,
4515
- suppress_errors : bool = False ) -> Optional [SymbolTableNode ]:
4516
+ suppress_errors : bool = False ,
4517
+ annotation : bool = False ) -> Optional [SymbolTableNode ]:
4516
4518
"""Look up an unqualified (no dots) name in all active namespaces.
4517
4519
4518
4520
Note that the result may contain a PlaceholderNode. The caller may
@@ -4568,6 +4570,18 @@ def lookup(self, name: str, ctx: Context,
4568
4570
return None
4569
4571
node = table [name ]
4570
4572
return node
4573
+ # 6. based
4574
+ if annotation and self .is_future_flag_set ("annotations" ):
4575
+ # 6a. typing
4576
+ table = self .modules ["typing" ].names
4577
+ if name in table :
4578
+ node = table [name ]
4579
+ return node
4580
+ # 6b. basedtyping
4581
+ table = self .modules ["basedtyping" ].names
4582
+ if name in table :
4583
+ node = table [name ]
4584
+ return node
4571
4585
# Give up.
4572
4586
if not implicit_name and not suppress_errors :
4573
4587
self .name_not_defined (name , ctx )
@@ -4638,7 +4652,8 @@ def is_defined_in_current_module(self, fullname: Optional[str]) -> bool:
4638
4652
return module_prefix (self .modules , fullname ) == self .cur_mod_id
4639
4653
4640
4654
def lookup_qualified (self , name : str , ctx : Context ,
4641
- suppress_errors : bool = False ) -> Optional [SymbolTableNode ]:
4655
+ suppress_errors : bool = False ,
4656
+ annotation : bool = False ) -> Optional [SymbolTableNode ]:
4642
4657
"""Lookup a qualified name in all activate namespaces.
4643
4658
4644
4659
Note that the result may contain a PlaceholderNode. The caller may
@@ -4650,10 +4665,10 @@ def lookup_qualified(self, name: str, ctx: Context,
4650
4665
"""
4651
4666
if '.' not in name :
4652
4667
# Simple case: look up a short name.
4653
- return self .lookup (name , ctx , suppress_errors = suppress_errors )
4668
+ return self .lookup (name , ctx , suppress_errors = suppress_errors , annotation = annotation )
4654
4669
parts = name .split ('.' )
4655
4670
namespace = self .cur_mod_id
4656
- sym = self .lookup (parts [0 ], ctx , suppress_errors = suppress_errors )
4671
+ sym = self .lookup (parts [0 ], ctx , suppress_errors = suppress_errors , annotation = annotation )
4657
4672
if sym :
4658
4673
for i in range (1 , len (parts )):
4659
4674
node = sym .node
0 commit comments