diff --git a/lark/common.py b/lark/common.py index 12149b6..6c3962e 100644 --- a/lark/common.py +++ b/lark/common.py @@ -1,14 +1,14 @@ from copy import deepcopy from types import ModuleType +from typing import Callable, Collection, Dict, Optional, TYPE_CHECKING + +if TYPE_CHECKING: + from .lark import PostLex from .utils import Serialize from .lexer import TerminalDef, Token ###{standalone -from typing import Any, Callable, Collection, Dict, Optional, TYPE_CHECKING - -if TYPE_CHECKING: - from .lark import PostLex _Callback = Callable[[Token], Token] diff --git a/lark/exceptions.py b/lark/exceptions.py index 797d5cb..55e9a3a 100644 --- a/lark/exceptions.py +++ b/lark/exceptions.py @@ -1,16 +1,14 @@ from .utils import logger, NO_VALUE - - -###{standalone -from collections.abc import Sequence - from typing import Dict, Iterable, Callable, Union, TypeVar, Tuple, Any, List, Set, Optional, TYPE_CHECKING +from collections.abc import Sequence if TYPE_CHECKING: from .lexer import Token from .parsers.lalr_interactive_parser import InteractiveParser from .tree import Tree +###{standalone + class LarkError(Exception): pass diff --git a/lark/grammar.py b/lark/grammar.py index 25aec17..3d6f0ff 100644 --- a/lark/grammar.py +++ b/lark/grammar.py @@ -1,10 +1,9 @@ +from typing import Optional, Tuple, ClassVar from .utils import Serialize ###{standalone -from typing import Optional, Tuple, ClassVar - class Symbol(Serialize): __slots__ = ('name',) diff --git a/lark/indenter.py b/lark/indenter.py index b7b3369..0a18347 100644 --- a/lark/indenter.py +++ b/lark/indenter.py @@ -1,19 +1,18 @@ "Provides Indentation services for languages with indentation similar to Python" from abc import ABC, abstractmethod +from typing import List, Iterator from .exceptions import LarkError from .lark import PostLex from .lexer import Token ###{standalone -from typing import List, Iterator class DedentError(LarkError): pass class Indenter(PostLex, ABC): - paren_level: int indent_level: List[int] diff --git a/lark/lark.py b/lark/lark.py index 8b8af4e..aed3346 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -1,7 +1,18 @@ from abc import ABC, abstractmethod import sys, os, pickle, hashlib import tempfile - +from typing import ( + TypeVar, Type, List, Dict, Iterator, Callable, Union, Optional, + Tuple, Iterable, IO, Any, TYPE_CHECKING +) +if TYPE_CHECKING: + from .parsers.lalr_interactive_parser import InteractiveParser + from .visitors import Transformer + if sys.version_info >= (3, 8): + from typing import Literal + else: + from typing_extensions import Literal + from .exceptions import ConfigurationError, assert_config, UnexpectedInput from .utils import Serialize, SerializeMemoizer, FS, isascii, logger from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files, PackageResource @@ -21,18 +32,7 @@ except ImportError: ###{standalone -from typing import ( - TypeVar, Type, List, Dict, Iterator, Callable, Union, Optional, - Tuple, Iterable, IO, Any, TYPE_CHECKING -) -if TYPE_CHECKING: - from .parsers.lalr_interactive_parser import InteractiveParser - from .visitors import Transformer - if sys.version_info >= (3, 8): - from typing import Literal - else: - from typing_extensions import Literal class PostLex(ABC): @abstractmethod diff --git a/lark/lexer.py b/lark/lexer.py index f826e06..173b3f5 100644 --- a/lark/lexer.py +++ b/lark/lexer.py @@ -3,6 +3,13 @@ from abc import abstractmethod, ABC import re from contextlib import suppress +from typing import ( + TypeVar, Type, List, Dict, Iterator, Collection, Callable, Optional, FrozenSet, Any, + Pattern as REPattern, ClassVar, TYPE_CHECKING +) +from types import ModuleType +if TYPE_CHECKING: + from .common import LexerConf from .utils import classify, get_regexp_width, Serialize from .exceptions import UnexpectedCharacters, LexError, UnexpectedToken @@ -10,14 +17,6 @@ from .exceptions import UnexpectedCharacters, LexError, UnexpectedToken ###{standalone from copy import copy -from types import ModuleType -from typing import ( - TypeVar, Type, Tuple, List, Dict, Iterator, Collection, Callable, Optional, FrozenSet, Any, - Pattern as REPattern, ClassVar, TYPE_CHECKING -) - -if TYPE_CHECKING: - from .common import LexerConf class Pattern(Serialize, ABC): diff --git a/lark/tools/standalone.py b/lark/tools/standalone.py index 1cc8f81..7282699 100644 --- a/lark/tools/standalone.py +++ b/lark/tools/standalone.py @@ -25,6 +25,13 @@ # from abc import ABC, abstractmethod +from collections.abc import Sequence +from types import ModuleType +from typing import ( + TypeVar, Generic, Type, Tuple, List, Dict, Iterator, Collection, Callable, Optional, FrozenSet, Any, + Union, Iterable, IO, TYPE_CHECKING, + Pattern as REPattern, ClassVar, Set, +) ###} import sys diff --git a/lark/tree.py b/lark/tree.py index 90ec0fe..1ca0c62 100644 --- a/lark/tree.py +++ b/lark/tree.py @@ -1,3 +1,4 @@ + try: from future_builtins import filter # type: ignore except ImportError: @@ -6,10 +7,7 @@ except ImportError: import sys from copy import deepcopy - -###{standalone -from collections import OrderedDict -from typing import List, Callable, Iterator, Union, Optional, Any, TYPE_CHECKING +from typing import List, Callable, Iterator, Union, Optional, TYPE_CHECKING if TYPE_CHECKING: from .lexer import TerminalDef @@ -18,6 +16,9 @@ if TYPE_CHECKING: else: from typing_extensions import Literal +###{standalone +from collections import OrderedDict + class Meta: empty: bool diff --git a/lark/visitors.py b/lark/visitors.py index 2c7309f..954886a 100644 --- a/lark/visitors.py +++ b/lark/visitors.py @@ -1,3 +1,4 @@ +from typing import TypeVar, Tuple, List, Callable, Generic, Type, Union, Optional from abc import ABC from functools import wraps @@ -8,7 +9,6 @@ from .lexer import Token ###{standalone from inspect import getmembers, getmro -from typing import TypeVar, Tuple, List, Callable, Generic, Type, Union, Optional _T = TypeVar('_T') _R = TypeVar('_R') diff --git a/tests/test_tools.py b/tests/test_tools.py index 7a732d1..fd42b1c 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -24,7 +24,7 @@ class TestStandalone(TestCase): standalone.gen_standalone(Lark(grammar, parser='lalr'), out=code_buf, compress=compress) code = code_buf.getvalue() - context = {'__doc__': None} + context = {'__doc__': None, '__name__': 'test_standalone'} exec(code, context) return context