Skip to content

Commit b28c1f6

Browse files
Add check for unnecessary-default-type-args (#9938)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent bd97b93 commit b28c1f6

23 files changed

+119
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from collections.abc import AsyncGenerator, Generator
2+
3+
a1: AsyncGenerator[int, None] # [unnecessary-default-type-args]
4+
b1: Generator[int, None, None] # [unnecessary-default-type-args]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
At the moment, this check only works for ``Generator`` and ``AsyncGenerator``.
2+
3+
Starting with Python 3.13, the ``SendType`` and ``ReturnType`` default to ``None``.
4+
As such it's no longer necessary to specify them. The ``collections.abc`` variants
5+
don't validate the number of type arguments. Therefore the defaults for these
6+
can be used in earlier versions as well.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from collections.abc import AsyncGenerator, Generator
2+
3+
a1: AsyncGenerator[int]
4+
b1: Generator[int]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[main]
2+
load-plugins=pylint.extensions.typing
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `Python documentation for AsyncGenerator <https://docs.python.org/3.13/library/typing.html#typing.AsyncGenerator>`_
2+
- `Python documentation for Generator <https://docs.python.org/3.13/library/typing.html#typing.Generator>`_

doc/user_guide/checkers/extensions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ Typing checker Messages
688688
:consider-alternative-union-syntax (R6003): *Consider using alternative Union syntax instead of '%s'%s*
689689
Emitted when 'typing.Union' or 'typing.Optional' is used instead of the
690690
alternative Union syntax 'int | None'.
691+
:unnecessary-default-type-args (R6007): *Type `%s` has unnecessary default type args. Change it to `%s`.*
692+
Emitted when types have default type args which can be omitted. Mainly used
693+
for `typing.Generator` and `typing.AsyncGenerator`.
691694
:redundant-typehint-argument (R6006): *Type `%s` is used more than once in union type annotation. Remove redundant typehints.*
692695
Duplicated type arguments will be skipped by `mypy` tool, therefore should be
693696
removed to avoid confusion.

doc/user_guide/messages/messages_overview.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ All messages in the refactor category:
545545
refactor/too-many-statements
546546
refactor/trailing-comma-tuple
547547
refactor/unnecessary-comprehension
548+
refactor/unnecessary-default-type-args
548549
refactor/unnecessary-dict-index-lookup
549550
refactor/unnecessary-list-index-lookup
550551
refactor/use-a-generator

doc/whatsnew/fragments/9938.new_check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add ``unnecessary-default-type-args`` to the ``typing`` extension to detect the use
2+
of unnecessary default type args for ``typing.Generator`` and ``typing.AsyncGenerator``.
3+
4+
Refs #9938

pylint/checkers/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def predicate(obj: Any) -> bool:
3535

3636
def _annotated_unpack_infer(
3737
stmt: nodes.NodeNG, context: InferenceContext | None = None
38-
) -> Generator[tuple[nodes.NodeNG, SuccessfulInferenceResult], None, None]:
38+
) -> Generator[tuple[nodes.NodeNG, SuccessfulInferenceResult]]:
3939
"""Recursively generate nodes inferred by the given statement.
4040
4141
If the inferred value is a list or a tuple, recurse on the elements.

pylint/checkers/symilar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def _get_similarity_report(
468468
# pylint: disable = too-many-locals
469469
def _find_common(
470470
self, lineset1: LineSet, lineset2: LineSet
471-
) -> Generator[Commonality, None, None]:
471+
) -> Generator[Commonality]:
472472
"""Find similarities in the two given linesets.
473473
474474
This the core of the algorithm. The idea is to compute the hashes of a
@@ -541,7 +541,7 @@ def _find_common(
541541
if eff_cmn_nb > self.namespace.min_similarity_lines:
542542
yield com
543543

544-
def _iter_sims(self) -> Generator[Commonality, None, None]:
544+
def _iter_sims(self) -> Generator[Commonality]:
545545
"""Iterate on similarities among all files, by making a Cartesian
546546
product.
547547
"""

0 commit comments

Comments
 (0)