wookayin / semshi

🌈 Semantic Highlighting for Python in Neovim
79 stars 5 forks source link

Add PEP 563 support (from __future__ import annotations) #2

Closed wookayin closed 1 year ago

wookayin commented 1 year ago

See numirias/semshi#116

PEP 563 - Postponed Evaluation of Annotations is available since python 3.7, but Semshi would throw an error when this behavior is enabled. The main point of PEP-563 is to resolve type hints at runtime; the python symbol table may not return a symbol for annotations.

Test code

See test_postponed_evaluation_of_annotations_pep563 in test_parser.py:

from __future__ import annotations
from typing import List, Any

# globals
a: int = 1
b: UnknownSymbol = 2
c: List[Any] = []

# nested scope and symbol: a tricker case
def foo():
   local_var: List[Any] = []  # local variables

class Foo:
   attr: List[Any] = ()  # class attributes
   def __init__(self, v: Optional[List[Any]]):  
       temp: Any = None                      
wookayin commented 1 year ago

With PEP 563 turned on, nested scope may not work properly with the previous implementation. f147b61 addresses this, adding nested scope support.

from __future__ import annotations
from typing import List, Any

global_var: List[Any]

def foo(param: List[Any]):
    local_var: List[Any] = []       # <--- `List`, `Any` should be looked up from the parent symbol table