This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `importlib.metadata.DeprecatedList` leaks `method_name` variable
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, jaraco, sobolevn
Priority: normal Keywords: patch

Created on 2022-01-27 11:24 by sobolevn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30956 closed sobolevn, 2022-01-27 11:26
Messages (3)
msg411855 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-27 11:24
Right now in `DeprecatedList` there's a possibly unwated name leak of `method_name` here: https://github.com/python/cpython/blob/08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85/Lib/importlib/metadata/__init__.py#L295-L308

```
for method_name in [
        '__setitem__',
        '__delitem__',
        'append',
        'reverse',
        'extend',
        'pop',
        'remove',
        '__iadd__',
        'insert',
        'sort',
    ]:
        locals()[method_name] = _wrap_deprecated_method(method_name)
```

Example:

```
>>> import importlib
>>> import importlib.metadata
>>> importlib.metadata.DeprecatedList.method_name
'sort'
```

Right now `method_name` is unused, undocumented, and untested.

My proposal is to add `del method_name` after the `for` loop, so it won't leak.
msg411882 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2022-01-27 14:29
Thanks Nikita for the report. I agree, it's inelegant that this property leaks. Because this code is synced with importlib_metadata, I'm not sure it's worth the effort of changing it here. Your change here implies a cherry-pick to the backport and updating its changelog there too.

I'd be more inclined to accept a change like this on importlib_metadata, which has a faster iteration and whose changes get naturally incorporated into CPython periodically.

What's the harm in leaving this attribute on a class that is itself standing in for deprecated behavior and slated for removal?
msg411885 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-27 14:52
Thanks, Jason! I've submitted https://github.com/python/importlib_metadata/pull/365

> What's the harm in leaving this attribute on a class that is itself standing in for deprecated behavior and slated for removal?

I think it does not do much harm, but there are several things that it affects:
1. Typing, we have to annotate it in typeshed, or write a custom ignore rule for our test suite
2. It is listed in `help()`:

```
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
 |  
 |  method_name = 'sort'
 |  
 |  ----------------------------------------------------------------------
```

3. It is also in `dir(importlib.metadata.DeprecatedList)`

So, even if we remove this class in some time, we still should care about it until the removal :)

I am going to close this as "third party".
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90704
2022-01-27 14:52:17sobolevnsetstatus: open -> closed
resolution: third party
messages: + msg411885

stage: patch review -> resolved
2022-01-27 14:29:53jaracosetmessages: + msg411882
2022-01-27 11:26:49sobolevnsetkeywords: + patch
stage: patch review
pull_requests: + pull_request29135
2022-01-27 11:24:52sobolevncreate