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.

Author sobolevn
Recipients sobolevn
Date 2022-01-28.16:47:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Some variables created as `for X in ...` leak into module's namespace, where the loop is defined.

I wrote a simple `flake8` plugin to find names that are used in `ast.Module` in `ast.For`, but not under `if __name__ == '__main__'` and are not used in `del` afterwards.

Here's what I got:

- Lib/
- Lib/
- Lib/
- Lib/ - 151
- Lib/multiprocessing/
- Lib/multiprocessing/ 
- Lib/json/
- Lib/http/ 
- Lib/email/
- Lib/email/
- Lib/email/
- Lib/email/
- Lib/email/
- Lib/_compat_pickle
- Lib/lib2to3/pgen2/

I think, that we need to remove these names. Why?
1. They complicate typeshed typing, we have to annotate them in typeshed, or write custom ignore rules for our test suite. Ref:

2. They are in `dir()`, example:

>>> import inspect
>>> 'k' in dir(inspect)

3. They are listed in `help()`, let's use `json.encoder` as an example:

    ESCAPE = re.compile('[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t]')
    ESCAPE_ASCII = re.compile('([\\\\"]|[^\\ -~])')
    ESCAPE_DCT = {'\x00': r'\u0000', '\x01': r'\u0001', '\x02': r'\u0002',...
    HAS_UTF8 = re.compile(b'[\x80-\xff]')
    INFINITY = inf
    i = 31

4. We also have to exclude them sometimes in tests, like

I think that adding `del X` somewhere in these modules is a good thing:
1. Not hard to backport
2. Fixes multiple issues above
3. Does not store useless objects in memory
4. Does not confuse people
5. Some modules already delete unused intermediate vars, so it is not something new to CPython, for example: `multiprocessing.process`

PR is on its way!
Date User Action Args
2022-01-28 16:47:31sobolevnsetrecipients: + sobolevn
2022-01-28 16:47:31sobolevnsetmessageid: <>
2022-01-28 16:47:31sobolevnlinkissue46565 messages
2022-01-28 16:47:31sobolevncreate