Skip to content

Commit e3d48a5

Browse files
committed
Mypy: static type checker for Python
1 parent 0f95a9a commit e3d48a5

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

cpplint.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
same line, but it is far from perfect (in either direction).
4242
"""
4343

44+
from __future__ import annotations
45+
4446
import codecs
4547
import collections
4648
import copy
@@ -57,7 +59,7 @@
5759
import xml.etree.ElementTree
5860

5961
# if empty, use defaults
60-
_valid_extensions = set()
62+
_valid_extensions: set[str] = set()
6163

6264
__VERSION__ = "2.0.1"
6365

@@ -830,7 +832,7 @@
830832
]
831833

832834
# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
833-
_CHECK_REPLACEMENT = {macro_var: {} for macro_var in _CHECK_MACROS}
835+
_CHECK_REPLACEMENT: dict[str, dict[str, str]] = {macro_var: {} for macro_var in _CHECK_MACROS}
834836

835837
for op, replacement in [
836838
("==", "EQ"),
@@ -934,7 +936,7 @@
934936

935937
# {str, set(int)}: a map from error categories to sets of linenumbers
936938
# on which those errors are expected and should be suppressed.
937-
_error_suppressions = {}
939+
_error_suppressions: dict[str, set[int]] = {}
938940

939941
# The root directory used for deriving header guard CPP variable.
940942
# This is set by --root flag.
@@ -964,7 +966,7 @@
964966

965967
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
966968
# This is set by --headers flag.
967-
_hpp_headers = set()
969+
_hpp_headers: set[str] = set()
968970

969971

970972
class ErrorSuppressions:
@@ -1034,7 +1036,7 @@ def Clear(self):
10341036
self._open_block_suppression = None
10351037

10361038

1037-
_error_suppressions = ErrorSuppressions()
1039+
_error_suppressions = ErrorSuppressions() # type: ignore[assignment]
10381040

10391041

10401042
def ProcessHppHeadersOption(val):
@@ -6609,7 +6611,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
66096611
)
66106612

66116613

6612-
_HEADERS_CONTAINING_TEMPLATES = (
6614+
_HEADERS_CONTAINING_TEMPLATES: tuple[tuple[str, tuple[str, ...]], ...] = (
66136615
("<deque>", ("deque",)),
66146616
(
66156617
"<functional>",
@@ -6705,7 +6707,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
67056707
("<slist>", ("slist",)),
67066708
)
67076709

6708-
_HEADERS_MAYBE_TEMPLATES = (
6710+
_HEADERS_MAYBE_TEMPLATES: tuple[tuple[str, tuple[str, ...]], ...] = (
67096711
(
67106712
"<algorithm>",
67116713
(
@@ -6721,15 +6723,15 @@ def ExpectingFunctionArgs(clean_lines, linenum):
67216723
)
67226724

67236725
# Non templated types or global objects
6724-
_HEADERS_TYPES_OR_OBJS = (
6726+
_HEADERS_TYPES_OR_OBJS: tuple[tuple[str, tuple[str, ...]], ...] = (
67256727
# String and others are special -- it is a non-templatized type in STL.
67266728
("<string>", ("string",)),
67276729
("<iostream>", ("cin", "cout", "cerr", "clog", "wcin", "wcout", "wcerr", "wclog")),
67286730
("<cstdio>", ("FILE", "fpos_t")),
67296731
)
67306732

67316733
# Non templated functions
6732-
_HEADERS_FUNCTIONS = (
6734+
_HEADERS_FUNCTIONS: tuple[tuple[str, tuple[str, ...]], ...] = (
67336735
(
67346736
"<cstdio>",
67356737
(
@@ -6780,7 +6782,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
67806782
),
67816783
)
67826784

6783-
_re_pattern_headers_maybe_templates = []
6785+
_re_pattern_headers_maybe_templates: list[tuple[re.Pattern, str, str]] = []
67846786
for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
67856787
# Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
67866788
# 'type::max()'.
@@ -6796,7 +6798,7 @@ def ExpectingFunctionArgs(clean_lines, linenum):
67966798
)
67976799

67986800
# Other scripts may reach in and modify this pattern.
6799-
_re_pattern_templates = []
6801+
_re_pattern_templates: list[tuple[re.Pattern, str, str]] = []
68006802
for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
68016803
_re_pattern_templates.extend(
68026804
(
@@ -6807,14 +6809,14 @@ def ExpectingFunctionArgs(clean_lines, linenum):
68076809
for _template in _templates
68086810
)
68096811

6810-
_re_pattern_types_or_objs = []
6812+
_re_pattern_types_or_objs: list[tuple[re.Pattern, object | type, str]] = []
68116813
for _header, _types_or_objs in _HEADERS_TYPES_OR_OBJS:
68126814
_re_pattern_types_or_objs.extend(
68136815
(re.compile(r"\b" + _type_or_obj + r"\b"), _type_or_obj, _header)
68146816
for _type_or_obj in _types_or_objs
68156817
)
68166818

6817-
_re_pattern_functions = []
6819+
_re_pattern_functions: list[tuple[re.Pattern, str, str]] = []
68186820
for _header, _functions in _HEADERS_FUNCTIONS:
68196821
# Match printf(..., ...), but not foo->printf, foo.printf or
68206822
# 'type::printf()'.

cpplint_clitest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
"""Command Line interface integration test for cpplint.py."""
3232

33+
from __future__ import annotations
34+
3335
import contextlib
3436
import glob
3537
import os
@@ -39,23 +41,23 @@
3941
import tempfile
4042

4143
import pytest
42-
from parameterized import parameterized
43-
from testfixtures import compare
44+
from parameterized import parameterized # type: ignore[import-untyped]
45+
from testfixtures import compare # type: ignore[import-untyped]
4446

4547
import cpplint # noqa: F401
4648

4749
BASE_CMD = sys.executable + " " + os.path.abspath("./cpplint.py ")
4850

4951

50-
def run_shell_command(cmd: str, args: str, cwd="."):
52+
def run_shell_command(cmd: str, args: str, cwd: str = ".") -> tuple[int, bytes, bytes]:
5153
"""Executes a command
5254
5355
Args:
5456
cmd: A string to execute.
5557
args: A string with arguments to the command.
5658
cwd: from which folder to run.
5759
"""
58-
cmd, args = cmd.split(), args.split()
60+
cmd, args = cmd.split(), args.split() # type: ignore[assignment]
5961
proc = subprocess.run(cmd + args, cwd=cwd, capture_output=True, check=False)
6062
out, err = proc.stdout, proc.stderr
6163

cpplint_unittest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
# TODO(unknown): Add a good test that tests UpdateIncludeState.
3434

35+
from __future__ import annotations
36+
3537
import codecs
3638
import os
3739
import platform
@@ -43,7 +45,7 @@
4345
import tempfile
4446

4547
import pytest
46-
from parameterized import parameterized
48+
from parameterized import parameterized # type: ignore[import-untyped]
4749

4850
import cpplint
4951

@@ -58,7 +60,7 @@ def codecs_latin_encode(x):
5860
class ErrorCollector:
5961
# These are a global list, covering all categories seen ever.
6062
_ERROR_CATEGORIES = cpplint._ERROR_CATEGORIES
61-
_SEEN_ERROR_CATEGORIES = {}
63+
_SEEN_ERROR_CATEGORIES: dict[str, str] = {}
6264

6365
def __init__(self, assert_fn):
6466
"""assert_fn: a function to call when we notice a problem."""

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dynamic = [ "version" ]
4141
dependencies = [ ]
4242

4343
optional-dependencies.dev = [
44+
"mypy",
4445
"parameterized",
4546
"pylint>=2.11",
4647
"pytest",

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
envlist = py38, py39, py3.10, py311, py312, py313, pypy3
33
skip_missing_interpreters = true
44

5+
56
[testenv]
7+
allowlist_externals = sh
68
extras = dev
79

810
commands =
911
{envpython} -m pytest {posargs:}
1012
{envpython} -m pylint cpplint.py
13+
sh -c 'mypy *.py'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy