Skip to content

Commit

Permalink
stubtest: Add a --ignore-positional-only argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Feb 2, 2024
1 parent 3f58c2d commit aef90a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/source/stubtest.rst
Expand Up @@ -116,6 +116,10 @@ The rest of this section documents the command line interface of stubtest.

Ignore errors for whether an argument should or shouldn't be positional-only

.. option:: --ignore-keyword-only

Ignore errors for whether an argument should or shouldn't be keyword-only

.. option:: --allowlist FILE

Use file as an allowlist. Can be passed multiple times to combine multiple
Expand Down
12 changes: 12 additions & 0 deletions mypy/stubtest.py
Expand Up @@ -135,6 +135,10 @@ def is_positional_only_related(self) -> bool:
# TODO: This is hacky, use error codes or something more resilient
return "leading double underscore" in self.message

def is_keyword_only_related(self) -> bool:
"""Whether or not the error is for something being (or not being) keyword-only."""
return "is not keyword-only" in self.message

def get_description(self, concise: bool = False) -> str:
"""Returns a description of the error.
Expand Down Expand Up @@ -1864,6 +1868,7 @@ class _Arguments:
concise: bool
ignore_missing_stub: bool
ignore_positional_only: bool
ignore_keyword_only: bool
allowlist: list[str]
generate_allowlist: bool
ignore_unused_allowlist: bool
Expand Down Expand Up @@ -1940,6 +1945,8 @@ def set_strict_flags() -> None: # not needed yet
continue
if args.ignore_positional_only and error.is_positional_only_related():
continue
if args.ignore_keyword_only and error.is_keyword_only_related():
continue
if error.object_desc in allowlist:
allowlist[error.object_desc] = True
continue
Expand Down Expand Up @@ -2017,6 +2024,11 @@ def parse_options(args: list[str]) -> _Arguments:
action="store_true",
help="Ignore errors for whether an argument should or shouldn't be positional-only",
)
parser.add_argument(
"--ignore-keyword-only",
action="store_true",
help="Ignore errors for whether an argument should or shouldn't be keyword-only",
)
parser.add_argument(
"--allowlist",
"--whitelist",
Expand Down
5 changes: 5 additions & 0 deletions mypy/test/teststubtest.py
Expand Up @@ -2263,6 +2263,11 @@ def test_ignore_flags(self) -> None:
)
assert output == "Success: no issues found in 1 module\n"

output = run_stubtest(
stub="def f(*, a): ...", runtime="def f(a): pass", options=["--ignore-keyword-only"],
)
assert output == "Success: no issues found in 1 module\n"

def test_allowlist(self) -> None:
# Can't use this as a context because Windows
allowlist = tempfile.NamedTemporaryFile(mode="w+", delete=False)
Expand Down

0 comments on commit aef90a3

Please sign in to comment.