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

[flake8-type-checking] Adds implementation for TC006 #14511

Merged
merged 5 commits into from
Nov 22, 2024

Conversation

Daverball
Copy link
Contributor

@Daverball Daverball commented Nov 21, 2024

Now that TCH has been renamed to TC and the redirect from TCH006 to TCH010 can no longer bite us, I've added an implementation for this very simple rule.

Summary

TC006 checks for non-string literal arguments to typing.cast which adds unnecessary runtime overhead, since the function doesn't do anything at runtime.

This PR has a very tiny overlap with my other PR for TCH007/TCH008 in flake8_type_checking/helpers.rs for adding the quote_type_expression function, but the two changes don't really depend on one another, so they can be merged in any order.

Test Plan

cargo nextest run

Unverified

This user has not yet uploaded their public signing key.
Copy link
Contributor

github-actions bot commented Nov 21, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+610 -1 violations, +0 -0 fixes in 11 projects; 43 projects unchanged)

DisnakeDev/disnake (+11 -0 violations, +0 -0 fixes)

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

+ disnake/abc.py:1249:25: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/abc.py:299:25: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/components.py:755:27: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/ext/commands/base_core.py:196:43: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/ext/commands/core.py:1950:19: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/ext/commands/core.py:1980:19: TC006 [*] Add quotes to type expression in `typing.cast()`
+ disnake/interactions/base.py:197:37: TC006 [*] Add quotes to type expression in `typing.cast()`
+ docs/extensions/fulltoc.py:122:34: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_abc.py:165:30: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_abc.py:166:30: TC006 [*] Add quotes to type expression in `typing.cast()`
... 1 additional changes omitted for project

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

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

+ airflow/api/auth/backend/deny_all.py:44:17: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/endpoints/request_dict.py:26:17: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/parameters.py:106:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:107:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:156:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:175:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:194:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:213:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:229:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:250:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_connexion/security.py:88:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/api_fastapi/core_api/app.py:86:29: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/cli/commands/task_command.py:596:25: TC006 [*] Add quotes to type expression in `typing.cast()`
+ airflow/dag_processing/manager.py:1172:34: TC006 [*] Add quotes to type expression in `typing.cast()`
... 162 additional changes omitted for project

apache/superset (+80 -0 violations, +0 -0 fixes)

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

+ superset/charts/api.py:588:22: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/charts/api.py:710:22: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/dashboard/filter_state/create.py:39:22: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/dashboard/filter_state/update.py:36:22: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/database/tables.py:136:28: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/distributed_lock/get.py:45:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/explore/get.py:116:26: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/explore/get.py:130:52: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/sql_lab/export.py:101:44: TC006 [*] Add quotes to type expression in `typing.cast()`
+ superset/commands/sql_lab/results.py:111:44: TC006 [*] Add quotes to type expression in `typing.cast()`
... 70 additional changes omitted for project

bokeh/bokeh (+60 -0 violations, +0 -0 fixes)

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

+ src/bokeh/core/serialization.py:428:26: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:537:50: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:539:53: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:541:53: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:543:52: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:545:50: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:547:50: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:549:52: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:551:52: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/bokeh/core/serialization.py:553:58: TC006 [*] Add quotes to type expression in `typing.cast()`
... 50 additional changes omitted for project

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

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

+ src/latch/registry/table.py:501:35: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch/registry/table.py:678:34: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch/registry/utils.py:38:26: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch/registry/utils.py:54:28: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch/registry/utils.py:64:22: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch_cli/services/register/utils.py:157:32: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/latch_cli/services/register/utils.py:159:24: TC006 [*] Add quotes to type expression in `typing.cast()`

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

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

+ pandas/_config/localization.py:159:89: E501 Line too long (90 > 88)
+ pandas/core/algorithms.py:27:5: TC001 Move application import `pandas._typing.AnyArrayLike` into a type-checking block
+ pandas/core/algorithms.py:28:5: TC001 Move application import `pandas._typing.ArrayLike` into a type-checking block
+ pandas/core/algorithms.py:29:5: TC001 Move application import `pandas._typing.ArrayLikeT` into a type-checking block
+ pandas/core/algorithms.py:30:5: TC001 Move application import `pandas._typing.AxisInt` into a type-checking block
+ pandas/core/algorithms.py:31:5: TC001 Move application import `pandas._typing.DtypeObj` into a type-checking block
+ pandas/core/algorithms.py:32:5: TC001 Move application import `pandas._typing.TakeIndexer` into a type-checking block
... 144 additional changes omitted for rule TC001
+ pandas/core/apply.py:5:29: TC003 Move standard library import `collections.abc.Callable` into a type-checking block
+ pandas/core/common.py:15:5: TC003 Move standard library import `collections.abc.Callable` into a type-checking block
+ pandas/core/common.py:16:5: TC003 Move standard library import `collections.abc.Collection` into a type-checking block
+ pandas/core/common.py:17:5: TC003 Move standard library import `collections.abc.Generator` into a type-checking block
+ pandas/core/common.py:18:5: TC003 Move standard library import `collections.abc.Hashable` into a type-checking block
+ pandas/core/common.py:19:5: TC003 Move standard library import `collections.abc.Iterable` into a type-checking block
... 21 additional changes omitted for rule TC003
+ pandas/core/indexes/api.py:8:17: TC002 Move third-party import `numpy` into a type-checking block
... 163 additional changes omitted for project

python-poetry/poetry (+1 -1 violations, +0 -0 fixes)

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

- src/poetry/inspection/lazy_wheel.py:278:41: SIM115 Use a context manager for opening files
+ src/poetry/inspection/lazy_wheel.py:278:43: SIM115 Use a context manager for opening files

rotki/rotki (+51 -0 violations, +0 -0 fixes)

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

