From 67904f4d24efaa97600d52682ade51fd63222206 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 25 Jul 2020 15:43:28 +0200 Subject: [PATCH] Make mypy happy --- parso/cache.py | 3 ++- parso/normalizer.py | 11 ++++------- parso/parser.py | 7 ++++--- parso/python/pep8.py | 10 ++++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/parso/cache.py b/parso/cache.py index fd3563a..c0bb479 100644 --- a/parso/cache.py +++ b/parso/cache.py @@ -8,6 +8,7 @@ import platform import logging import warnings import pickle +from typing import Dict, Any from parso.file_io import FileIO @@ -98,7 +99,7 @@ def _get_cache_clear_lock(cache_path=None): return FileIO(os.path.join(cache_path, "PARSO-CACHE-LOCK")) -parser_cache = {} +parser_cache: Dict[str, Any] = {} class _NodeCacheItem(object): diff --git a/parso/normalizer.py b/parso/normalizer.py index 4477147..5c5d2b9 100644 --- a/parso/normalizer.py +++ b/parso/normalizer.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from typing import Dict, List class _NormalizerMeta(type): @@ -10,8 +11,8 @@ class _NormalizerMeta(type): class Normalizer(metaclass=_NormalizerMeta): - _rule_type_instances = {} - _rule_value_instances = {} + _rule_type_instances: Dict[str, List[type]] = {} + _rule_value_instances: Dict[str, List[type]] = {} def __init__(self, grammar, config): self.grammar = grammar @@ -75,7 +76,7 @@ class Normalizer(metaclass=_NormalizerMeta): return True @classmethod - def register_rule(cls, **kwargs): + def register_rule(cls, *, value=None, values=(), type=None, types=()): """ Use it as a class decorator:: @@ -84,10 +85,6 @@ class Normalizer(metaclass=_NormalizerMeta): class MyRule(Rule): error_code = 42 """ - return cls._register_rule(**kwargs) - - @classmethod - def _register_rule(cls, value=None, values=(), type=None, types=()): values = list(values) types = list(types) if value is not None: diff --git a/parso/parser.py b/parso/parser.py index 2bfa89f..a7cc6fa 100644 --- a/parso/parser.py +++ b/parso/parser.py @@ -23,6 +23,8 @@ within the statement. This lowers memory usage and cpu time and reduces the complexity of the ``Parser`` (there's another parser sitting inside ``Statement``, which produces ``Array`` and ``Call``). """ +from typing import Dict + from parso import tree from parso.pgen2.generator import ReservedString @@ -108,11 +110,10 @@ class BaseParser(object): When a syntax error occurs, error_recovery() is called. """ - node_map = {} + node_map: Dict[str, type] = {} default_node = tree.Node - leaf_map = { - } + leaf_map: Dict[str, type] = {} default_leaf = tree.Leaf def __init__(self, pgen_grammar, start_nonterminal='file_input', error_recovery=False): diff --git a/parso/python/pep8.py b/parso/python/pep8.py index 5506134..13e17ed 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -1,5 +1,6 @@ import re from contextlib import contextmanager +from typing import Tuple from parso.python.errors import ErrorFinder, ErrorFinderConfig from parso.normalizer import Rule @@ -15,10 +16,11 @@ _CLOSING_BRACKETS = ')', ']', '}' _FACTOR = '+', '-', '~' _ALLOW_SPACE = '*', '+', '-', '**', '/', '//', '@' _BITWISE_OPERATOR = '<<', '>>', '|', '&', '^' -_NEEDS_SPACE = ('=', '%', '->', - '<', '>', '==', '>=', '<=', '<>', '!=', - '+=', '-=', '*=', '@=', '/=', '%=', '&=', '|=', '^=', '<<=', - '>>=', '**=', '//=') +_NEEDS_SPACE: Tuple[str, ...] = ( + '=', '%', '->', + '<', '>', '==', '>=', '<=', '<>', '!=', + '+=', '-=', '*=', '@=', '/=', '%=', '&=', '|=', '^=', '<<=', + '>>=', '**=', '//=') _NEEDS_SPACE += _BITWISE_OPERATOR _IMPLICIT_INDENTATION_TYPES = ('dictorsetmaker', 'argument') _POSSIBLE_SLICE_PARENTS = ('subscript', 'subscriptlist', 'sliceop')