Enable strict mypy type checking and add type annotations#15307
Open
Carreau wants to merge 2 commits into
Open
Enable strict mypy type checking and add type annotations#15307Carreau wants to merge 2 commits into
Carreau wants to merge 2 commits into
Conversation
start_ipython() gained a proper signature so IPython/__main__.py's call is no longer flagged; DummyDB's dunder/close/commit methods got real annotations instead of blanket type: ignore comments, fixing the weakref.finalize callback in history.py; locate_profile() gained a signature; and the one genuinely untyped third-party call (stack_data.style_with_executing_node) got a scoped ignore with an explanation.
Several attributes and parameters were typed narrower than what the code actually stores or accepts, silently turning defensive branches into dead code: - Macro.__init__ had a bytes-decode branch that could never run since the preceding coding-declaration loop already crashes on bytes input (regex matching a str pattern against bytes lines); removed the dead branch instead of half-fixing bytes support nothing exercises. - ProfileDir.copy_config_file had a `path is None` fallback that no caller ever hits, since `path: Path` is never optional; removed it. - get_line_number_of_frame checked `co_filename is None`, but typeshed (correctly) types co_filename as always `str`; dropped the dead branch and its stray debug print. - FrameInfo._sd is set from stack_data.FrameInfo.stack_data() results, which can be RepeatedFrames or None, not just FrameInfo as declared; widened the attribute type and scoped the resulting union-attr errors to where callers already guard against RepeatedFrames. - InteractiveShell.data_pub_class and .pylab_gui_select were declared as plain `= None` attributes (inferred type `None`), making every branch that actually uses them unreachable; gave them real types. - module_paths.py and PyColorize.py had comparisons/branches that are genuinely unreachable per their stubs/logic but intentional at runtime (loader-is-a-meta_path-finder check, style property kept only to satisfy its return type); scoped-ignored with a comment. - async_helpers._asyncio_event_loop lacked a type annotation, so mypy inferred it as always-None and flagged the "or" short-circuit as dead; annotated it as Optional. - ultratb.py's structured_traceback methods handle `etb` being a tuple (chained-exception recursion) or a list, and one handles `etype` being None (via `structured_traceback(*sys.exc_info())`), none of which fit the public `TracebackType | None` / `type` signatures; scoped-ignored with comments rather than widening the signature across the whole TB class hierarchy.
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.
This PR enables stricter mypy type checking and adds necessary type annotations to resolve the newly enabled checks.
Summary
Enable
disallow_untyped_callsanddisallow_untyped_decoratorsin mypy configuration and add corresponding type annotations throughout the codebase to comply with these stricter rules.Key Changes
disallow_untyped_callsanddisallow_untyped_decoratorsmypy options to enforce stricter type checkingDummyDBmethods (commit,__enter__,__exit__,close) replacing# type: ignorecomments with propertyping.Anyannotations and return type hints# type: ignore[no-untyped-call]comment forstack_data.style_with_executing_node()call which is an external library without type annotationsstart_ipython()function signature with proper parameter and return typeslocate_profile()function signatureNotable Details
stack_data) are handled with targetedtype: ignorecomments rather than disabling checks globallyhttps://claude.ai/code/session_018PchnNg3PY6WPdPmkcMcb2