Skip to content

Comments

[ty] support del statement and deletion of except handler names#18593

Merged
carljm merged 6 commits intoastral-sh:mainfrom
mtshiba:del-statement
Jun 12, 2025
Merged

[ty] support del statement and deletion of except handler names#18593
carljm merged 6 commits intoastral-sh:mainfrom
mtshiba:del-statement

Conversation

@mtshiba
Copy link
Collaborator

@mtshiba mtshiba commented Jun 9, 2025

Summary

This PR closes astral-sh/ty#238.

Since DefinitionState::Deleted was introduced in #18041, support for the del statement (and deletion of except handler names) is straightforward.

However, it is difficult to determine whether references to attributes or subscripts are unresolved after they are deleted. This PR only invalidates narrowing by assignment if the attribute or subscript is deleted.

Test Plan

mdtest/del.md is added.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Jun 9, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Jun 9, 2025

mypy_primer results

Changes were detected when running on open source projects
anyio (https://github.com/agronholm/anyio)
+ warning[possibly-unresolved-reference] src/anyio/__init__.py:92:5: Name `__value` used when possibly not defined
+ warning[possibly-unresolved-reference] src/anyio/abc/__init__.py:58:5: Name `__value` used when possibly not defined
- Found 109 diagnostics
+ Found 111 diagnostics

trio (https://github.com/python-trio/trio)
+ warning[possibly-unresolved-reference] src/trio/_core/_run.py:2910:25: Name `msg` used when possibly not defined
+ error[unresolved-reference] src/trio/_core/_tests/test_guest_mode.py:70:13: Name `todo` used when not defined
+ error[unresolved-reference] src/trio/_core/_tests/test_guest_mode.py:71:9: Name `todo` used when not defined
+ error[unresolved-reference] src/trio/_core/_tests/test_guest_mode.py:80:13: Name `todo` used when not defined
+ error[unresolved-reference] src/trio/_core/_tests/test_guest_mode.py:81:9: Name `todo` used when not defined
+ error[unresolved-reference] src/trio/_core/_tests/test_guest_mode.py:85:9: Name `todo` used when not defined
- Found 1103 diagnostics
+ Found 1109 diagnostics

apprise (https://github.com/caronc/apprise)
- error[unsupported-operator] apprise/utils/parse.py:781:9: Operator `+=` is unsupported between objects of type `str` and `str | @Todo(Type::Intersection.call()) | None`
- Found 3691 diagnostics
+ Found 3690 diagnostics

werkzeug (https://github.com/pallets/werkzeug)
+ error[invalid-assignment] tests/test_http.py:141:9: Object of type `float` is not assignable to attribute `max_age` of type `int | None`
+ error[invalid-assignment] tests/test_http.py:144:9: Object of type `float` is not assignable to attribute `s_maxage` of type `int | None`
- Found 451 diagnostics
+ Found 453 diagnostics

colour (https://github.com/colour-science/colour)
+ warning[possibly-unresolved-reference] colour/__init__.py:68:21: Name `_KEY` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/__init__.py:68:27: Name `_VALUE` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/__init__.py:907:5: Name `_JCh` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/__init__.py:907:11: Name `_Jab` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/__init__.py:907:17: Name `_module` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/__init__.py:907:26: Name `_sub_module` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/graph/conversion.py:1012:24: Name `_JCh` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/graph/conversion.py:1012:30: Name `_Jab` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/graph/conversion.py:1012:36: Name `_module` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/graph/conversion.py:1012:45: Name `_Jab_name` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/graph/conversion.py:1012:56: Name `_JCh_name` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/models/__init__.py:501:51: Name `_JCh` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/models/__init__.py:501:57: Name `_Jab` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/models/__init__.py:501:63: Name `_callable` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/models/__init__.py:501:74: Name `_module` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/plotting/common.py:229:5: Name `_scaling` used when possibly not defined
+ warning[possibly-unresolved-reference] colour/plotting/common.py:229:15: Name `_value` used when possibly not defined
- Found 542 diagnostics
+ Found 559 diagnostics

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ warning[possibly-unresolved-reference] test/helper_tools/memoryleak.py:43:13: Name `ssl` used when possibly not defined
- Found 2047 diagnostics
+ Found 2048 diagnostics

pycryptodome (https://github.com/Legrandin/pycryptodome)
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_CBC.py:454:5: Name `file_name` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_CBC.py:454:16: Name `new_func` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_CFB.py:258:5: Name `file_name` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_CFB.py:258:16: Name `new_func` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_OFB.py:121:5: Name `file_name` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/Crypto/SelfTest/Cipher/test_OFB.py:121:16: Name `new_func` used when possibly not defined
- Found 1530 diagnostics
+ Found 1536 diagnostics

django-stubs (https://github.com/typeddjango/django-stubs)
+ error[unresolved-attribute] ext/tests/test_monkeypatching.py:44:17: Type `<module 'builtins'>` has no attribute `reveal_type`
+ error[unresolved-attribute] ext/tests/test_monkeypatching.py:46:17: Type `<module 'builtins'>` has no attribute `reveal_locals`
- Found 470 diagnostics
+ Found 472 diagnostics

scrapy (https://github.com/scrapy/scrapy)
+ warning[possibly-unresolved-reference] tests/test_utils_datatypes.py:360:13: Name `r` used when possibly not defined
- Found 1328 diagnostics
+ Found 1329 diagnostics

freqtrade (https://github.com/freqtrade/freqtrade)
+ error[unresolved-attribute] freqtrade/rpc/api_server/webserver.py:89:13: Attribute `_rpc` can only be accessed on instances, not on the class object `<class 'ApiServer'>` itself.
- Found 464 diagnostics
+ Found 465 diagnostics

streamlit (https://github.com/streamlit/streamlit)
+ warning[possibly-unresolved-reference] lib/streamlit/vendor/pympler/asizeof.py:1532:5: Name `i` used when possibly not defined
+ warning[possibly-unresolved-reference] lib/streamlit/vendor/pympler/asizeof.py:1532:11: Name `t` used when possibly not defined
- Found 3292 diagnostics
+ Found 3294 diagnostics

bokeh (https://github.com/bokeh/bokeh)
+ warning[possibly-unresolved-reference] src/bokeh/application/handlers/document_lifecycle.py:70:13: Name `callback` used when possibly not defined
- Found 938 diagnostics
+ Found 939 diagnostics

scikit-learn (https://github.com/scikit-learn/scikit-learn)
+ warning[possibly-unresolved-reference] sklearn/decomposition/_fastica.py:630:20: Name `d` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/externals/array_api_compat/dask/array/fft.py:10:9: Name `k` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/externals/array_api_compat/dask/array/linalg.py:26:9: Name `k` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/externals/array_api_compat/torch/__init__.py:13:5: Name `n` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/linear_model/_least_angle.py:832:21: Name `coef` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/linear_model/_least_angle.py:832:27: Name `alpha` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/linear_model/_least_angle.py:832:34: Name `prev_alpha` used when possibly not defined
+ warning[possibly-unresolved-reference] sklearn/linear_model/_least_angle.py:832:46: Name `prev_coef` used when possibly not defined
- Found 2519 diagnostics
+ Found 2527 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ error[unresolved-attribute] ddtrace/contrib/internal/unittest/patch.py:372:9: Type `<class 'CIVisibility'>` has no attribute `_unittest_data`
+ error[unresolved-attribute] ddtrace/contrib/internal/unittest/patch.py:870:17: Type `<class 'CIVisibility'>` has no attribute `_datadog_session_span`
+ error[unresolved-attribute] ddtrace/contrib/internal/unittest/patch.py:875:13: Type `<class 'CIVisibility'>` has no attribute `_datadog_session_span`
+ warning[possibly-unresolved-reference] tests/tracer/test_memory_leak.py:181:5: Name `span` used when possibly not defined
+ warning[possibly-unresolved-reference] tests/tracer/test_memory_leak.py:182:9: Name `span` used when possibly not defined
- Found 6899 diagnostics
+ Found 6904 diagnostics

scipy (https://github.com/scipy/scipy)
+ warning[possibly-unresolved-reference] scipy/_lib/array_api_compat/array_api_compat/dask/array/fft.py:10:9: Name `k` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/_lib/array_api_compat/array_api_compat/dask/array/linalg.py:26:9: Name `k` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/_lib/array_api_compat/array_api_compat/torch/__init__.py:13:5: Name `n` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/optimize/_shgo_lib/_complex.py:334:17: Name `cC0x` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/optimize/_shgo_lib/_complex.py:336:17: Name `cC1x` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/optimize/_shgo_lib/_complex.py:338:17: Name `ab_Cc` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/optimize/_shgo_lib/_complex.py:927:17: Name `ab_Cc` used when possibly not defined
+ warning[possibly-unresolved-reference] scipy/stats/_distn_infrastructure.py:369:5: Name `obj` used when possibly not defined
- Found 7607 diagnostics
+ Found 7615 diagnostics

sympy (https://github.com/sympy/sympy)
+ warning[possibly-unresolved-reference] sympy/abc.py:111:29: Name `_k` used when possibly not defined
+ warning[possibly-unbound-attribute] sympy/assumptions/tests/test_query.py:2250:13: Attribute `my_key` on type `Unknown | AssumptionKeys` is possibly unbound
+ warning[possibly-unbound-attribute] sympy/assumptions/tests/test_query.py:2264:13: Attribute `my_key` on type `Unknown | AssumptionKeys` is possibly unbound
+ warning[possibly-unbound-attribute] sympy/assumptions/tests/test_query.py:2495:13: Attribute `mersenne` on type `Unknown | AssumptionKeys` is possibly unbound
+ warning[possibly-unbound-attribute] sympy/assumptions/tests/test_query.py:2513:13: Attribute `mersenne` on type `Unknown | AssumptionKeys` is possibly unbound
+ warning[possibly-unbound-attribute] sympy/assumptions/tests/test_query.py:2540:13: Attribute `sexyprime` on type `Unknown | AssumptionKeys` is possibly unbound
+ warning[possibly-unresolved-reference] sympy/physics/units/systems/si.py:344:5: Name `_scale_factor` used when possibly not defined
+ warning[possibly-unresolved-reference] sympy/physics/units/systems/si.py:344:20: Name `_dimension` used when possibly not defined
- Found 18566 diagnostics
+ Found 18574 diagnostics

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.

Nice!

@carljm carljm merged commit ef56409 into astral-sh:main Jun 12, 2025
35 checks passed
@mtshiba mtshiba deleted the del-statement branch June 12, 2025 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support del statement and deletion of except handler names

3 participants