Modernize type annotations: typing.Optional/Union/List/Dict/Tuple/Type → built-in generics#15291
Open
Carreau wants to merge 8 commits into
Open
Modernize type annotations: typing.Optional/Union/List/Dict/Tuple/Type → built-in generics#15291Carreau wants to merge 8 commits into
Carreau wants to merge 8 commits into
Conversation
…s.py Replace Optional/Union/List/Dict/Tuple with built-in X | None, X | Y, list[...], dict[...], tuple[...] and add from __future__ import annotations. These two files were not touched by the recent pyupgrade py311-plus pass.
…upgrade tips.py, clipboard.py, data.py, importstring.py, ipstruct.py, sentinel.py, sysinfo.py, terminal.py were not touched by the pyupgrade py311-plus pass; add the future import for consistency with the rest of the codebase.
…pub.py typing Place the future import after each module's docstring (a prior version of this change placed it before the docstring, which silently discarded __doc__ for these 8 modules). Also replace displaypub.py's 'import typing as t' / 't.Dict' with the built-in 'dict', the only remaining bracket-less deprecated typing usage found in these files.
…eter.py Convert AnyMatcherCompletion/MatcherResult/MatcherAPIv1/Matcher type aliases from Union[...] to X | Y (pyupgrade left these dotted-assignment forms untouched). Add missing annotations to get__all__entries, _parse_tokens, python_func_kw_matches, latex_name_matcher, custom_completer_matcher, dispatch_custom_completer, and fwd_unicode_matcher.
…eshell.py and history.py interactiveshell.py: cell_meta: Optional[dict] -> dict | None; drop now-unused Optional/Tuple imports. history.py: InOrInOut = typing.Union[str, tuple[str, Optional[str]]] -> str | tuple[str, str | None]; drop now-unused Tuple/Optional imports. pyupgrade's regex-based pass left this dotted/nested form untouched.
…l.py, auto_suggest.py alias.py: add from __future__ import annotations; default_aliases() return type t.List[t.Tuple[str, str]] -> list[tuple[str, str]]; drop the now-unused 'import typing as t'. guarded_eval.py: MayHaveGetattr = Union[...] -> X | Y (Union import kept, still used at runtime for an 'origin is Union' check). auto_suggest.py: Provider = Union[...] -> X | Y | None; drop the now-unused List/Optional/Union/Tuple typing imports.
Add from __future__ import annotations (placed after the module docstring) and remove now-unnecessary quotes from forward references, and drop the now-unused Dict/List/Optional/Tuple/Type/Union typing imports. This file was not touched by the recent pyupgrade py311-plus pass, though a few of its Optional/Union usages had already been modernized by an earlier commit.
4fee11c to
070a727
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rebased on
mainafter therun pyupgrade py311--plus on all but IPython/utils/text.pycommit (b5119b4), which independently modernized almost everything this PR originally touched. Rather than force the old 27 commits through that history, this PR was rebuilt from scratch on top of currentmain, keeping only what pyupgrade did not already cover:IPython/utils/text.pyanddocs/autogen_shortcuts.py— explicitly excluded from the pyupgrade run (the former due tolist = property(...)shadowing the builtin), so still needed fullOptional/Union/List/Dict/Tuple→ built-in modernization +from __future__ import annotations.macro.py,tips.py,autoreload.py,clipboard.py,ptutils.py,skipdoctest.py,data.py,importstring.py,ipstruct.py,sentinel.py,sysinfo.py,terminal.py,displaypub.py,magics/auto.py,magics/basic.py,paths.py,_process_posix.py) that pyupgrade didn't addfrom __future__ import annotationsto.completer.py(4Union[...]type aliases + missing annotations on 7 functions),pytest_ipdoctest.py(quoted forward refs + severalOptional/Union),interactiveshell.py,history.py,guarded_eval.py,alias.py,terminal/shortcuts/auto_suggest.py.Bug caught while rebuilding this: the original commits had inserted
from __future__ import annotationsbefore the module docstring in 8 files (autoreload.py,ptutils.py,skipdoctest.py,displaypub.py,magics/auto.py,magics/basic.py,paths.py,_process_posix.py). Per the future-statement placement rules, a statement before the docstring demotes it from__doc__to a no-op expression — silently discarding the module docstring. Fixed by placing the import after the docstring in all cases, and verifiedast.get_docstring()still returns non-None for each.Everything else from the original 27-commit set (~50 files) is now byte-for-byte covered by the pyupgrade commit already on
mainand was dropped from this PR as redundant.Test plan
python -m pytest tests/— 1584 passed, 120 skipped, 3 xfailed (full suite)import IPythonand direct imports of every touched module succeedast.get_docstring()verified non-None on the 8 files where the future-import placement bug was fixed