Commit 5d973963 authored by Nsukami Patrick's avatar Nsukami Patrick
Browse files

refactor(mypy): Finish type hint the rql/__init__.py module

- Type hint all `parse` functions inside rql/__init__.py
- Create a type alias to represent what's returned by `parse` functions above
- Update RQLSTChecker.check method signature:
  * node parameter should be a BaseNode	or any children	of BaseNode class
- Type hint `copy_uid_node` function
- Remove useless variable: pyparse = parse
- Format without any compromise
parent e1ac84f98295
Pipeline #74348 passed with stages
in 1 minute and 14 seconds
......@@ -113,7 +113,7 @@ class RQLHelper:
backend = property(get_backend, set_backend)
def parse(self, rqlstring: str, annotate: bool = True) -> "rql.stmts.Statement":
def parse(self, rqlstring: str, annotate: bool = True) -> rt.Query:
"""Return a syntax tree created from a RQL string."""
rqlst = parse(rqlstring, False)
self._checker.check(rqlst)
......@@ -221,14 +221,20 @@ class RQLHelper:
select.clean_solutions()
def copy_uid_node(select, node, vconsts):
node = node.copy(select)
def copy_uid_node(
select: "rql.stmts.Select",
node: "rql.nodes.Constant",
vconsts: List["rql.nodes.Constant"],
) -> "rql.nodes.Constant":
# Incompatible types in assignment
# (expression has type "BaseNode", variable has type "Constant")
node = node.copy(select) # type:ignore[assignment]
node.uid = True
vconsts.append(node)
return node
def parse(rqlstring, print_errors=True):
def parse(rqlstring: str, print_errors: bool = True) -> rt.Query:
"""Return a syntax tree created from a RQL string."""
from yapps.runtime import print_error, SyntaxError, NoMoreTokens
from rql.parser import Hercule, HerculeScanner
......@@ -304,7 +310,10 @@ def parse(rqlstring, print_errors=True):
except ImportError: # duh?
sys.stderr = out
exc = RQLSyntaxError(
"Syntax Error", ex.msg, "on line", 1 + out.count("\n", 0, ex.pos)
"Syntax Error",
ex.msg,
"on line",
1 + out.count("\n", 0, ex.pos), # type:ignore
)
exc.__traceback__ = sys.exc_info()[-1]
raise exc
......@@ -313,6 +322,3 @@ def parse(rqlstring, print_errors=True):
exc = RQLSyntaxError(msg % parser._scanner)
exc.__traceback__ = sys.exc_info()[-1]
raise exc
pyparse = parse
......@@ -125,3 +125,13 @@ else:
BaseNode = "rql.base.BaseNode"
UIDFuncMapping = Optional[Dict[str, Callable[[int], str]]]
Query = Union_[
"rql.stmts.Set",
"rql.stmts.Delete",
"rql.stmts.Union",
"rql.stmts.Insert",
"rql.stmts.Select",
"rql.stmts.Union",
"rql.stmts.Select",
]
......@@ -37,7 +37,16 @@ from rql.nodes import (
from rql import rqltypes as rt
from typing import TYPE_CHECKING, Dict, List, Union as Union_, Any, Optional, cast
from typing import (
TYPE_CHECKING,
Dict,
List,
Union as Union_,
Any,
Optional,
cast,
TypeVar,
)
__docformat__: str = "restructuredtext en"
......@@ -121,8 +130,9 @@ class RQLSTChecker:
self.special_relations = special_relations or {}
self.backend = backend
def check(self, node: "rql.base.BaseNode") -> None: # "rql.stmts.Statement"
T = TypeVar("T", bound="rql.base.BaseNode")
def check(self, node: "T") -> None:
state = STCheckState()
self._visit(node, state)
if state.errors:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment