Skip to content

Enable strict mypy type checking and add type annotations#15307

Open
Carreau wants to merge 2 commits into
mainfrom
claude/stricter-mypy-config-qdpvml
Open

Enable strict mypy type checking and add type annotations#15307
Carreau wants to merge 2 commits into
mainfrom
claude/stricter-mypy-config-qdpvml

Conversation

@Carreau

@Carreau Carreau commented Jul 10, 2026

Copy link
Copy Markdown
Member

This PR enables stricter mypy type checking and adds necessary type annotations to resolve the newly enabled checks.

Summary

Enable disallow_untyped_calls and disallow_untyped_decorators in mypy configuration and add corresponding type annotations throughout the codebase to comply with these stricter rules.

Key Changes

  • pyproject.toml: Enabled disallow_untyped_calls and disallow_untyped_decorators mypy options to enforce stricter type checking
  • IPython/core/history.py: Added explicit type annotations to DummyDB methods (commit, __enter__, __exit__, close) replacing # type: ignore comments with proper typing.Any annotations and return type hints
  • IPython/core/doctb.py: Added # type: ignore[no-untyped-call] comment for stack_data.style_with_executing_node() call which is an external library without type annotations
  • IPython/init.py: Added type annotations to start_ipython() function signature with proper parameter and return types
  • IPython/paths.py: Added type annotations to locate_profile() function signature

Notable Details

  • The changes maintain backward compatibility while improving type safety
  • External library calls without type annotations (e.g., stack_data) are handled with targeted type: ignore comments rather than disabling checks globally
  • All internal function signatures now have explicit type hints to satisfy the stricter mypy configuration

https://claude.ai/code/session_018PchnNg3PY6WPdPmkcMcb2

claude added 2 commits July 10, 2026 09:24
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants