openrewrite-migrate-python 0.17.0.dev20261021162604__tar.gz → 0.17.0.dev20261021164454__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/PKG-INFO +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/pyproject.toml +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/_literals.py +2 -2
- openrewrite_migrate_python-0.17.0.dev20261021164454/src/openrewrite_code_quality_python/cleanup/_negation.py +39 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/attr_access.py +6 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/boolean_simplification.py +102 -177
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/collection_literals.py +26 -17
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/comparison_simplification.py +16 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/complex_transforms.py +11 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/comprehension_cleanup.py +13 -12
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/conditionals.py +37 -65
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/datetime_cleanup.py +11 -9
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/dict_cleanup.py +17 -8
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/exception_cleanup.py +7 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/expression_simplification.py +14 -8
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/logger_warn.py +5 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/loop_cleanup.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/merge_nested_with.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/merge_startswith_endswith.py +13 -11
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/naming_conventions.py +26 -19
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/negated_operators.py +6 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/none_and_identity.py +20 -9
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/object_inheritance.py +8 -8
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/pandas_cleanup.py +6 -9
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/python_best_practices.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/range_cleanup.py +15 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/redundant_code.py +3 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/remove_trailing_return_none.py +1 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/string_cleanup.py +73 -149
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/unreachable_code.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/use_items.py +12 -10
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/use_list_clear.py +8 -12
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/yoda_conditions.py +14 -25
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/__init__.py +2 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/_reindent.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/_trivia.py +2 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/all_branches_identical.py +26 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/boolean_checks_not_inverted.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/collapsible_if_statements.py +3 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/find_suspicious_chained_comparison.py +3 -3
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/merge_identical_branches.py +18 -16
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/remove_duplicate_conditions.py +11 -14
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/remove_self_assignment.py +42 -29
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/remove_unconditional_value_overwrite.py +16 -14
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/simplify_boolean_literal.py +7 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/simplify_redundant_logical_expression.py +12 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/__init__.py +4 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_markers.py +2 -3
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_module_imports.py +6 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/array_deprecations.py +26 -18
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/ast_deprecations.py +69 -29
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/asyncio_coroutine_to_async.py +30 -7
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/avro_migrations.py +15 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/backoff_to_tenacity.py +172 -162
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/calendar_deprecations.py +15 -33
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/datetime_utc.py +26 -30
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/distutils_migrations.py +20 -44
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/html_parser_deprecations.py +18 -4
- openrewrite_migrate_python-0.17.0.dev20261021164454/src/openrewrite_migrate_python/migrate/inspect_deprecations.py +84 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/langchain_classic_imports.py +41 -56
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/langchain_community_imports.py +19 -40
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/langchain_provider_imports.py +3 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pkgutil_deprecations.py +35 -21
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/platform_deprecations.py +12 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pydantic_deprecations.py +133 -170
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pydantic_fields.py +11 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pydantic_model_fields.py +30 -49
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/string_formatting.py +35 -33
- openrewrite_migrate_python-0.17.0.dev20261021164454/src/openrewrite_migrate_python/migrate/sys_deprecations.py +156 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/sys_last_deprecations.py +27 -6
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/threading_deprecations.py +87 -51
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/typing_union_to_pipe.py +84 -82
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_langchain02.py +2 -2
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_langchain1.py +6 -9
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_pandas.py +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_pydantic.py +18 -18
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python310.py +5 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python311.py +7 -5
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python312.py +12 -12
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python313.py +7 -7
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python314.py +7 -12
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python38.py +18 -21
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python39.py +28 -16
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/xml_deprecations.py +23 -9
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/PKG-INFO +1 -1
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/SOURCES.txt +3 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_ast_deprecations.py +12 -37
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_asyncio_coroutine_to_async.py +64 -15
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_backoff_to_tenacity.py +57 -52
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_calendar_deprecations.py +4 -40
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_change_import_recipes.py +0 -36
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_datetime_utc.py +37 -3
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_distutils_migrations.py +23 -3
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_html_parser_deprecations.py +48 -10
- openrewrite_migrate_python-0.17.0.dev20261021164454/tests/test_inspect_deprecations.py +30 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_langchain_classic_imports.py +51 -127
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_langchain_community_imports.py +14 -95
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_langchain_provider_imports.py +9 -27
- openrewrite_migrate_python-0.17.0.dev20261021164454/tests/test_markers.py +19 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pkgutil_deprecations.py +12 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_platform_deprecations.py +22 -41
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pydantic_deprecations.py +176 -89
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pydantic_fields.py +36 -39
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pydantic_model_fields.py +79 -65
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_string_formatting.py +19 -56
- openrewrite_migrate_python-0.17.0.dev20261021164454/tests/test_sys_deprecations.py +57 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_sys_last_deprecations.py +30 -7
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_threading_deprecations.py +25 -23
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_threading_extended.py +92 -19
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_typing_union_to_pipe.py +73 -17
- openrewrite_migrate_python-0.17.0.dev20261021164454/tests/test_upgrade_recipes.py +208 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_upgrade_to_pydantic.py +12 -10
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_xml_deprecations.py +30 -28
- openrewrite_migrate_python-0.17.0.dev20261021162604/src/openrewrite_migrate_python/migrate/sys_deprecations.py +0 -78
- openrewrite_migrate_python-0.17.0.dev20261021162604/tests/test_markers.py +0 -31
- openrewrite_migrate_python-0.17.0.dev20261021162604/tests/test_sys_deprecations.py +0 -42
- openrewrite_migrate_python-0.17.0.dev20261021162604/tests/test_upgrade_recipes.py +0 -344
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/setup.cfg +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/__init__.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/__init__.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/cleanup/assign_return.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/_semantically_equal.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/_side_effects.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/_truth_value.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_code_quality_python/codequality/code_quality.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/__init__.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_decorators.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_langchain_tables.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_method_renames.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/_slots.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/aifc_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/asyncio_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/cgi_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/cgi_parse_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/collections_abc_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/configparser_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/distutils_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/functools_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/future_imports.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/gettext_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/imp_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/locale_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/locale_getdefaultlocale_deprecation.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/macpath_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/mailcap_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/nntplib_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/os_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pathlib_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pep594_system_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/pipes_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/re_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/removed_modules_312.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/shutil_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/socket_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/ssl_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/tarfile_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/telnetlib_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/tempfile_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/threading_is_alive_deprecation.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/typing_callable.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/typing_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/unittest_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/upgrade_to_python315.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/urllib_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/uu_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python/migrate/xdrlib_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/dependency_links.txt +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/entry_points.txt +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/requires.txt +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/src/openrewrite_migrate_python.egg-info/top_level.txt +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_aifc_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_array_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_asyncio_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_avro_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_cgi_parse_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_collections_abc_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_configparser_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_distutils_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_functools_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_future_imports.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_gettext_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_imp_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_locale_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_locale_getdefaultlocale_deprecation.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_macpath_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_mailcap_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_os_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pathlib_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pep594_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_pep594_system_migrations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_re_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_removed_modules_312.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_shutil_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_socket_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_ssl_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_tarfile_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_tempfile_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_threading_is_alive_deprecation.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_typing_alias_moves.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_typing_callable.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_typing_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_unittest_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_urllib_deprecations.py +0 -0
- {openrewrite_migrate_python-0.17.0.dev20261021162604 → openrewrite_migrate_python-0.17.0.dev20261021164454}/tests/test_xdrlib_migrations.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: openrewrite-migrate-python
|
|
3
|
-
Version: 0.17.0.
|
|
3
|
+
Version: 0.17.0.dev20261021164454
|
|
4
4
|
Summary: OpenRewrite recipes for migrating Python codebases to newer Python versions.
|
|
5
5
|
Author-email: "Moderne Inc." <support@moderne.io>
|
|
6
6
|
License: Moderne Proprietary
|
|
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "openrewrite-migrate-python"
|
|
7
7
|
description = "OpenRewrite recipes for migrating Python codebases to newer Python versions."
|
|
8
|
-
version = "0.17.0.
|
|
8
|
+
version = "0.17.0.dev20261021164454" # Updated dynamically during release
|
|
9
9
|
authors = [{ name = "Moderne Inc.", email = "support@moderne.io" }]
|
|
10
10
|
license = { text = "Moderne Proprietary" }
|
|
11
11
|
readme = "README.md"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Classify literals by their source text, which tells
|
|
1
|
+
"""Classify literals by their source text, which tells ``str`` from ``bytes`` where the type does not."""
|
|
2
2
|
|
|
3
3
|
from typing import Any
|
|
4
4
|
|
|
@@ -19,5 +19,5 @@ def is_string_literal(expr: Any) -> bool:
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
def is_bytes_literal(expr: Literal) -> bool:
|
|
22
|
-
"""Whether ``expr`` is a ``b``-prefixed literal, which the parser
|
|
22
|
+
"""Whether ``expr`` is a ``b``-prefixed literal, which the parser does not type as ``bytes``."""
|
|
23
23
|
return "b" in _prefix(expr.value_source or "").lower()
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Negate a condition by its own operator, where one says the negation without a ``not``."""
|
|
2
|
+
|
|
3
|
+
from typing import TypeGuard
|
|
4
|
+
|
|
5
|
+
from rewrite.java.support_types import Space
|
|
6
|
+
from rewrite.java.tree import Expression, Unary
|
|
7
|
+
from rewrite.python.tree import Binary as PyBinary
|
|
8
|
+
|
|
9
|
+
from openrewrite_code_quality_python.codequality._truth_value import is_chained_comparison
|
|
10
|
+
|
|
11
|
+
_FLIPPED = {
|
|
12
|
+
PyBinary.Type.In: PyBinary.Type.NotIn,
|
|
13
|
+
PyBinary.Type.NotIn: PyBinary.Type.In,
|
|
14
|
+
PyBinary.Type.Is: PyBinary.Type.IsNot,
|
|
15
|
+
PyBinary.Type.IsNot: PyBinary.Type.Is,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def is_not(expr: Expression) -> TypeGuard[Unary]:
|
|
20
|
+
return isinstance(expr, Unary) and expr.operator == Unary.Type.Not
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def negated_without_not(expr: Expression, cancel_not: bool) -> Expression | None:
|
|
24
|
+
"""``expr`` negated without prepending ``not``, or None where that takes a ``not``.
|
|
25
|
+
|
|
26
|
+
``in`` and ``is`` flip to ``not in`` and ``is not`` and back. A link of a chained
|
|
27
|
+
comparison does not, since the flip would negate that link alone. With ``cancel_not``,
|
|
28
|
+
``not b`` gives ``b``, which is not a bool in general and so needs a truth-value context.
|
|
29
|
+
"""
|
|
30
|
+
if isinstance(expr, PyBinary) and not is_chained_comparison(expr):
|
|
31
|
+
flipped = _FLIPPED.get(expr.operator)
|
|
32
|
+
if flipped is not None:
|
|
33
|
+
# `not in` and `is not` carry a `not` keyword that needs whitespace.
|
|
34
|
+
negation = Space.SINGLE_SPACE if flipped in (PyBinary.Type.NotIn, PyBinary.Type.IsNot) else None
|
|
35
|
+
operator = expr.padding.operator.replace(_element=flipped)
|
|
36
|
+
return expr.padding.replace(_operator=operator).replace(_negation=negation)
|
|
37
|
+
if cancel_not and is_not(expr):
|
|
38
|
+
return expr.expression
|
|
39
|
+
return None
|
|
@@ -79,9 +79,9 @@ def _constant_attribute_name(method: MethodInvocation) -> str | None:
|
|
|
79
79
|
@categorize(_Cleanup)
|
|
80
80
|
class ReplaceGetAttrWithConstant(Recipe):
|
|
81
81
|
"""
|
|
82
|
-
Replace
|
|
82
|
+
Replace ``getattr(x, "attr")`` with ``x.attr``.
|
|
83
83
|
|
|
84
|
-
The
|
|
84
|
+
The ``getattr()`` builtin with a constant string attribute name that is a
|
|
85
85
|
valid Python identifier can be replaced with direct attribute access for
|
|
86
86
|
clarity and brevity.
|
|
87
87
|
|
|
@@ -145,7 +145,7 @@ class ReplaceGetAttrWithConstant(Recipe):
|
|
|
145
145
|
_markers=Markers.EMPTY,
|
|
146
146
|
_annotations=[],
|
|
147
147
|
_simple_name=attr_name,
|
|
148
|
-
_type=
|
|
148
|
+
_type=m.type,
|
|
149
149
|
_field_type=None,
|
|
150
150
|
)
|
|
151
151
|
return FieldAccess(
|
|
@@ -158,7 +158,7 @@ class ReplaceGetAttrWithConstant(Recipe):
|
|
|
158
158
|
_element=attr_identifier,
|
|
159
159
|
_markers=Markers.EMPTY,
|
|
160
160
|
),
|
|
161
|
-
_type=
|
|
161
|
+
_type=m.type,
|
|
162
162
|
)
|
|
163
163
|
|
|
164
164
|
return Preconditions.check(uses_method(_GETATTR_PATTERN), Visitor())
|
|
@@ -167,9 +167,9 @@ class ReplaceGetAttrWithConstant(Recipe):
|
|
|
167
167
|
@categorize(_Cleanup)
|
|
168
168
|
class ReplaceSetAttrWithConstant(Recipe):
|
|
169
169
|
"""
|
|
170
|
-
Replace
|
|
170
|
+
Replace ``setattr(x, "attr", value)`` with ``x.attr = value``.
|
|
171
171
|
|
|
172
|
-
When
|
|
172
|
+
When ``setattr()`` is called as a statement with a constant string attribute
|
|
173
173
|
name that is a valid Python identifier, it can be replaced with a direct
|
|
174
174
|
attribute assignment for better readability.
|
|
175
175
|
|
|
@@ -14,11 +14,11 @@ unnecessary operators, literals, or indirection:
|
|
|
14
14
|
See: https://peps.python.org/pep-0008/#programming-recommendations
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
-
import uuid
|
|
18
17
|
from typing import Any, List
|
|
19
18
|
|
|
20
19
|
from rewrite import ExecutionContext, Preconditions, Recipe, TreeVisitor
|
|
21
20
|
from rewrite.markers import Markers
|
|
21
|
+
from rewrite.utils import random_id
|
|
22
22
|
from rewrite.visitor import Cursor
|
|
23
23
|
from rewrite.category import CategoryDescriptor
|
|
24
24
|
from rewrite.decorators import categorize
|
|
@@ -32,19 +32,17 @@ from rewrite.python.template import pattern, template, capture
|
|
|
32
32
|
from rewrite.python.template.pattern import MatchResult
|
|
33
33
|
from rewrite.java.tree import (
|
|
34
34
|
Binary as JBinary, Block, ControlParentheses, Empty, Expression, If as JIf, J,
|
|
35
|
-
Literal, MethodInvocation,
|
|
35
|
+
Literal, MethodInvocation, Return, Ternary as JTernary, Unary,
|
|
36
36
|
WhileLoop as JWhileLoop,
|
|
37
37
|
)
|
|
38
|
-
from rewrite.java.support_types import JavaType,
|
|
39
|
-
from rewrite.python.tree import
|
|
40
|
-
|
|
41
|
-
)
|
|
38
|
+
from rewrite.java.support_types import JavaType, Space
|
|
39
|
+
from rewrite.python.tree import CollectionLiteral, DictLiteral, KeyValue, Star
|
|
40
|
+
from openrewrite_code_quality_python.cleanup._negation import is_not, negated_without_not
|
|
42
41
|
from openrewrite_code_quality_python.codequality._semantically_equal import SemanticallyEqual
|
|
43
42
|
from openrewrite_code_quality_python.codequality._side_effects import is_side_effect_free
|
|
44
43
|
from openrewrite_code_quality_python.codequality._trivia import has_comments
|
|
45
44
|
from openrewrite_code_quality_python.codequality._truth_value import (
|
|
46
45
|
is_bool_valued,
|
|
47
|
-
is_chained_comparison,
|
|
48
46
|
is_comparison,
|
|
49
47
|
is_read_as_truth_value,
|
|
50
48
|
)
|
|
@@ -75,8 +73,8 @@ _len_eq_0_pattern = pattern("len({lc_x}) == 0", lc_x=_lc_x)
|
|
|
75
73
|
_len_gte_1_pattern = pattern("len({lc_x}) >= 1", lc_x=_lc_x)
|
|
76
74
|
_LEN_PATTERN = "builtins len(..)"
|
|
77
75
|
_LEN = MethodMatcher.create(_LEN_PATTERN)
|
|
78
|
-
# Classes whose truth value is defined by their length.
|
|
79
|
-
_SIZED_CLASSES = ("list", "tuple", "dict", "set", "frozenset", "bytearray")
|
|
76
|
+
# Classes whose truth value is defined by their length.
|
|
77
|
+
_SIZED_CLASSES = ("list", "tuple", "dict", "set", "frozenset", "bytes", "bytearray")
|
|
80
78
|
_lc_x_template = template("{lc_x}", lc_x=_lc_x)
|
|
81
79
|
_lc_not_x_template = template("not {lc_x}", lc_x=_lc_x)
|
|
82
80
|
|
|
@@ -111,48 +109,14 @@ _neq_or_neq_template = template(
|
|
|
111
109
|
"{dm_p} != {dm_q} or {dm_r} != {dm_s}", dm_p=_dm_p, dm_q=_dm_q, dm_r=_dm_r, dm_s=_dm_s
|
|
112
110
|
)
|
|
113
111
|
|
|
114
|
-
# Operator flipping for idiomatic negation of in/is comparisons
|
|
115
|
-
_FLIP_OPS = {
|
|
116
|
-
PyBinary.Type.In: PyBinary.Type.NotIn,
|
|
117
|
-
PyBinary.Type.NotIn: PyBinary.Type.In,
|
|
118
|
-
PyBinary.Type.Is: PyBinary.Type.IsNot,
|
|
119
|
-
PyBinary.Type.IsNot: PyBinary.Type.Is,
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def _flip_py_binary(expr) -> PyBinary | None:
|
|
124
|
-
"""Flip a membership/identity operator: ``in`` ↔ ``not in``, ``is`` ↔ ``is not``.
|
|
125
|
-
|
|
126
|
-
Returns the flipped :class:`PyBinary`, or ``None`` if the expression is
|
|
127
|
-
not a :class:`PyBinary` with a flippable operator, or is a link of a
|
|
128
|
-
chained comparison, where the flip would negate that link alone.
|
|
129
|
-
"""
|
|
130
|
-
if not isinstance(expr, PyBinary) or is_chained_comparison(expr):
|
|
131
|
-
return None
|
|
132
|
-
new_type = _FLIP_OPS.get(expr.operator)
|
|
133
|
-
if new_type is None:
|
|
134
|
-
return None
|
|
135
|
-
new_op = expr.padding.operator.replace(_element=new_type)
|
|
136
|
-
# NotIn / IsNot carry a `not` keyword that needs whitespace
|
|
137
|
-
new_negation = (
|
|
138
|
-
Space.SINGLE_SPACE
|
|
139
|
-
if new_type in (PyBinary.Type.NotIn, PyBinary.Type.IsNot)
|
|
140
|
-
else None
|
|
141
|
-
)
|
|
142
|
-
return expr.padding.replace(_operator=new_op).replace(_negation=new_negation)
|
|
143
|
-
|
|
144
|
-
|
|
145
112
|
def _is_bool_literal(node, expected_source: str) -> bool:
|
|
146
113
|
"""Check if a node is a specific boolean literal (by value_source)."""
|
|
147
114
|
return isinstance(node, Literal) and node.value_source == expected_source
|
|
148
115
|
|
|
149
116
|
|
|
150
|
-
def _is_not(expr: Expression) -> bool:
|
|
151
|
-
return isinstance(expr, Unary) and expr.operator == Unary.Type.Not
|
|
152
|
-
|
|
153
|
-
|
|
154
117
|
_as_bool_cond = capture('cond')
|
|
155
118
|
_bool_template = template("bool({cond})", cond=_as_bool_cond)
|
|
119
|
+
_not_cond_template = template("not {cond}", cond=_as_bool_cond)
|
|
156
120
|
|
|
157
121
|
|
|
158
122
|
def _as_bool(cond: Expression, cursor: Cursor) -> Expression | None:
|
|
@@ -209,15 +173,14 @@ class BooleanIfExpIdentity(Recipe):
|
|
|
209
173
|
return ["python", "cleanup", "ruff", "SIM210", "SIM211", "RUF034"]
|
|
210
174
|
|
|
211
175
|
def editor(self) -> TreeVisitor[Any, ExecutionContext]:
|
|
212
|
-
_cond = capture('cond')
|
|
213
|
-
_not_cond_template = template("not {cond}", cond=_cond)
|
|
214
|
-
|
|
215
176
|
class Visitor(PythonVisitor[ExecutionContext]):
|
|
216
177
|
def visit_ternary(
|
|
217
178
|
self, ternary: JTernary, p: ExecutionContext
|
|
218
179
|
) -> J:
|
|
219
180
|
visited = super().visit_ternary(ternary, p)
|
|
220
|
-
if not isinstance(visited, JTernary)
|
|
181
|
+
if not isinstance(visited, JTernary) or has_comments(
|
|
182
|
+
visited.replace(_prefix=Space.EMPTY)
|
|
183
|
+
):
|
|
221
184
|
return visited
|
|
222
185
|
ternary = visited
|
|
223
186
|
|
|
@@ -305,7 +268,9 @@ class RemoveRedundantBoolean(Recipe):
|
|
|
305
268
|
binary = visited
|
|
306
269
|
|
|
307
270
|
# Guard: at least one side must be an actual True/False literal (not 1/0)
|
|
308
|
-
if not self._has_any_bool_literal(binary)
|
|
271
|
+
if not self._has_any_bool_literal(binary) or has_comments(
|
|
272
|
+
binary.replace(_prefix=Space.EMPTY)
|
|
273
|
+
):
|
|
309
274
|
return binary
|
|
310
275
|
|
|
311
276
|
# `x and True` yields True, not x, for a truthy x, so a literal on the
|
|
@@ -334,25 +299,6 @@ class RemoveRedundantBoolean(Recipe):
|
|
|
334
299
|
return Visitor()
|
|
335
300
|
|
|
336
301
|
|
|
337
|
-
def _negate_operand(expr, cancel_not: bool):
|
|
338
|
-
"""Try to negate *expr* without prepending ``not``.
|
|
339
|
-
|
|
340
|
-
Returns the negated form, or ``None`` if no short-cut applies.
|
|
341
|
-
|
|
342
|
-
Short-cuts (in priority order):
|
|
343
|
-
|
|
344
|
-
1. **Operator flip** – ``in`` ↔ ``not in``, ``is`` ↔ ``is not``.
|
|
345
|
-
2. **Double-negation cancel** – ``not B`` → ``B``, if ``cancel_not``.
|
|
346
|
-
``B`` is not a bool in general, so this needs a truth-value context.
|
|
347
|
-
"""
|
|
348
|
-
flipped = _flip_py_binary(expr)
|
|
349
|
-
if flipped is not None:
|
|
350
|
-
return flipped
|
|
351
|
-
if cancel_not and _is_not(expr):
|
|
352
|
-
return expr.expression
|
|
353
|
-
return None
|
|
354
|
-
|
|
355
|
-
|
|
356
302
|
def _apply_demorgan(cursor, match, truth_value, both_not, left_not, right_not, neither_not):
|
|
357
303
|
"""Apply a DeMorgan template, folding negations where possible.
|
|
358
304
|
|
|
@@ -367,10 +313,10 @@ def _apply_demorgan(cursor, match, truth_value, both_not, left_not, right_not, n
|
|
|
367
313
|
"""
|
|
368
314
|
dm_x_val = match['dm_x']
|
|
369
315
|
dm_y_val = match['dm_y']
|
|
370
|
-
if not truth_value and (
|
|
316
|
+
if not truth_value and (is_not(dm_x_val) or is_not(dm_y_val)):
|
|
371
317
|
return None
|
|
372
|
-
x_negated =
|
|
373
|
-
y_negated =
|
|
318
|
+
x_negated = negated_without_not(dm_x_val, truth_value)
|
|
319
|
+
y_negated = negated_without_not(dm_y_val, truth_value)
|
|
374
320
|
|
|
375
321
|
if x_negated and y_negated:
|
|
376
322
|
values = MatchResult({'dm_x': x_negated, 'dm_y': y_negated})
|
|
@@ -384,6 +330,58 @@ def _apply_demorgan(cursor, match, truth_value, both_not, left_not, right_not, n
|
|
|
384
330
|
return both_not.apply(cursor, values=match)
|
|
385
331
|
|
|
386
332
|
|
|
333
|
+
def _distribute_not(unary: Unary, cursor: Cursor) -> J:
|
|
334
|
+
"""``unary`` with its ``not`` distributed one level in, or ``unary`` itself."""
|
|
335
|
+
truth_value = is_read_as_truth_value(cursor)
|
|
336
|
+
|
|
337
|
+
# not (p == q and r == s) -> p != q or r != s
|
|
338
|
+
match = _not_eq_and_eq_pattern.match(unary, cursor)
|
|
339
|
+
if match and not any(
|
|
340
|
+
isinstance(operand, Expression) and is_comparison(operand)
|
|
341
|
+
for operand in (match[k] for k in ('dm_p', 'dm_q', 'dm_r', 'dm_s'))
|
|
342
|
+
):
|
|
343
|
+
if not truth_value:
|
|
344
|
+
return unary
|
|
345
|
+
return _neq_or_neq_template.apply(cursor, values=match) or unary
|
|
346
|
+
|
|
347
|
+
if truth_value:
|
|
348
|
+
# Fused: not (not x and not y) -> x or y
|
|
349
|
+
match = _not_notx_and_noty_pattern.match(unary, cursor)
|
|
350
|
+
if match:
|
|
351
|
+
return _x_or_y_template.apply(cursor, values=match) or unary
|
|
352
|
+
|
|
353
|
+
# Fused: not (not x or not y) -> x and y
|
|
354
|
+
match = _not_notx_or_noty_pattern.match(unary, cursor)
|
|
355
|
+
if match:
|
|
356
|
+
return _x_and_y_template.apply(cursor, values=match) or unary
|
|
357
|
+
|
|
358
|
+
# not (x and y) -> not x or not y
|
|
359
|
+
# Skip multi-way chains: not (a and b and c) where dm_x
|
|
360
|
+
# captures (a and b) — partial expansion is messy.
|
|
361
|
+
match = _not_and_pattern.match(unary, cursor)
|
|
362
|
+
if match:
|
|
363
|
+
dm_x_val = match['dm_x']
|
|
364
|
+
if not (isinstance(dm_x_val, JBinary) and dm_x_val.operator == JBinary.Type.And):
|
|
365
|
+
return _apply_demorgan(
|
|
366
|
+
cursor, match, truth_value, _not_x_or_not_y_template,
|
|
367
|
+
_not_x_or_y_template, _x_or_not_y_template,
|
|
368
|
+
_x_or_y_template,
|
|
369
|
+
) or unary
|
|
370
|
+
|
|
371
|
+
# not (x or y) -> not x and not y
|
|
372
|
+
match = _not_or_pattern.match(unary, cursor)
|
|
373
|
+
if match:
|
|
374
|
+
dm_x_val = match['dm_x']
|
|
375
|
+
if not (isinstance(dm_x_val, JBinary) and dm_x_val.operator == JBinary.Type.Or):
|
|
376
|
+
return _apply_demorgan(
|
|
377
|
+
cursor, match, truth_value, _not_x_and_not_y_template,
|
|
378
|
+
_not_x_and_y_template, _x_and_not_y_template,
|
|
379
|
+
_x_and_y_template,
|
|
380
|
+
) or unary
|
|
381
|
+
|
|
382
|
+
return unary
|
|
383
|
+
|
|
384
|
+
|
|
387
385
|
@categorize(_Cleanup)
|
|
388
386
|
class DeMorgan(Recipe):
|
|
389
387
|
"""
|
|
@@ -429,58 +427,15 @@ class DeMorgan(Recipe):
|
|
|
429
427
|
self, unary: Unary, p: ExecutionContext
|
|
430
428
|
) -> J:
|
|
431
429
|
visited = super().visit_unary(unary, p)
|
|
432
|
-
if not isinstance(visited, Unary)
|
|
433
|
-
|
|
434
|
-
unary = visited
|
|
435
|
-
cursor = Cursor(self.cursor.parent, unary)
|
|
436
|
-
truth_value = is_read_as_truth_value(cursor)
|
|
437
|
-
|
|
438
|
-
# not (p == q and r == s) -> p != q or r != s
|
|
439
|
-
match = _not_eq_and_eq_pattern.match(unary, cursor)
|
|
440
|
-
if match and not any(
|
|
441
|
-
isinstance(operand, Expression) and is_comparison(operand)
|
|
442
|
-
for operand in (match[k] for k in ('dm_p', 'dm_q', 'dm_r', 'dm_s'))
|
|
430
|
+
if not isinstance(visited, Unary) or has_comments(
|
|
431
|
+
visited.replace(_prefix=Space.EMPTY)
|
|
443
432
|
):
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
match = _not_notx_and_noty_pattern.match(unary, cursor)
|
|
451
|
-
if match:
|
|
452
|
-
return _x_or_y_template.apply(cursor, values=match) or unary
|
|
453
|
-
|
|
454
|
-
# Fused: not (not x or not y) -> x and y
|
|
455
|
-
match = _not_notx_or_noty_pattern.match(unary, cursor)
|
|
456
|
-
if match:
|
|
457
|
-
return _x_and_y_template.apply(cursor, values=match) or unary
|
|
458
|
-
|
|
459
|
-
# not (x and y) -> not x or not y
|
|
460
|
-
# Skip multi-way chains: not (a and b and c) where dm_x
|
|
461
|
-
# captures (a and b) — partial expansion is messy.
|
|
462
|
-
match = _not_and_pattern.match(unary, cursor)
|
|
463
|
-
if match:
|
|
464
|
-
dm_x_val = match['dm_x']
|
|
465
|
-
if not (isinstance(dm_x_val, JBinary) and dm_x_val.operator == JBinary.Type.And):
|
|
466
|
-
return _apply_demorgan(
|
|
467
|
-
cursor, match, truth_value, _not_x_or_not_y_template,
|
|
468
|
-
_not_x_or_y_template, _x_or_not_y_template,
|
|
469
|
-
_x_or_y_template,
|
|
470
|
-
) or unary
|
|
471
|
-
|
|
472
|
-
# not (x or y) -> not x and not y
|
|
473
|
-
match = _not_or_pattern.match(unary, cursor)
|
|
474
|
-
if match:
|
|
475
|
-
dm_x_val = match['dm_x']
|
|
476
|
-
if not (isinstance(dm_x_val, JBinary) and dm_x_val.operator == JBinary.Type.Or):
|
|
477
|
-
return _apply_demorgan(
|
|
478
|
-
cursor, match, truth_value, _not_x_and_not_y_template,
|
|
479
|
-
_not_x_and_y_template, _x_and_not_y_template,
|
|
480
|
-
_x_and_y_template,
|
|
481
|
-
) or unary
|
|
482
|
-
|
|
483
|
-
return unary
|
|
433
|
+
return visited
|
|
434
|
+
result = _distribute_not(visited, Cursor(self.cursor.parent, visited))
|
|
435
|
+
if result is visited:
|
|
436
|
+
return visited
|
|
437
|
+
# The negations a rewrite creates may distribute in turn.
|
|
438
|
+
return Visitor().visit(result, p, self.cursor.parent_or_throw) or result
|
|
484
439
|
|
|
485
440
|
return Visitor()
|
|
486
441
|
|
|
@@ -526,7 +481,7 @@ class SimplifyLenComparison(Recipe):
|
|
|
526
481
|
"Rewrite `len(seq) > 0`, `len(seq) != 0` and `len(seq) >= 1` to `seq` "
|
|
527
482
|
"and `len(seq) == 0` to `not seq`. Only fires where the comparison "
|
|
528
483
|
"is read as a truth value and the type of `seq` resolves to a builtin "
|
|
529
|
-
"string, list, tuple, dict, set, frozenset or bytearray, whose truth "
|
|
484
|
+
"string, list, tuple, dict, set, frozenset, bytes or bytearray, whose truth "
|
|
530
485
|
"value is its length."
|
|
531
486
|
)
|
|
532
487
|
|
|
@@ -550,6 +505,7 @@ class SimplifyLenComparison(Recipe):
|
|
|
550
505
|
or not _is_sized_builtin(call.arguments[0].type)
|
|
551
506
|
or not is_read_as_truth_value(self.cursor)
|
|
552
507
|
or not _LEN.matches(call, cursor=Cursor(self.cursor, call))
|
|
508
|
+
or has_comments(binary.replace(_prefix=Space.EMPTY))
|
|
553
509
|
):
|
|
554
510
|
return binary
|
|
555
511
|
|
|
@@ -578,9 +534,9 @@ def _is_collection_literal(node) -> bool | None:
|
|
|
578
534
|
element or dict key must be a scalar literal, since hashing anything else
|
|
579
535
|
may raise.
|
|
580
536
|
|
|
581
|
-
Note: In the OpenRewrite AST, empty collections like
|
|
582
|
-
element which is an
|
|
583
|
-
exactly one
|
|
537
|
+
Note: In the OpenRewrite AST, empty collections like ``[]`` still have one
|
|
538
|
+
element which is an ``Empty`` sentinel node. A truly empty collection has
|
|
539
|
+
exactly one ``Empty`` element, while non-empty collections have real elements.
|
|
584
540
|
"""
|
|
585
541
|
if not isinstance(node, (CollectionLiteral, DictLiteral)) or has_comments(node):
|
|
586
542
|
return None
|
|
@@ -649,7 +605,7 @@ class CollectionToBool(Recipe):
|
|
|
649
605
|
def _make_bool_literal(self, is_true: bool, prefix) -> Literal:
|
|
650
606
|
"""Create a True or False literal with the given prefix."""
|
|
651
607
|
return Literal(
|
|
652
|
-
_id=
|
|
608
|
+
_id=random_id(),
|
|
653
609
|
_prefix=prefix,
|
|
654
610
|
_markers=Markers.EMPTY,
|
|
655
611
|
_value=is_true,
|
|
@@ -709,7 +665,7 @@ class CollectionToBool(Recipe):
|
|
|
709
665
|
|
|
710
666
|
|
|
711
667
|
def _returned_bool(stmt) -> bool | None:
|
|
712
|
-
"""The literal a
|
|
668
|
+
"""The literal a ``return True``/``return False`` statement returns, else None."""
|
|
713
669
|
if isinstance(stmt, Return):
|
|
714
670
|
expr = stmt.expression
|
|
715
671
|
if isinstance(expr, Literal) and expr.value_source in _BOOLEAN_SOURCES:
|
|
@@ -755,9 +711,8 @@ class SimplifyBooleanReturn(Recipe):
|
|
|
755
711
|
"`return cond` and the inverse with `return not cond`, including the "
|
|
756
712
|
"fallthrough variant without an explicit `else`. A condition other than "
|
|
757
713
|
"a comparison, a `not`, or an `and`/`or` of those is returned as "
|
|
758
|
-
"`bool(cond)`. The "
|
|
759
|
-
"
|
|
760
|
-
"statements hold comments."
|
|
714
|
+
"`bool(cond)`. The rewrite is declined on an `elif`, where `bool` is "
|
|
715
|
+
"not the builtin, or where the removed statements hold comments."
|
|
761
716
|
)
|
|
762
717
|
|
|
763
718
|
@property
|
|
@@ -768,7 +723,7 @@ class SimplifyBooleanReturn(Recipe):
|
|
|
768
723
|
class Visitor(PythonVisitor[ExecutionContext]):
|
|
769
724
|
|
|
770
725
|
def _get_single_bool_return(self, body) -> bool | None:
|
|
771
|
-
"""If body is a Block holding only
|
|
726
|
+
"""If body is a Block holding only ``return True``/``return False``, that literal."""
|
|
772
727
|
if not isinstance(body, Block) or len(body.statements) != 1:
|
|
773
728
|
return None
|
|
774
729
|
return _returned_bool(body.statements[0])
|
|
@@ -782,60 +737,28 @@ class SimplifyBooleanReturn(Recipe):
|
|
|
782
737
|
def _make_return(self, prefix, expression) -> Return:
|
|
783
738
|
"""Create a Return node with the given prefix and expression."""
|
|
784
739
|
return Return(
|
|
785
|
-
_id=
|
|
740
|
+
_id=random_id(),
|
|
786
741
|
_prefix=prefix,
|
|
787
742
|
_markers=Markers.EMPTY,
|
|
788
743
|
_expression=expression.replace(_prefix=Space.SINGLE_SPACE),
|
|
789
744
|
)
|
|
790
745
|
|
|
791
|
-
def _make_not(self, expression) -> Unary:
|
|
792
|
-
"""Wrap expression in `not`, adding parentheses for compound expressions.
|
|
793
|
-
|
|
794
|
-
Without parentheses, `not A and B` is parsed as `(not A) and B`
|
|
795
|
-
due to Python's operator precedence. When the intent is
|
|
796
|
-
`not (A and B)`, we must wrap in `J.Parentheses`.
|
|
797
|
-
"""
|
|
798
|
-
inner = expression
|
|
799
|
-
# Wrap and/or compounds in parens to preserve semantics
|
|
800
|
-
if isinstance(expression, JBinary) and expression.operator in (
|
|
801
|
-
JBinary.Type.And, JBinary.Type.Or,
|
|
802
|
-
):
|
|
803
|
-
inner = Parentheses(
|
|
804
|
-
_id=uuid.uuid4(),
|
|
805
|
-
_prefix=Space.SINGLE_SPACE,
|
|
806
|
-
_markers=Markers.EMPTY,
|
|
807
|
-
_tree=JRightPadded(
|
|
808
|
-
_element=expression.replace(_prefix=Space.EMPTY),
|
|
809
|
-
_after=Space.EMPTY,
|
|
810
|
-
_markers=Markers.EMPTY,
|
|
811
|
-
),
|
|
812
|
-
)
|
|
813
|
-
else:
|
|
814
|
-
inner = expression.replace(_prefix=Space.SINGLE_SPACE)
|
|
815
|
-
return Unary(
|
|
816
|
-
_id=uuid.uuid4(),
|
|
817
|
-
_prefix=Space.SINGLE_SPACE,
|
|
818
|
-
_markers=Markers.EMPTY,
|
|
819
|
-
_operator=JLeftPadded(
|
|
820
|
-
_before=Space.EMPTY,
|
|
821
|
-
_element=Unary.Type.Not,
|
|
822
|
-
_markers=Markers.EMPTY,
|
|
823
|
-
),
|
|
824
|
-
_expression=inner,
|
|
825
|
-
_type=JavaType.Primitive.Boolean,
|
|
826
|
-
)
|
|
827
|
-
|
|
828
746
|
def _simplify_return(
|
|
829
747
|
self, if_: JIf, parent: Cursor, then_is_true: bool, else_is_true: bool
|
|
830
748
|
) -> Return | None:
|
|
831
749
|
"""Build Return for the simplified boolean pattern."""
|
|
832
750
|
cond = self._unwrap_condition(if_.if_condition)
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
return None
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
751
|
+
cursor = Cursor(Cursor(parent, if_), cond)
|
|
752
|
+
if then_is_true == else_is_true:
|
|
753
|
+
return None
|
|
754
|
+
if then_is_true:
|
|
755
|
+
result = _as_bool(cond, cursor)
|
|
756
|
+
elif is_not(cond):
|
|
757
|
+
result = _as_bool(cond.expression, Cursor(cursor, cond.expression))
|
|
758
|
+
else:
|
|
759
|
+
negated = _not_cond_template.apply(cursor, values=MatchResult({'cond': cond}))
|
|
760
|
+
result = negated if isinstance(negated, Expression) else None
|
|
761
|
+
return None if result is None else self._make_return(if_.prefix, result)
|
|
839
762
|
|
|
840
763
|
def visit_if(
|
|
841
764
|
self, if_: JIf, p: ExecutionContext
|
|
@@ -845,9 +768,11 @@ class SimplifyBooleanReturn(Recipe):
|
|
|
845
768
|
return visited
|
|
846
769
|
if_stmt = visited
|
|
847
770
|
|
|
848
|
-
# Only handle explicit else pattern here
|
|
771
|
+
# Only handle explicit else pattern here. An `elif` is the body of
|
|
772
|
+
# its enclosing `else`, where a `return` cannot take its place.
|
|
849
773
|
if (
|
|
850
774
|
if_stmt.else_part is None
|
|
775
|
+
or isinstance(self.cursor.parent_tree_cursor().value, JIf.Else)
|
|
851
776
|
or has_comments(if_stmt.then_part)
|
|
852
777
|
or has_comments(if_stmt.else_part)
|
|
853
778
|
):
|