summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2022-01-05 13:02:38 -0500
committerAnthony Sottile <asottile@umich.edu>2022-01-05 13:02:38 -0500
commit3c885219b56ec5755c44d1649be973ae206806de (patch)
tree3cae12eaebc3dc0d4049a8a5d0871fc204b2d70a
parentf0fb7868832486091e26072e53ead5e50bdf3e64 (diff)
downloadflake8-new_namedtuple.tar.gz
use typesafe NamedTuplenew_namedtuple
-rw-r--r--src/flake8/statistics.py9
-rw-r--r--src/flake8/style_guide.py27
-rw-r--r--src/flake8/utils.py7
-rw-r--r--tests/unit/test_style_guide.py2
4 files changed, 23 insertions, 22 deletions
diff --git a/src/flake8/statistics.py b/src/flake8/statistics.py
index 073bfe4..ad93c8f 100644
--- a/src/flake8/statistics.py
+++ b/src/flake8/statistics.py
@@ -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.
"""
diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py
index c471fa6..9d45c79 100644
--- a/src/flake8/style_guide.py
+++ b/src/flake8/style_guide.py
@@ -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:
diff --git a/src/flake8/utils.py b/src/flake8/utils.py
index bbc89a1..2506266 100644
--- a/src/flake8/utils.py
+++ b/src/flake8/utils.py
@@ -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 = [
diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py
index da28355..e4aaff2 100644
--- a/tests/unit/test_style_guide.py
+++ b/tests/unit/test_style_guide.py
@@ -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"
)