mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 07:44:16 +00:00
Merge pull request #1511 from PyCQA/new_namedtuple
use typesafe NamedTuple
This commit is contained in:
commit
f6267dd4d7
4 changed files with 23 additions and 22 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
"""Statistic collection logic for Flake8."""
|
"""Statistic collection logic for Flake8."""
|
||||||
import collections
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
|
@ -73,7 +73,7 @@ class Statistics:
|
||||||
yield self._store[error_code]
|
yield self._store[error_code]
|
||||||
|
|
||||||
|
|
||||||
class Key(collections.namedtuple("Key", ["filename", "code"])):
|
class Key(NamedTuple):
|
||||||
"""Simple key structure for the Statistics dictionary.
|
"""Simple key structure for the Statistics dictionary.
|
||||||
|
|
||||||
To make things clearer, easier to read, and more understandable, we use a
|
To make things clearer, easier to read, and more understandable, we use a
|
||||||
|
|
@ -81,7 +81,8 @@ class Key(collections.namedtuple("Key", ["filename", "code"])):
|
||||||
Statistics object.
|
Statistics object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ()
|
filename: str
|
||||||
|
code: str
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_from(cls, error: "Violation") -> "Key":
|
def create_from(cls, error: "Violation") -> "Key":
|
||||||
|
|
@ -111,7 +112,7 @@ class Statistic:
|
||||||
"""Simple wrapper around the logic of each statistic.
|
"""Simple wrapper around the logic of each statistic.
|
||||||
|
|
||||||
Instead of maintaining a simple but potentially hard to reason about
|
Instead of maintaining a simple but potentially hard to reason about
|
||||||
tuple, we create a namedtuple which has attributes and a couple
|
tuple, we create a class which has attributes and a couple
|
||||||
convenience methods on it.
|
convenience methods on it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
"""Implementation of the StyleGuide used by Flake8."""
|
"""Implementation of the StyleGuide used by Flake8."""
|
||||||
import argparse
|
import argparse
|
||||||
import collections
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import copy
|
import copy
|
||||||
import enum
|
import enum
|
||||||
|
|
@ -12,6 +11,7 @@ from typing import Dict
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Match
|
from typing import Match
|
||||||
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
from typing import Set
|
from typing import Set
|
||||||
|
|
@ -54,21 +54,16 @@ def find_noqa(physical_line: str) -> Optional[Match[str]]:
|
||||||
return defaults.NOQA_INLINE_REGEXP.search(physical_line)
|
return defaults.NOQA_INLINE_REGEXP.search(physical_line)
|
||||||
|
|
||||||
|
|
||||||
class Violation(
|
class Violation(NamedTuple):
|
||||||
collections.namedtuple(
|
|
||||||
"Violation",
|
|
||||||
[
|
|
||||||
"code",
|
|
||||||
"filename",
|
|
||||||
"line_number",
|
|
||||||
"column_number",
|
|
||||||
"text",
|
|
||||||
"physical_line",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
):
|
|
||||||
"""Class representing a violation reported by Flake8."""
|
"""Class representing a violation reported by Flake8."""
|
||||||
|
|
||||||
|
code: str
|
||||||
|
filename: str
|
||||||
|
line_number: int
|
||||||
|
column_number: int
|
||||||
|
text: str
|
||||||
|
physical_line: Optional[str]
|
||||||
|
|
||||||
def is_inline_ignored(self, disable_noqa: bool) -> bool:
|
def is_inline_ignored(self, disable_noqa: bool) -> bool:
|
||||||
"""Determine if a comment has been added to ignore this line.
|
"""Determine if a comment has been added to ignore this line.
|
||||||
|
|
||||||
|
|
@ -394,7 +389,7 @@ class StyleGuideManager:
|
||||||
code: str,
|
code: str,
|
||||||
filename: str,
|
filename: str,
|
||||||
line_number: int,
|
line_number: int,
|
||||||
column_number: Optional[int],
|
column_number: int,
|
||||||
text: str,
|
text: str,
|
||||||
physical_line: Optional[str] = None,
|
physical_line: Optional[str] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
@ -527,7 +522,7 @@ class StyleGuide:
|
||||||
code: str,
|
code: str,
|
||||||
filename: str,
|
filename: str,
|
||||||
line_number: int,
|
line_number: int,
|
||||||
column_number: Optional[int],
|
column_number: int,
|
||||||
text: str,
|
text: str,
|
||||||
physical_line: Optional[str] = None,
|
physical_line: Optional[str] = None,
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import textwrap
|
||||||
import tokenize
|
import tokenize
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Pattern
|
from typing import Pattern
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
@ -50,7 +51,11 @@ def parse_comma_separated_list(
|
||||||
return [item for item in item_gen if item]
|
return [item for item in item_gen if item]
|
||||||
|
|
||||||
|
|
||||||
_Token = collections.namedtuple("_Token", ("tp", "src"))
|
class _Token(NamedTuple):
|
||||||
|
tp: str
|
||||||
|
src: str
|
||||||
|
|
||||||
|
|
||||||
_CODE, _FILE, _COLON, _COMMA, _WS = "code", "file", "colon", "comma", "ws"
|
_CODE, _FILE, _COLON, _COMMA, _WS = "code", "file", "colon", "comma", "ws"
|
||||||
_EOF = "eof"
|
_EOF = "eof"
|
||||||
_FILE_LIST_TOKEN_TYPES = [
|
_FILE_LIST_TOKEN_TYPES = [
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ def test_handle_error_does_not_raise_type_errors():
|
||||||
)
|
)
|
||||||
|
|
||||||
assert 1 == guide.handle_error(
|
assert 1 == guide.handle_error(
|
||||||
"T111", "file.py", 1, None, "error found", "a = 1"
|
"T111", "file.py", 1, 1, "error found", "a = 1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue