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-pyi] Apply redundant-numeric-union to more type expressions (PYI041) #14332

Conversation

sbrugman
Copy link
Contributor

Summary

Fixes false negatives for redundant numeric unions in other places than function arguments, eg. return types, type aliases

Splitting this change of from #14273 (will rebase once that is merged)

Test Plan

cargo test and reviewed ecosystem results

Copy link
Contributor

github-actions bot commented Nov 14, 2024

ruff-ecosystem results

Linter (stable)

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

PlasmaPy/PlasmaPy (+4 -0 violations, +0 -0 fixes)

+ src/plasmapy/analysis/swept_langmuir/floating_potential.py:221:37: PYI041 Use `float` instead of `int | float`
+ src/plasmapy/formulary/densities.py:159:32: PYI041 Use `float` instead of `int | float`
+ tests/plasma/test_grids.py:261:32: PYI041 Use `float` instead of `int | float`
+ tests/plasma/test_grids.py:302:32: PYI041 Use `float` instead of `int | float`

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

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

+ airflow/configuration.py:1168:10: PYI041 Use `float` instead of `int | float`
+ airflow/configuration.py:69:14: PYI041 Use `float` instead of `int | float`
+ airflow/jobs/job.py:63:62: PYI041 Use `float` instead of `int | float`
+ airflow/jobs/job.py:65:35: PYI041 Use `float` instead of `int | float`
+ airflow/metrics/otel_logger.py:53:15: PYI041 Use `float` instead of `int | float`
+ airflow/metrics/protocols.py:26:13: PYI041 Use `float` instead of `int | float`
+ airflow/policies.py:109:54: PYI041 Use `float` instead of `int | float`
+ airflow/serialization/helpers.py:28:65: PYI041 Use `float` instead of `int | float`
+ airflow/serialization/serde.py:59:5: PYI041 Use `float` instead of `int | float`
+ airflow/utils/log/colored_log.py:60:33: PYI041 Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/hooks/analyticdb_spark.py:201:24: PYI041 Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/hooks/analyticdb_spark.py:301:54: PYI041 Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/operators/analyticdb_spark.py:183:24: PYI041 Use `float` instead of `int | float`
+ providers/src/airflow/providers/amazon/aws/hooks/glue.py:111:31: PYI041 Use `float` instead of `int | float`
... 16 additional changes omitted for project

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

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

+ superset/connectors/sqla/models.py:1673:10: PYI041 Use `float` instead of `int | float`
+ superset/db_engine_specs/base.py:875:10: PYI041 Use `float` instead of `int | float`
+ superset/distributed_lock/utils.py:31:15: PYI041 Use `float` instead of `int | float`
+ superset/models/helpers.py:900:10: PYI041 Use `float` instead of `int | float`
+ superset/superset_typing.py:105:15: PYI041 Use `float` instead of `int | float`
+ superset/utils/core.py:349:53: PYI041 Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:20: PYI041 Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:46: PYI041 Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:65: PYI041 Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/resample.py:30:26: PYI041 Use `float` instead of `int | float`
... 1 additional changes omitted for project

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

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

+ src/bokeh/core/property/numeric.py:53:24: PYI041 Use `float` instead of `int | float`
+ tests/support/util/compare.py:50:18: PYI041 Use `float` instead of `int | float`

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

+ src/latch/registry/types.py:37:43: PYI041 Use `float` instead of `int | float`
+ src/latch/types/json.py:11:24: PYI041 Use `float` instead of `int | float`
+ src/latch/types/metadata.py:418:28: PYI041 Use `float` instead of `int | float`
+ src/latch_cli/snakemake/config/utils.py:12:24: PYI041 Use `float` instead of `int | float`
+ src/latch_cli/snakemake/config/utils.py:196:50: PYI041 Use `float` instead of `int | float`
+ src/latch_sdk_gql/__init__.py:14:24: PYI041 Use `float` instead of `int | float`
+ src/latch_sdk_gql/utils.py:37:28: PYI041 Use `float` instead of `int | float`

python/typeshed (+33 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --no-preview --select E,F,FA,I,PYI,RUF,UP,W

+ stdlib/_sqlite3.pyi:28:26: PYI041 Use `float` instead of `int | float`
+ stdlib/ast.pyi:1076:12: PYI041 Use `complex` instead of `int | float | complex`
+ stdlib/ast.pyi:1694:16: PYI041 Use `complex` instead of `int | float | complex`
+ stdlib/dbm/sqlite3.pyi:13:26: PYI041 Use `float` instead of `int | float`
+ stdlib/fractions.pyi:8:29: PYI041 Use `float` instead of `int | float`
+ stdlib/marshal.pyi:12:5: PYI041 Use `complex` instead of `int | float | complex`
+ stdlib/pstats.pyi:19:24: PYI041 Use `float` instead of `int | float`
+ stdlib/sqlite3/__init__.pyi:216:26: PYI041 Use `float` instead of `int | float`
+ stdlib/tarfile.pyi:531:12: PYI041 Use `float` instead of `int | float`
+ stdlib/xmlrpc/client.pyi:17:5: PYI041 Use `float` instead of `int | float`
+ stubs/assertpy/assertpy/numeric.pyi:6:23: PYI041 Use `float` instead of `int | float`
+ stubs/docker/docker/_types.pyi:10:19: PYI041 Use `float` instead of `int | float`
+ stubs/fpdf2/fpdf/drawing.pyi:19:21: PYI041 Use `float` instead of `int | float`
+ stubs/gdb/gdb/__init__.pyi:74:29: PYI041 Use `float` instead of `int | float`
+ stubs/gevent/gevent/timeout.pyi:16:26: PYI041 Use `float` instead of `int | float`
+ stubs/hdbcli/hdbcli/dbapi.pyi:135:14: PYI041 Use `complex` instead of `int | float | complex`
... 17 additional changes omitted for project

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

+ rotkehlchen/fval.py:8:27: PYI041 Use `float` instead of `int | float`

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

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

+ zerver/lib/data_types.py:116:28: PYI041 Use `float` instead of `int | float`
+ zerver/lib/drafts.py:33:16: PYI041 Use `float` instead of `int | float`
+ zerver/openapi/markdown_extension.py:257:64: PYI041 Use `float` instead of `int | float`
+ zilencer/views.py:588:16: PYI041 Use `float` instead of `int | float`

python-trio/trio (+6 -0 violations, +0 -0 fixes)

+ src/trio/_channel.py:114:22: PYI041 Use `float` instead of `int | float`
+ src/trio/_channel.py:123:22: PYI041 Use `float` instead of `int | float`
+ src/trio/_sync.py:159:19: PYI041 Use `float` instead of `int | float`
+ src/trio/_sync.py:229:28: PYI041 Use `float` instead of `int | float`
+ src/trio/_sync.py:236:31: PYI041 Use `float` instead of `int | float`
+ src/trio/_sync.py:270:35: PYI041 Use `float` instead of `int | float`

pytest-dev/pytest (+1 -0 violations, +0 -0 fixes)

+ src/_pytest/mark/structures.py:472:27: PYI041 Use `float` instead of `int | float`

astropy/astropy (+3 -0 violations, +0 -0 fixes)

+ astropy/units/typing.py:75:19: PYI041 Use `float` instead of `int | float`
+ astropy/units/typing.py:78:24: PYI041 Use `float` instead of `int | float`
+ astropy/units/typing.py:79:24: PYI041 Use `complex` instead of `int | float | complex`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PYI041 102 102 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+66 -0 violations, +0 -0 fixes in 10 projects; 45 projects unchanged)

PlasmaPy/PlasmaPy (+4 -0 violations, +0 -0 fixes)

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

+ src/plasmapy/analysis/swept_langmuir/floating_potential.py:221:37: PYI041 [*] Use `float` instead of `int | float`
+ src/plasmapy/formulary/densities.py:159:32: PYI041 [*] Use `float` instead of `int | float`
+ tests/plasma/test_grids.py:261:32: PYI041 [*] Use `float` instead of `int | float`
+ tests/plasma/test_grids.py:302:32: PYI041 [*] Use `float` instead of `int | float`

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

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

+ airflow/configuration.py:1168:10: PYI041 [*] Use `float` instead of `int | float`
+ airflow/configuration.py:69:14: PYI041 [*] Use `float` instead of `int | float`
+ airflow/jobs/job.py:63:62: PYI041 [*] Use `float` instead of `int | float`
+ airflow/jobs/job.py:65:35: PYI041 [*] Use `float` instead of `int | float`
+ airflow/metrics/otel_logger.py:53:15: PYI041 [*] Use `float` instead of `int | float`
+ airflow/metrics/protocols.py:26:13: PYI041 [*] Use `float` instead of `int | float`
+ airflow/policies.py:109:54: PYI041 [*] Use `float` instead of `int | float`
+ airflow/serialization/helpers.py:28:65: PYI041 [*] Use `float` instead of `int | float`
+ airflow/serialization/serde.py:59:5: PYI041 [*] Use `float` instead of `int | float`
+ airflow/utils/log/colored_log.py:60:33: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/hooks/analyticdb_spark.py:201:24: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/hooks/analyticdb_spark.py:301:54: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/alibaba/cloud/operators/analyticdb_spark.py:183:24: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/amazon/aws/hooks/glue.py:111:31: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/apache/livy/hooks/livy.py:340:24: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/apache/livy/hooks/livy.py:429:54: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/apache/livy/hooks/livy.py:725:24: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/apache/livy/hooks/livy.py:810:54: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/apache/livy/operators/livy.py:77:24: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/google/cloud/hooks/vertex_ai/custom_job.py:1043:20: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/google/cloud/hooks/vertex_ai/custom_job.py:1431:20: PYI041 [*] Use `float` instead of `int | float`
+ providers/src/airflow/providers/google/cloud/hooks/vertex_ai/custom_job.py:1815:20: PYI041 [*] Use `float` instead of `int | float`
... 8 additional changes omitted for project

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

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

+ superset/connectors/sqla/models.py:1673:10: PYI041 [*] Use `float` instead of `int | float`
+ superset/db_engine_specs/base.py:875:10: PYI041 [*] Use `float` instead of `int | float`
+ superset/distributed_lock/utils.py:31:15: PYI041 [*] Use `float` instead of `int | float`
+ superset/models/helpers.py:900:10: PYI041 [*] Use `float` instead of `int | float`
+ superset/superset_typing.py:105:15: PYI041 [*] Use `float` instead of `int | float`
+ superset/utils/core.py:349:53: PYI041 [*] Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:20: PYI041 [*] Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:46: PYI041 [*] Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/boxplot.py:34:65: PYI041 [*] Use `float` instead of `int | float`
+ superset/utils/pandas_postprocessing/resample.py:30:26: PYI041 [*] Use `float` instead of `int | float`
... 1 additional changes omitted for project

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

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

+ src/bokeh/core/property/numeric.py:53:24: PYI041 [*] Use `float` instead of `int | float`
+ tests/support/util/compare.py:50:18: PYI041 [*] Use `float` instead of `int | float`

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

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

+ src/latch/registry/types.py:37:43: PYI041 [*] Use `float` instead of `int | float`
+ src/latch/types/json.py:11:24: PYI041 [*] Use `float` instead of `int | float`
+ src/latch/types/metadata.py:418:28: PYI041 [*] Use `float` instead of `int | float`
+ src/latch_cli/snakemake/config/utils.py:12:24: PYI041 [*] Use `float` instead of `int | float`
+ src/latch_cli/snakemake/config/utils.py:196:50: PYI041 [*] Use `float` instead of `int | float`
+ src/latch_sdk_gql/__init__.py:14:24: PYI041 [*] Use `float` instead of `int | float`
+ src/latch_sdk_gql/utils.py:37:28: PYI041 [*] Use `float` instead of `int | float`

python/typeshed (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select E,F,FA,I,PYI,RUF,UP,W

+ stdlib/marshal.pyi:12:5: PYI041 Use `complex` instead of `int | float | complex`
+ stdlib/xmlrpc/client.pyi:17:5: PYI041 Use `float` instead of `int | float`
+ stubs/seaborn/seaborn/utils.pyi:43:5: PYI041 Use `complex` instead of `float | complex`

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

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

+ rotkehlchen/fval.py:8:27: PYI041 [*] Use `float` instead of `int | float`

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

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

+ zerver/lib/data_types.py:116:28: PYI041 [*] Use `float` instead of `int | float`
+ zerver/lib/drafts.py:33:16: PYI041 [*] Use `float` instead of `int | float`
+ zerver/openapi/markdown_extension.py:257:64: PYI041 [*] Use `float` instead of `int | float`
+ zilencer/views.py:588:16: PYI041 [*] Use `float` instead of `int | float`

pytest-dev/pytest (+1 -0 violations, +0 -0 fixes)

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

+ src/_pytest/mark/structures.py:472:27: PYI041 [*] Use `float` instead of `int | float`

astropy/astropy (+3 -0 violations, +0 -0 fixes)

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

+ astropy/units/typing.py:75:19: PYI041 [*] Use `float` instead of `int | float`
+ astropy/units/typing.py:78:24: PYI041 [*] Use `float` instead of `int | float`
+ astropy/units/typing.py:79:24: PYI041 [*] Use `complex` instead of `int | float | complex`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PYI041 66 66 0 0 0

@MichaReiser
Copy link
Member

Note for myself, merge #14273 after this PR lands

@sbrugman sbrugman force-pushed the pyi041-redundant-numeric-union-false-negatives branch from 16daf9d to c7c1125 Compare November 25, 2024 18:33
@sbrugman sbrugman marked this pull request as ready for review November 25, 2024 18:33
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Unfortunately, I think the premise of this PR is incorrect. As the docs for this rule state:

/// ## Why is this bad?
/// The [typing specification] states:
///
/// > Python’s numeric types `complex`, `float` and `int` are not subtypes of
/// > each other, but to support common use cases, the type system contains a
/// > straightforward shortcut: when an argument is annotated as having type
/// > `float`, an argument of type `int` is acceptable; similar, for an
/// > argument annotated as having type `complex`, arguments of type `float` or
/// > `int` are acceptable.
///
/// As such, a union that includes both `int` and `float` is redundant in the
/// specific context of a parameter annotation, as it is equivalent to a union
/// that only includes `float`. For readability and clarity, unions should omit
/// redundant elements.

Note the specific language of the typing spec there: float is not a subtype of complex, and int is not a subtype of float. You cannot always simplify the union int | float to float, you can only do this "in the specific context of a parameter annotation", because of a very special case that the typing system carves out for this situation

@sbrugman
Copy link
Contributor Author

Ah, thanks for pointing that out! I wrongly assumed the scope to parameters was unintentional and that this rule applied in all type annotation contexts.

@sbrugman sbrugman closed this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants