Skip to content

Replace typing.re import #2414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions wemake_python_styleguide/visitors/ast/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
FrozenSet,
List,
Optional,
Pattern,
Sequence,
Union,
)
from typing.re import Pattern

from typing_extensions import Final, final

Expand Down Expand Up @@ -73,7 +73,7 @@ class WrongStringVisitor(base.BaseNodeVisitor):
))

#: Copied from https://stackoverflow.com/a/30018957/4842742
_modulo_string_pattern: ClassVar[Pattern] = re.compile(
_modulo_string_pattern: ClassVar[Pattern[str]] = re.compile(
r""" # noqa: WPS323
( # start of capture group 1
% # literal "%"
Expand Down Expand Up @@ -142,7 +142,7 @@ def _check_modulo_patterns(
if parent and strings.is_doc_string(parent):
return # we allow `%s` in docstrings: they cannot be formatted.

if self._modulo_string_pattern.search(text_data):
if text_data and self._modulo_string_pattern.search(text_data):
if not self._is_modulo_pattern_exception(parent):
self.add_violation(
consistency.ModuloStringFormatViolation(node),
Expand Down
13 changes: 7 additions & 6 deletions wemake_python_styleguide/visitors/tokenize/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import re
import tokenize
from token import ENDMARKER
from typing import ClassVar
from typing.re import Pattern
from typing import ClassVar, Pattern

from typing_extensions import Final, final

Expand Down Expand Up @@ -54,8 +53,8 @@
class WrongCommentVisitor(BaseTokenVisitor):
"""Checks comment tokens."""

_no_cover: ClassVar[Pattern] = re.compile(r'^pragma:\s+no\s+cover')
_type_check: ClassVar[Pattern] = re.compile(
_no_cover: ClassVar[Pattern[str]] = re.compile(r'^pragma:\s+no\s+cover')
_type_check: ClassVar[Pattern[str]] = re.compile(
r'^type:\s?([\w\d\[\]\'\"\.]+)$',
)

Expand Down Expand Up @@ -182,7 +181,7 @@ class ShebangVisitor(BaseTokenVisitor):
Code is insipired by https://github.com/xuhdev/flake8-executable
"""

_shebang: ClassVar[Pattern] = re.compile(r'(\s*)#!')
_shebang: ClassVar[Pattern[str]] = re.compile(r'(\s*)#!')
_python_executable: ClassVar[str] = 'python'

def visit_comment(self, token: tokenize.TokenInfo) -> None:
Expand Down Expand Up @@ -262,7 +261,9 @@ def _is_valid_shebang_line(self, token: tokenize.TokenInfo) -> bool:
class NoqaVisitor(BaseTokenVisitor):
"""Checks noqa comment tokens."""

_noqa_check: ClassVar[Pattern] = re.compile(r'^(noqa:?)($|[A-Z\d\,\s]+)')
_noqa_check: ClassVar[Pattern[str]] = re.compile(
r'^(noqa:?)($|[A-Z\d\,\s]+)',
)

def __init__(self, *args, **kwargs) -> None:
"""Initializes a counter."""
Expand Down
15 changes: 7 additions & 8 deletions wemake_python_styleguide/visitors/tokenize/primitives.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
import tokenize
from typing import ClassVar, FrozenSet, Optional
from typing.re import Pattern
from typing import ClassVar, FrozenSet, Optional, Pattern

from typing_extensions import final

Expand All @@ -27,18 +26,18 @@ def _replace_braces(string: str) -> str:
class WrongNumberTokenVisitor(BaseTokenVisitor):
"""Visits number tokens to find incorrect usages."""

_bad_number_suffixes: ClassVar[Pattern] = re.compile(
_bad_number_suffixes: ClassVar[Pattern[str]] = re.compile(
r'^[0-9\.]+[BOXE]',
)

_leading_zero_pattern: ClassVar[Pattern] = re.compile(
_leading_zero_pattern: ClassVar[Pattern[str]] = re.compile(
r'^[0-9\.]+([box]|e\+?\-?)0.+', re.IGNORECASE | re.ASCII,
)
_leading_zero_float_pattern: ClassVar[Pattern] = re.compile(
_leading_zero_float_pattern: ClassVar[Pattern[str]] = re.compile(
r'^[0-9]*\.[0-9]+0+$',
)

_positive_exponent_patterns: ClassVar[Pattern] = re.compile(
_positive_exponent_patterns: ClassVar[Pattern[str]] = re.compile(
r'^[0-9\.]+e\+', re.IGNORECASE | re.ASCII,
)

Expand All @@ -48,7 +47,7 @@ class WrongNumberTokenVisitor(BaseTokenVisitor):

_bad_complex_suffix: ClassVar[str] = 'J'

_float_zero: ClassVar[Pattern] = re.compile(
_float_zero: ClassVar[Pattern[str]] = re.compile(
r'^0\.0$',
)

Expand Down Expand Up @@ -145,7 +144,7 @@ class WrongStringTokenVisitor(BaseTokenVisitor):
'u', 'U', 'N',
))

_implicit_raw_strings: ClassVar[Pattern] = re.compile(r'\\{2}.+')
_implicit_raw_strings: ClassVar[Pattern[str]] = re.compile(r'\\{2}.+')

def __init__(self, *args, **kwargs) -> None:
"""Initializes new visitor and saves all docstrings."""
Expand Down