Skip to content

Commit

Permalink
fix: typing issues with cython.
Browse files Browse the repository at this point in the history
  • Loading branch information
akornatskyy committed Jul 28, 2023
1 parent 893ce35 commit 00ba980
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
15 changes: 15 additions & 0 deletions src/wheezy/template/comp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import sys
import typing
from _thread import allocate_lock # noqa

Expand All @@ -7,3 +8,17 @@ def adjust_source_lineno(source: str, name: str, lineno: int) -> typing.Any:
node = compile(source, name, "exec", ast.PyCF_ONLY_AST)
ast.increment_lineno(node, lineno)
return node


if sys.version_info <= (3, 9, 0): # pragma: nocover
from typing import List, Tuple
else: # pragma: nocover
Tuple = tuple # type: ignore
List = list # type: ignore


__all__ = (
"adjust_source_lineno",
"Tuple",
"List",
)
5 changes: 2 additions & 3 deletions src/wheezy/template/ext/code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import typing

from wheezy.template.comp import Tuple
from wheezy.template.typing import Builder, LexerRule, ParserRule, Token
from wheezy.template.utils import find_balanced

Expand Down Expand Up @@ -54,6 +55,4 @@ def __init__(self, token_start: str = "@") -> None:

parser_rules: typing.Mapping[str, ParserRule] = {"code": parse_code}

builder_rules: typing.List[typing.Tuple[str, typing.Any]] = [
("code", build_code)
]
builder_rules: typing.List[Tuple[str, typing.Any]] = [("code", build_code)]
11 changes: 6 additions & 5 deletions src/wheezy/template/ext/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import typing

from wheezy.template.comp import Tuple
from wheezy.template.typing import Builder, LexerRule, ParserConfig, Token
from wheezy.template.utils import find_all_balanced

Expand Down Expand Up @@ -83,12 +84,12 @@ def parse_include(value: str) -> str:
return value.rstrip()[8:-1]


def parse_import(value: str) -> typing.Tuple[str, str]:
def parse_import(value: str) -> Tuple[str, str]:
name, var = value[7:].rsplit(" as ", 1)
return name, var


def parse_from(value: str) -> typing.Tuple[str, str, str]:
def parse_from(value: str) -> Tuple[str, str, str]:
name, var = value[5:].rsplit(" import ", 1)
s = var.rsplit(" as ", 1)
if len(s) == 2:
Expand All @@ -100,7 +101,7 @@ def parse_from(value: str) -> typing.Tuple[str, str, str]:

def parse_var(
value: str,
) -> typing.Tuple[str, typing.Optional[typing.List[str]]]:
) -> Tuple[str, typing.Optional[typing.List[str]]]:
if "!!" not in value:
return value, None
var, var_filter = value.rsplit("!!", 1)
Expand Down Expand Up @@ -144,7 +145,7 @@ def build_module(


def build_import(
builder: Builder, lineno: int, token: str, value: typing.Tuple[str, str]
builder: Builder, lineno: int, token: str, value: Tuple[str, str]
) -> bool:
assert token == "import "
name, var = value
Expand All @@ -156,7 +157,7 @@ def build_from(
builder: Builder,
lineno: int,
token: str,
value: typing.Tuple[str, str, str],
value: Tuple[str, str, str],
) -> bool:
assert token == "from "
name, var, alias = value
Expand Down
3 changes: 2 additions & 1 deletion src/wheezy/template/ext/determined.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import typing

from wheezy.template.comp import Tuple
from wheezy.template.utils import find_balanced

RE_ARGS = re.compile(r'\s*(?P<expr>(([\'"]).*?\3|.+?))\s*\,')
Expand Down Expand Up @@ -117,7 +118,7 @@ def parse_args(text: str) -> typing.List[str]:

def parse_params(
text: str,
) -> typing.Tuple[typing.List[str], typing.Mapping[str, str]]:
) -> Tuple[typing.List[str], typing.Mapping[str, str]]:
"""Parses function parameters.
>>> parse_params('')
Expand Down
9 changes: 5 additions & 4 deletions src/wheezy/template/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import typing

from wheezy.template.comp import Tuple
from wheezy.template.engine import Engine
from wheezy.template.typing import Loader, SupportsRender

Expand All @@ -27,7 +28,7 @@ def __init__(
self.searchpath = searchpath
self.encoding = encoding

def list_names(self) -> typing.Tuple[str, ...]:
def list_names(self) -> Tuple[str, ...]:
"""Return a list of names relative to directories. Ignores any files
and directories that start with dot.
"""
Expand Down Expand Up @@ -83,7 +84,7 @@ class DictLoader(Loader):
def __init__(self, templates: typing.Mapping[str, str]) -> None:
self.templates = templates

def list_names(self) -> typing.Tuple[str, ...]:
def list_names(self) -> Tuple[str, ...]:
"""List all keys from internal dict."""
return tuple(sorted(self.templates.keys()))

Expand All @@ -100,7 +101,7 @@ class ChainLoader(Loader):
def __init__(self, loaders: typing.List[Loader]) -> None:
self.loaders = loaders

def list_names(self) -> typing.Tuple[str, ...]:
def list_names(self) -> Tuple[str, ...]:
"""Returns as list of names from all loaders."""
names = set()
for loader in self.loaders:
Expand All @@ -127,7 +128,7 @@ def __init__(
self.engine = engine
self.ctx = ctx or {}

def list_names(self) -> typing.Tuple[str, ...]:
def list_names(self) -> Tuple[str, ...]:
return self.engine.loader.list_names()

def load(self, name: str) -> str:
Expand Down
26 changes: 12 additions & 14 deletions src/wheezy/template/typing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import sys
import typing
from abc import abstractmethod

if sys.version_info <= (3, 9, 0): # pragma: nocover
Token = typing.Tuple[int, str, str]
else: # pragma: nocover
Token = tuple[int, str, str] # type: ignore[misc]
from wheezy.template.comp import List, Tuple

Token = Tuple[int, str, str]


class Builder:
Expand Down Expand Up @@ -38,26 +36,26 @@ def build_token(


Tokenizer = typing.Callable[[typing.Match], Token]
LexerRule = typing.Tuple[typing.Pattern, Tokenizer]
LexerRule = Tuple[typing.Pattern, Tokenizer]
PreProcessorRule = typing.Callable[[str], str]
PostProcessorRule = typing.Callable[[typing.List[Token]], str]
PostProcessorRule = typing.Callable[[List[Token]], str]
BuilderRule = typing.Callable[
[
Builder,
int,
str,
typing.Union[str, typing.List[str], typing.Iterable[Token]],
typing.Union[str, List[str], typing.Iterable[Token]],
],
bool,
]
ParserRule = typing.Callable[[str], typing.Union[str, typing.List[str]]]
ParserRule = typing.Callable[[str], typing.Union[str, List[str]]]


class ParserConfig:
end_tokens: typing.List[str]
continue_tokens: typing.List[str]
compound_tokens: typing.List[str]
out_tokens: typing.List[str]
end_tokens: List[str]
continue_tokens: List[str]
compound_tokens: List[str]
out_tokens: List[str]


RenderTemplate = typing.Callable[
Expand All @@ -81,7 +79,7 @@ def render(self, ctx: typing.Mapping[str, typing.Any]) -> str:

class Loader:
@abstractmethod
def list_names(self) -> typing.Tuple[str, ...]:
def list_names(self) -> Tuple[str, ...]:
... # pragma: nocover

@abstractmethod
Expand Down

0 comments on commit 00ba980

Please sign in to comment.