+ rotkehlchen/accounting/history_base_entries.py:105:29: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/accounting/history_base_entries.py:109:34: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/api/rest.py:649:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/api/v1/schemas.py:2861:18: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/api/v1/schemas.py:2892:27: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/chain/aggregator.py:1488:41: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/chain/arbitrum_one/node_inquirer.py:63:31: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/chain/base/node_inquirer.py:63:31: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/chain/bitcoin/hdkey.py:283:36: TC006 [*] Add quotes to type expression in `typing.cast()`
+ rotkehlchen/chain/bitcoin/hdkey.py:284:27: TC006 [*] Add quotes to type expression in `typing.cast()`
... 41 additional changes omitted for project

scikit-build/scikit-build-core (+8 -0 violations, +0 -0 fixes)

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

+ src/scikit_build_core/hatch/plugin.py:50:29: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/scikit_build_core/settings/sources.py:642:29: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/scikit_build_core/settings/sources.py:643:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ src/scikit_build_core/settings/sources.py:644:21: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_builder.py:108:26: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_builder.py:128:26: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_builder.py:141:28: TC006 [*] Add quotes to type expression in `typing.cast()`
+ tests/test_pyproject_pep660.py:14:24: TC006 [*] Add quotes to type expression in `typing.cast()`

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (6 rules affected)

code total + violation - violation + fix - fix
TC006 431 431 0 0 0
TC001 149 149 0 0 0
TC003 27 27 0 0 0
SIM115 2 1 1 0 0
E501 1 1 0 0 0
TC002 1 1 0 0 0

@Daverball
Copy link
Contributor Author

Daverball commented Nov 21, 2024

The extra hits for the other rules make sense to me, since fixing TC006 can result in subsequent TC001-003 errors (or E501 for a very long annotation).

In the case of SIM115 the fix for TC006 causes the SIM115 violation to shift by two characters. But the actual violation doesn't change.

Unverified

This user has not yet uploaded their public signing key.
@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 22, 2024
@MichaReiser
Copy link
Member

Thanks for contributing this rule. The code changes look good to me.

I want to wait for updated ecosystem checks. I don't see why this PR should result in any new diagnostics other than TC006.

@MichaReiser
Copy link
Member

I'm a bit confused by the ecosystem checks. I understand that we run ruff exactly once and don't apply any fixes. That makes it unclear to me why there would be any changed diagnostics other than for TC006.

@Daverball
Copy link
Contributor Author

Daverball commented Nov 22, 2024

I'm a bit confused by the ecosystem checks. I understand that we run ruff exactly once and don't apply any fixes. That makes it unclear to me why there would be any changed diagnostics other than for TC006.

The only way this output makes sense to me is if it does in fact repeatedly apply fixes until it runs out of fixes and uses the output from whatever the last iteration was for the ecosystem results. It's also a little sus that there's no +/- on the count of fixes, since these are definitely all auto-fixable. Maybe this was changed at some point without anyone noticing? Because the fixes count was always zero on my other PRs as well, even though they definitely included some new fixes.

Daverball and others added 2 commits November 22, 2024 14:37

Unverified

This user has not yet uploaded their public signing key.
…e_cast_value.rs

Co-authored-by: Micha Reiser <[email protected]>

Unverified

This user has not yet uploaded their public signing key.
@MichaReiser
Copy link
Member

Something's definitely broken. Running the two ruff binaries without the ecosystem wrapper script gives me zero changes for pandas.

Unverified

This user has not yet uploaded their public signing key.
@Daverball
Copy link
Contributor Author

Daverball commented Nov 22, 2024

Something's definitely broken. Running the two ruff binaries without the ecosystem wrapper script gives me zero changes for pandas.

Indeed, I double checked, there's not even any type aliases defined in the first file in pandas. Maybe it's the result of broken caching on the CI side? I've noticed the ecosystem job has gotten significantly faster recently.

@MichaReiser
Copy link
Member

MichaReiser commented Nov 22, 2024

I can reproduce the ecosystem differences locally.... I'm sure it's something obvious and very stupid :D

@Daverball
Copy link
Contributor Author

Daverball commented Nov 22, 2024

I can reproduce the ecosystem differences locally.... I'm sure it's something obvious and very stupid :D

Actually I confused myself, this is about typing.cast and not type aliases. So it actually still makes sense. If you apply the fix for TC006, you will get the TC001 errors afterwards, because the file uses from __future__ import annotations, so the only runtime use of those _typing imports happened inside cast statements, so if you quote them, there are none left and TC001-003 triggers.

So I still think the ecosystem script applies fixes repeatedly and uses either the state where it can no longer fix things or the state just before it fixed everything for the results.

That also explains the poetry hit, since fixing TC006 causes the SIM115 violation to be moved two characters over, by the newly introduced quotes.

@MichaReiser
Copy link
Member

Actually I confused myself, this is about typing.cast and not type aliases. So it actually still makes sense. If you apply the fix for TC006, you will get the TC001 errors afterwards, because the file uses from future import annotations, so the only runtime use of those _typing imports happened inside cast statements, so if you quote them, there are none left and TC001-003 triggers.

Possibly. But the ecosystem check run ruff check. You can see the command at the top:

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

And that ultimately calls into lint_only which lints the file once, without applying fixes.

Ohhhhh.... pandas sets fix = true

@MichaReiser MichaReiser merged commit e25e704 into astral-sh:main Nov 22, 2024
20 checks passed
@Daverball
Copy link
Contributor Author

Ohhhhh.... pandas sets fix = true

I see, ecosystem should probably force that to false then. Although I would see some value in checking for fix circularity issues in ecosystem, but that would require a larger restructure of how ruff is invoked and the results are interpreted.

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.

None yet

2 participants