Instagram / LibCST

A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
https://libcst.readthedocs.io/
Other
1.57k stars 192 forks source link

pyright sees return type of CSTTransformer.get_metadata() as `CodeRange | type[_UNDEFINED_DEFAULT]` #1107

Open jakkdl opened 9 months ago

jakkdl commented 9 months ago

I started encountering this when updating https://github.com/python-trio/flake8-trio - but it's been 9 months since last time I ran the type tests so it'd be a pain to figure out when exactly it started happening. If needed I can try to binary search for it though, and/or figure out if it's an update in pyright or libcst that messed it up.

repro

foo.py:

import libcst
from libcst.metadata import PositionProvider

class Foo(libcst.CSTTransformer):
    METADATA_DEPENDENCIES = (PositionProvider,)
    def visit_Comment(self, node: libcst.Comment) -> bool:
        metadata = self.get_metadata(PositionProvider, node)
        reveal_type(metadata)
        return False

Works in mypy

$ mypy foo.py
foo.py:8: note: Revealed type is "libcst._position.CodeRange"
Success: no issues found in 1 source file

but pyright gives an additional weird union

$ pyright foo.py 
/tmp/pyright+libcst/foo.py
  /tmp/pyright+libcst/foo.py:8:21 - information: Type of "metadata" is "CodeRange | type[_UNDEFINED_DEFAULT]"
0 errors, 0 warnings, 1 information

versions