Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for unnecessary-default-type-args #9938

Merged

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Sep 20, 2024

Type of Changes

Type
✨ New feature

Description

Starting with Python 3.13, typing.Generator and typing.AsyncGenerator have default type argument. E.g. it's no longer necessary to specify None, None for the send and return type.

# before
a1: Generator[int, None, None]

# after
a1: Generator[int]

This check is similar to the one in ruff but extends on it to also cover the collections.abc` variants which can also be used with older Python versions.

https://docs.astral.sh/ruff/rules/unnecessary-default-type-args/
https://github.com/asottile/pyupgrade?tab=readme-ov-file#pep-696-typevar-defaults

@cdce8p cdce8p added typing Optional Checkers Related to a checked, disabled by default labels Sep 20, 2024
@cdce8p cdce8p added this to the 3.3.0 milestone Sep 20, 2024
@cdce8p cdce8p added Enhancement ✨ Improvement to a component python 3.13 labels Sep 20, 2024
@cdce8p cdce8p force-pushed the check-unnecessary-default-type-args branch from 9641ce2 to f25fda9 Compare September 20, 2024 10:19
@cdce8p cdce8p force-pushed the check-unnecessary-default-type-args branch from f25fda9 to ef87e5e Compare September 20, 2024 10:26
Copy link

codecov bot commented Sep 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.80%. Comparing base (7aa4436) to head (d0b84f5).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #9938   +/-   ##
=======================================
  Coverage   95.80%   95.80%           
=======================================
  Files         174      174           
  Lines       18933    18940    +7     
=======================================
+ Hits        18139    18146    +7     
  Misses        794      794           
Files with missing lines Coverage Δ
pylint/checkers/exceptions.py 98.29% <ø> (ø)
pylint/checkers/symilar.py 96.31% <100.00%> (ø)
pylint/checkers/variables.py 97.23% <100.00%> (ø)
pylint/extensions/typing.py 97.76% <100.00%> (+0.09%) ⬆️
pylint/pyreverse/diadefslib.py 100.00% <ø> (ø)
pylint/testutils/checker_test_case.py 97.82% <ø> (ø)
pylint/testutils/utils.py 98.30% <ø> (ø)
pylint/utils/pragma_parser.py 98.24% <100.00%> (ø)

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great ! I'm glad we did not release 3.3.0 asap after the merge of #9853, this is a nice check to have in the first python 3.13 release !

This comment has been minimized.

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on astroid:
The following messages are now emitted:

  1. unnecessary-default-type-args:
    Type Generator[InferenceResult, None, None] has unnecessary default type args. Change it to Generator[InferenceResult].
    https://github.com/pylint-dev/astroid/blob/5a93a9f9e7e8d1cbcbfc93783e86572b6c678152/astroid/typing.py#L79
  2. unnecessary-default-type-args:
    Type collections.abc.Generator[InferenceResult, None, None] has unnecessary default type args. Change it to collections.abc.Generator[InferenceResult].
    https://github.com/pylint-dev/astroid/blob/5a93a9f9e7e8d1cbcbfc93783e86572b6c678152/astroid/bases.py#L150
  3. unnecessary-default-type-args:
    Type collections.abc.Generator[nodes.Const | Instance | UninferableBase, None, None] has unnecessary default type args. Change it to collections.abc.Generator[nodes.Const | Instance | UninferableBase].
    https://github.com/pylint-dev/astroid/blob/5a93a9f9e7e8d1cbcbfc93783e86572b6c678152/astroid/bases.py#L494
  4. unnecessary-default-type-args:
    Type Generator[Union[InferenceResult, _BadOpMessageT], None, None] has unnecessary default type args. Change it to Generator[Union[InferenceResult, _BadOpMessageT]].
    https://github.com/pylint-dev/astroid/blob/5a93a9f9e7e8d1cbcbfc93783e86572b6c678152/astroid/nodes/node_classes.py#L85
  5. unnecessary-default-type-args:
    Type Generator[InferenceResult, None, None] has unnecessary default type args. Change it to Generator[InferenceResult].
    https://github.com/pylint-dev/astroid/blob/5a93a9f9e7e8d1cbcbfc93783e86572b6c678152/astroid/nodes/_base_nodes.py#L45

Effect on music21:
The following messages are now emitted:

  1. unnecessary-default-type-args:
    Type Generator[stream.Stream, None, None] has unnecessary default type args. Change it to Generator[stream.Stream].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/sites.py#L397
  2. unnecessary-default-type-args:
    Type Generator[stream.Stream | None, None, None] has unnecessary default type args. Change it to Generator[stream.Stream | None].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/sites.py#L407
  3. unnecessary-default-type-args:
    Type Generator[stream.Stream | None, None, None] has unnecessary default type args. Change it to Generator[stream.Stream | None].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/sites.py#L415
  4. unnecessary-default-type-args:
    Type Generator[VoiceLeadingQuartet, None, None] has unnecessary default type args. Change it to Generator[VoiceLeadingQuartet].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/voiceLeading.py#L2419
  5. unnecessary-default-type-args:
    Type Generator[ContextSortTuple, None, None] has unnecessary default type args. Change it to Generator[ContextSortTuple].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/base.py#L1790
  6. unnecessary-default-type-args:
    Type Generator[ContextTuple, None, None] has unnecessary default type args. Change it to Generator[ContextTuple].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/base.py#L1805
  7. unnecessary-default-type-args:
    Type Generator[ContextTuple | ContextSortTuple, None, None] has unnecessary default type args. Change it to Generator[ContextTuple | ContextSortTuple].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/base.py#L1820
  8. unnecessary-default-type-args:
    Type Generator[base.Music21Object, None, None] has unnecessary default type args. Change it to Generator[base.Music21Object].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/derivation.py#L217
  9. unnecessary-default-type-args:
    Type Generator[list[note.NotRest], None, None] has unnecessary default type args. Change it to Generator[list[note.NotRest]].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/stream/makeNotation.py#L1804
  10. unnecessary-default-type-args:
    Type Generator[Verticality, None, None] has unnecessary default type args. Change it to Generator[Verticality].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/tree/timespanTree.py#L457
  11. unnecessary-default-type-args:
    Type Generator[VerticalitySequence, None, None] has unnecessary default type args. Change it to Generator[VerticalitySequence].
    https://github.com/cuthbertLab/music21/blob/e05fc53dfef7b2c9ac974c0cacb8b85e9c4d4605/music21/tree/timespanTree.py#L522

Effect on pandas:
The following messages are now emitted:

  1. unnecessary-default-type-args:
    Type Generator[Series, None, None] has unnecessary default type args. Change it to Generator[Series].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/apply.py#L809
  2. unnecessary-default-type-args:
    Type Generator[Series, None, None] has unnecessary default type args. Change it to Generator[Series].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/apply.py#L1134
  3. unnecessary-default-type-args:
    Type Generator[Series, None, None] has unnecessary default type args. Change it to Generator[Series].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/apply.py#L1246
  4. unnecessary-default-type-args:
    Type abc.Generator[bool, None, None] has unnecessary default type args. Change it to abc.Generator[bool].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/common.py#L338
  5. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/common.py#L565
  6. unnecessary-default-type-args:
    Type Generator[list, None, None] has unnecessary default type args. Change it to Generator[list].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/methods/to_dict.py#L36
  7. unnecessary-default-type-args:
    Type Generator[np.ndarray, None, None] has unnecessary default type args. Change it to Generator[np.ndarray].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/indexes/multi.py#L2667
  8. unnecessary-default-type-args:
    Type Generator[Index, None, None] has unnecessary default type args. Change it to Generator[Index].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/groupby/ops.py#L868
  9. unnecessary-default-type-args:
    Type Generator[Block, None, None] has unnecessary default type args. Change it to Generator[Block].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/internals/blocks.py#L391
  10. unnecessary-default-type-args:
    Type Generator[tuple[BlockPlacement, list[JoinUnit]], None, None] has unnecessary default type args. Change it to Generator[tuple[BlockPlacement, list[JoinUnit]]].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/internals/concat.py#L253
  11. unnecessary-default-type-args:
    Type Generator[Block, None, None] has unnecessary default type args. Change it to Generator[Block].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/internals/managers.py#L859
  12. unnecessary-default-type-args:
    Type Generator[tuple[str, Self], None, None] has unnecessary default type args. Change it to Generator[tuple[str, Self]].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/internals/managers.py#L1734
  13. unnecessary-default-type-args:
    Type Generator[Timestamp, None, None] has unnecessary default type args. Change it to Generator[Timestamp].
    https://github.com/pandas-dev/pandas/blob/f1e6cc184ae0534e11c0a2947f4948bc4c5e0a9d/pandas/core/arrays/datetimes.py#L2917

Effect on sentry:
The following messages are now emitted:

  1. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/runner/commands/repair.py#L17
  2. unnecessary-default-type-args:
    Type Generator[Any, None, None] has unnecessary default type args. Change it to Generator[Any].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/auth/services/auth/model.py#L145
  3. unnecessary-default-type-args:
    Type Generator[list[T], None, None] has unnecessary default type args. Change it to Generator[list[T]].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/utils/iterators.py#L31
  4. unnecessary-default-type-args:
    Type Generator[MutableTags, None, None] has unnecessary default type args. Change it to Generator[MutableTags].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/utils/metrics.py#L197
  5. unnecessary-default-type-args:
    Type Generator[T, None, None] has unnecessary default type args. Change it to Generator[T].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/consumers/synchronized.py#L41
  6. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/testutils/helpers/pagination.py#L9
  7. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/testutils/helpers/redis.py#L25
  8. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/testutils/helpers/features.py#L154
  9. unnecessary-default-type-args:
    Type Generator[CallbackFuture, None, None] has unnecessary default type args. Change it to Generator[CallbackFuture].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/rules/actions/notify_event.py#L32
  10. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/api/endpoints/organization_traces.py#L97
  11. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/eventstream/kafka/dispatch.py#L27
  12. unnecessary-default-type-args:
    Type Generator[None, None, None] has unnecessary default type args. Change it to Generator[None].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/metrics/middleware.py#L84
  13. unnecessary-default-type-args:
    Type Generator[ServiceMemory, None, None] has unnecessary default type args. Change it to Generator[ServiceMemory].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/processing/backpressure/memory.py#L54
  14. unnecessary-default-type-args:
    Type Generator[ServiceMemory, None, None] has unnecessary default type args. Change it to Generator[ServiceMemory].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/processing/backpressure/monitor.py#L39
  15. unnecessary-default-type-args:
    Type Generator[CodeLocationQuery, None, None] has unnecessary default type args. Change it to Generator[CodeLocationQuery].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/sentry_metrics/querying/metadata/metrics_code_locations.py#L97
  16. unnecessary-default-type-args:
    Type Generator[CodeLocationQuery, None, None] has unnecessary default type args. Change it to Generator[CodeLocationQuery].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/sentry_metrics/querying/metadata/metrics_code_locations.py#L184
  17. unnecessary-default-type-args:
    Type Generator[Sequence[CodeLocationQuery], None, None] has unnecessary default type args. Change it to Generator[Sequence[CodeLocationQuery]].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/sentry_metrics/querying/metadata/metrics_code_locations.py#L185
  18. unnecessary-default-type-args:
    Type Generator[tuple[QueryExpression, QueryOrder | None, int | None], None, None] has unnecessary default type args. Change it to Generator[tuple[QueryExpression, QueryOrder | None, int | None]].
    https://github.com/getsentry/sentry/blob/0c446e45c7cbcf2419d8d5d6cee7425c181e10ed/src/sentry/sentry_metrics/querying/data/parsing.py#L64

This comment was generated for commit d0b84f5

@Pierre-Sassoulas Pierre-Sassoulas merged commit b28c1f6 into pylint-dev:main Sep 20, 2024
44 checks passed
@cdce8p cdce8p deleted the check-unnecessary-default-type-args branch September 20, 2024 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Optional Checkers Related to a checked, disabled by default python 3.13 typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants