Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 703d067

Browse files
authored
Make _LiteralEvalVisitor public (#129)
1 parent 6c7740f commit 703d067

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

typeshed_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from .parser import (
1313
ImportedName,
14+
LiteralEvalVisitor,
1415
NameDict,
1516
NameInfo,
1617
OverloadedName,
@@ -25,6 +26,7 @@
2526
__all__ = [
2627
"ImportedInfo",
2728
"ImportedName",
29+
"LiteralEvalVisitor",
2830
"ModulePath",
2931
"NameDict",
3032
"NameInfo",

typeshed_client/parser.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def visit_If(self, node: ast.If) -> Iterable[NameInfo]:
305305
yield from self.visit(stmt)
306306

307307
def _visit_condition(self, expr: ast.expr) -> Optional[bool]:
308-
visitor = _LiteralEvalVisitor(self.ctx, self.file_path)
308+
visitor = LiteralEvalVisitor(self.ctx, self.file_path)
309309
try:
310310
value = visitor.visit(expr)
311311
except InvalidStub:
@@ -430,7 +430,17 @@ def generic_visit(self, node: ast.AST) -> Iterable[NameInfo]:
430430
raise InvalidStub(f"Cannot handle node {ast.dump(node)}", self.file_path)
431431

432432

433-
class _LiteralEvalVisitor(ast.NodeVisitor):
433+
class LiteralEvalVisitor(ast.NodeVisitor):
434+
"""Visitor to evaluate the truthiness of a ``test`` expression in an ``ast.Compare`` node.
435+
436+
``LiteralEvalVisitor(ctx, path).visit(node)`` will return ``True`` if ``node`` is an
437+
expression that can be statically determined to always be ``True``, ``False`` if it can
438+
be statically determined to always be ``False``, and ``None`` if its truthiness cannot
439+
be determined statically. For example, if passed an AST node representing the expression
440+
``sys.platform == "linux"``, it will return ``True`` if ``ctx.platform`` is equal to
441+
``"linux"``, otherwise ``False``.
442+
"""
443+
434444
def __init__(self, ctx: SearchContext, file_path: Optional[Path]) -> None:
435445
self.ctx = ctx
436446
self.file_path = file_path

0 commit comments

Comments
 (0)