Mercurial > public > mercurial-scm > hg
comparison contrib/import-checker.py @ 52644:e627cc25b6f3
pyupgrade: rewrite `yield` statements in a loop to `yield from`
This is the `legacy` fixer in `pyupgrade`, with the `yield` statement yielding
loop commented back in. This seems to help pytype in some cases, and hurt it in
others. But that can be manually fixed later.
Note that it's possibly buggy in that it aggressively changed `import-checker.py`
to `yield from 'fcntl', 'grp', 'pwd', 'select', 'termios': # Unix only`, which
is invalid syntax. Possibly it needed help from the token fixer that I've
disabled locally (because that wants to make a bunch of unrelated changes).
Just change those few places to yield from a list, to avoid having to constantly
revert that.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 05 Jan 2025 22:26:16 -0500 |
parents | 0cc50d9ac6b0 |
children | b95b12cb31e2 |
comparison
equal
deleted
inserted
replaced
52643:5cc8deb96b48 | 52644:e627cc25b6f3 |
---|---|
215 True | 215 True |
216 | 216 |
217 >>> 'cffi' in mods | 217 >>> 'cffi' in mods |
218 True | 218 True |
219 """ | 219 """ |
220 for m in sys.builtin_module_names: | 220 yield from sys.builtin_module_names |
221 yield m | |
222 # These modules only exist on windows, but we should always | 221 # These modules only exist on windows, but we should always |
223 # consider them stdlib. | 222 # consider them stdlib. |
224 for m in ['msvcrt', '_winreg']: | 223 yield from ['msvcrt', '_winreg'] |
225 yield m | |
226 yield '__builtin__' | 224 yield '__builtin__' |
227 yield 'builtins' # python3 only | 225 yield 'builtins' # python3 only |
228 yield 'importlib.abc' # python3 only | 226 yield 'importlib.abc' # python3 only |
229 yield 'importlib.machinery' # python3 only | 227 yield 'importlib.machinery' # python3 only |
230 yield 'importlib.util' # python3 only | 228 yield 'importlib.util' # python3 only |
231 yield 'packaging.version' | 229 yield 'packaging.version' |
232 for m in 'fcntl', 'grp', 'pwd', 'select', 'termios': # Unix only | 230 yield from ['fcntl', 'grp', 'pwd', 'select', 'termios'] |
233 yield m | 231 yield from ['cPickle', 'datetime'] |
234 for m in 'cPickle', 'datetime': # in Python (not C) on PyPy | 232 yield from ['cffi'] |
235 yield m | |
236 for m in ['cffi']: | |
237 yield m | |
238 yield 'distutils' # in Python < 3.12 | 233 yield 'distutils' # in Python < 3.12 |
239 yield 'distutils.version' # in Python < 3.12 | 234 yield 'distutils.version' # in Python < 3.12 |
240 stdlib_prefixes = {sys.prefix, sys.exec_prefix} | 235 stdlib_prefixes = {sys.prefix, sys.exec_prefix} |
241 # We need to supplement the list of prefixes for the search to work | 236 # We need to supplement the list of prefixes for the search to work |
242 # when run from within a virtualenv. | 237 # when run from within a virtualenv. |
441 def msg(fmt, *args): | 436 def msg(fmt, *args): |
442 return (fmt % args, node.lineno) | 437 return (fmt % args, node.lineno) |
443 | 438 |
444 if newscope: | 439 if newscope: |
445 # Check for local imports in function | 440 # Check for local imports in function |
446 for r in verify_modern_convention( | 441 yield from verify_modern_convention( |
447 module, node, localmods, node.col_offset + 4 | 442 module, node, localmods, node.col_offset + 4 |
448 ): | 443 ) |
449 yield r | |
450 elif isinstance(node, ast.Import): | 444 elif isinstance(node, ast.Import): |
451 # Disallow "import foo, bar" and require separate imports | 445 # Disallow "import foo, bar" and require separate imports |
452 # for each module. | 446 # for each module. |
453 if len(node.names) > 1: | 447 if len(node.names) > 1: |
454 yield msg( | 448 yield msg( |