Skip to content

[flake8-pyi] Implement redundant-none-literal (PYI061)#14316

Merged
charliermarsh merged 9 commits into
astral-sh:mainfrom
sbrugman:redundant-none-literal
Nov 16, 2024
Merged

[flake8-pyi] Implement redundant-none-literal (PYI061)#14316
charliermarsh merged 9 commits into
astral-sh:mainfrom
sbrugman:redundant-none-literal

Conversation

@sbrugman

@sbrugman sbrugman commented Nov 13, 2024

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Nov 13, 2024

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+14 -0 violations, +0 -0 fixes in 4 projects; 50 projects unchanged)

apache/airflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ airflow/models/dag.py:1038:36: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`

latchbio/latch (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/latch/types/metadata.py:500:45: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`

pandas-dev/pandas (+4 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ pandas/core/groupby/groupby.py:4069:39: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ pandas/core/groupby/indexing.py:299:39: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ pandas/io/html.py:1027:28: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ pandas/io/html.py:223:32: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`

zulip/zulip (+8 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

+ corporate/views/billing_page.py:326:24: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:158:26: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:159:42: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:160:48: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:678:26: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:679:42: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:680:48: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
+ corporate/views/remote_billing_page.py:70:5: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PYI061 14 14 0 0 0

@sbrugman sbrugman marked this pull request as draft November 13, 2024 13:51
@sbrugman sbrugman marked this pull request as ready for review November 13, 2024 14:25
@MichaReiser

Copy link
Copy Markdown
Member

Hi @sbrugman Thanks for another PR. It would help me review your PRs and avoid unnecessary work if you could fill out the Test plan. E.g. did you review the ecosystem changes already? If so, then I don't have to do so :)

@MichaReiser

MichaReiser commented Nov 13, 2024

Copy link
Copy Markdown
Member

Is there some previous issue where we discussed this rule? I'm asking because the rule itself would be a better fit for flake8-annotations... except that it isn't an upstream rule

@sbrugman

Copy link
Copy Markdown
Contributor Author

@MichaReiser I've updated the test plan above, will do so for the other issues as well.

You're right that these rules fit better in another category, but since there is no upstream rule I've placed them here.
I don't think we should hold back on contributing new rules, especially with the new categorisation upcoming.

I'm ok moving the rules if preferred.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer preview Related to preview mode features labels Nov 13, 2024
@sbrugman

Copy link
Copy Markdown
Contributor Author

It looks like this could actually be in flake8-pyi:
https://github.com/PyCQA/flake8-pyi/blob/main/ERRORCODES.md#Y061

@sbrugman sbrugman marked this pull request as draft November 14, 2024 07:32
@sbrugman sbrugman changed the title [ruff] Implement redundant-none-literal (RUF037) [flake8-pyi] Implement redundant-none-literal (PYI061) Nov 14, 2024
@sbrugman sbrugman force-pushed the redundant-none-literal branch from 9f4cad0 to 581f0da Compare November 14, 2024 07:44
@sbrugman sbrugman marked this pull request as ready for review November 14, 2024 07:45
@MichaReiser MichaReiser removed the needs-decision Awaiting a decision from a maintainer label Nov 14, 2024

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, @AlexWaygood could you sign off the rule :)

Comment on lines +96 to +98
if let Some(ref fix) = fix {
diagnostic.set_fix(fix.clone());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm surprised that setting the same fix works 😆

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! A couple of nitpicks, but looks great overall

Comment thread crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_none_literal.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_none_literal.rs Outdated
Comment thread crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI061.py
sbrugman and others added 2 commits November 14, 2024 15:54
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! Looks great, just a couple of nits about the test remaining

Comment thread crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI061.pyi Outdated
Comment thread crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI061.pyi Outdated
@charliermarsh charliermarsh enabled auto-merge (squash) November 16, 2024 18:11
@charliermarsh charliermarsh merged commit 78210b1 into astral-sh:main Nov 16, 2024
dylwil3 pushed a commit to dylwil3/ruff that referenced this pull request Nov 17, 2024
…sh#14316)

## Summary

`Literal[None]` can be simplified into `None` in type annotations.

Surprising to see that this is not that rare:
-
https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chat_models/base.py#L54
-
https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/sql/annotation.py#L69
- https://github.com/jax-ml/jax/blob/main/jax/numpy/__init__.pyi#L961
-
https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/inference/_common.py#L179

## Test Plan

`cargo test`

Reviewed all ecosystem results, and they are true positives.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
@sbrugman sbrugman deleted the redundant-none-literal branch November 18, 2024 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants