Merge pull request #1511 from PyCQA/new_namedtuple

use typesafe NamedTuple
This commit is contained in:
Anthony Sottile 2022-01-05 13:24:37 -05:00 committed by GitHub
commit f6267dd4d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 22 deletions

View file

@ -1,8 +1,8 @@
"""Statistic collection logic for Flake8."""
import collections
from typing import Dict
from typing import Generator
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import TYPE_CHECKING
@ -73,7 +73,7 @@ class Statistics:
yield self._store[error_code]
class Key(collections.namedtuple("Key", ["filename", "code"])):
class Key(NamedTuple):
"""Simple key structure for the Statistics dictionary.
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.
"""
__slots__ = ()
filename: str
code: str
@classmethod
def create_from(cls, error: "Violation") -> "Key":
@ -111,7 +112,7 @@ class Statistic:
"""Simple wrapper around the logic of each statistic.
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.
"""

View file

@ -1,6 +1,5 @@
"""Implementation of the StyleGuide used by Flake8."""
import argparse
import collections
import contextlib
import copy
import enum
@ -12,6 +11,7 @@ from typing import Dict
from typing import Generator
from typing import List
from typing import Match
from typing import NamedTuple
from typing import Optional
from typing import Sequence
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)
class Violation(
collections.namedtuple(
"Violation",
[
"code",
"filename",
"line_number",
"column_number",
"text",
"physical_line",
],
)
):
class Violation(NamedTuple):
"""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:
"""Determine if a comment has been added to ignore this line.
@ -394,7 +389,7 @@ class StyleGuideManager:
code: str,
filename: str,
line_number: int,
column_number: Optional[int],
column_number: int,
text: str,
physical_line: Optional[str] = None,
) -> int:
@ -527,7 +522,7 @@ class StyleGuide:
code: str,
filename: str,
line_number: int,
column_number: Optional[int],
column_number: int,
text: str,
physical_line: Optional[str] = None,
) -> int:

View file

@ -12,6 +12,7 @@ import textwrap
import tokenize
from typing import Dict
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Pattern
from typing import Sequence
@ -50,7 +51,11 @@ def parse_comma_separated_list(
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"
_EOF = "eof"
_FILE_LIST_TOKEN_TYPES = [

View file

@ -34,7 +34,7 @@ def test_handle_error_does_not_raise_type_errors():
)
assert 1 == guide.handle_error(
"T111", "file.py", 1, None, "error found", "a = 1"
"T111", "file.py", 1, 1, "error found", "a = 1"
)