From cb35acad1784af466274c1819a28c989f3eea37c Mon Sep 17 00:00:00 2001 From: James Westman Date: Fri, 28 Jan 2022 14:07:29 -0600 Subject: [PATCH] Fix mypy issues --- blueprintcompiler/completions.py | 25 ++++++++++---------- blueprintcompiler/language/__init__.py | 4 +++- blueprintcompiler/language/gobject_object.py | 5 ++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/blueprintcompiler/completions.py b/blueprintcompiler/completions.py index 25f0301..0a5e759 100644 --- a/blueprintcompiler/completions.py +++ b/blueprintcompiler/completions.py @@ -19,7 +19,8 @@ import typing as T -from . import gir +from . import gir, language +from .ast_utils import AstNode from .completions_utils import * from .lsp_utils import Completion, CompletionItemKind from .parser import SKIP_TOKENS @@ -28,7 +29,7 @@ from .tokenizer import TokenType, Token Pattern = T.List[T.Tuple[TokenType, T.Optional[str]]] -def _complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int, token_idx: int) -> T.Iterator[Completion]: +def _complete(ast_node: AstNode, tokens: T.List[Token], idx: int, token_idx: int) -> T.Iterator[Completion]: for child in ast_node.children: if child.group.start <= idx and (idx < child.group.end or (idx == child.group.end and child.incomplete)): yield from _complete(child, tokens, idx, token_idx) @@ -47,7 +48,7 @@ def _complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int, token_idx: yield from completer(prev_tokens, ast_node) -def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterator[Completion]: +def complete(ast_node: AstNode, tokens: T.List[Token], idx: int) -> T.Iterator[Completion]: token_idx = 0 # find the current token for i, token in enumerate(tokens): @@ -62,24 +63,24 @@ def complete(ast_node: ast.AstNode, tokens: T.List[Token], idx: int) -> T.Iterat yield from _complete(ast_node, tokens, idx, token_idx) -@completer([ast.GtkDirective]) +@completer([language.GtkDirective]) def using_gtk(ast_node, match_variables): yield Completion("using Gtk 4.0;", CompletionItemKind.Keyword) @completer( - applies_in=[ast.UI, ast.ObjectContent, ast.Template], + applies_in=[language.UI, language.ObjectContent, language.Template], matches=new_statement_patterns ) def namespace(ast_node, match_variables): yield Completion("Gtk", CompletionItemKind.Module, text="Gtk.") - for ns in ast_node.root.children[ast.Import]: + for ns in ast_node.root.children[language.Import]: if ns.gir_namespace is not None: yield Completion(ns.gir_namespace.name, CompletionItemKind.Module, text=ns.gir_namespace.name + ".") @completer( - applies_in=[ast.UI, ast.ObjectContent, ast.Template], + applies_in=[language.UI, language.ObjectContent, language.Template], matches=[ [(TokenType.IDENT, None), (TokenType.OP, "."), (TokenType.IDENT, None)], [(TokenType.IDENT, None), (TokenType.OP, ".")], @@ -93,7 +94,7 @@ def object_completer(ast_node, match_variables): @completer( - applies_in=[ast.ObjectContent], + applies_in=[language.ObjectContent], matches=new_statement_patterns, ) def property_completer(ast_node, match_variables): @@ -103,7 +104,7 @@ def property_completer(ast_node, match_variables): @completer( - applies_in=[ast.Property, ast.BaseTypedAttribute], + applies_in=[language.Property, language.BaseTypedAttribute], matches=[ [(TokenType.IDENT, None), (TokenType.OP, ":")] ], @@ -119,13 +120,13 @@ def prop_value_completer(ast_node, match_variables): @completer( - applies_in=[ast.ObjectContent], + applies_in=[language.ObjectContent], matches=new_statement_patterns, ) def signal_completer(ast_node, match_variables): if ast_node.gir_class: for signal in ast_node.gir_class.signals: - if not isinstance(ast_node.parent, ast.Object): + if not isinstance(ast_node.parent, language.Object): name = "on" else: name = "on_" + (ast_node.parent.tokens["id"] or ast_node.parent.tokens["class_name"].lower()) @@ -133,7 +134,7 @@ def signal_completer(ast_node, match_variables): @completer( - applies_in=[ast.UI], + applies_in=[language.UI], matches=new_statement_patterns ) def template_completer(ast_node, match_variables): diff --git a/blueprintcompiler/language/__init__.py b/blueprintcompiler/language/__init__.py index 61af8e7..68c3d8a 100644 --- a/blueprintcompiler/language/__init__.py +++ b/blueprintcompiler/language/__init__.py @@ -1,7 +1,8 @@ """ Contains all the syntax beyond basic objects, properties, signal, and templates. """ -from .gobject_object import Object +from .attributes import BaseAttribute, BaseTypedAttribute +from .gobject_object import Object, ObjectContent from .gobject_property import Property from .gobject_signal import Signal from .gtk_a11y import A11y @@ -14,6 +15,7 @@ from .gtk_string_list import Strings from .gtk_styles import Styles from .gtkbuilder_child import Child from .gtkbuilder_template import Template +from .imports import GtkDirective, Import from .ui import UI from .values import IdentValue, TranslatedStringValue, FlagsValue, LiteralValue diff --git a/blueprintcompiler/language/gobject_object.py b/blueprintcompiler/language/gobject_object.py index 51b34a6..c8889a0 100644 --- a/blueprintcompiler/language/gobject_object.py +++ b/blueprintcompiler/language/gobject_object.py @@ -18,6 +18,7 @@ # SPDX-License-Identifier: LGPL-3.0-or-later +import typing as T from .common import * @@ -41,11 +42,11 @@ class ObjectContent(AstNode): x.emit_xml(xml) class Object(AstNode): - grammar = Sequence( + grammar: T.Any = [ class_name, Optional(UseIdent("id")), ObjectContent, - ) + ] @validate("namespace") def gir_ns_exists(self):