8000 Add runtime type annotations to IPython.utils via MonkeyType by Carreau · Pull Request #15146 · ipython/ipython · GitHub
[go: up one dir, main page]

Skip to content

Add runtime type annotations to IPython.utils via MonkeyType#15146

Open
Carreau wants to merge 5 commits intomainfrom
claude/monkeytype-ipython-tests-Y0WBi
Open

Add runtime type annotations to IPython.utils via MonkeyType#15146
Carreau wants to merge 5 commits intomainfrom
claude/monkeytype-ipython-tests-Y0WBi

Conversation

@Carreau
Copy link
Member
@Carreau Carreau commented Feb 26, 2026

Run MonkeyType against the IPython test suite to collect runtime type
information, then apply the generated annotations to 6 files in
IPython/utils/:

  • decorators.py: annotate undoc() and sphinx_options() return type
  • dir2.py: annotate safe_hasattr/dir2/get_real_method with object/str/List[str]
  • io.py: annotate Tee.init (Union[str, StringIO]) and temp_pyfile
  • path.py: annotate _writable_dir, get_home_dir, get_xdg_dir, expand_path, ensure_dir_exists
  • tempdir.py: annotate NamedFileInTemporaryDirectory and TemporaryWorkingDirectory
  • tokenutil.py: annotate generate_tokens readline parameter as Callable

Manual corrections applied on top of MonkeyType output:

  • dir2.py: replaced test-specific Base import with object (the correct general type)
  • io.py: widened Tee file_or_name from StringIO to Union[str, StringIO]
  • tempdir.py: corrected exit params from None to Optional[Type[BaseException]] etc.

Tests: 146 passed, 6 skipped across the modified modules and related tests.

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp

claude added 3 commits March 3, 2026 17:42
Run MonkeyType against the IPython test suite to collect runtime type
information, then apply the generated annotations to 6 files in
IPython/utils/:

- decorators.py: annotate undoc() and sphinx_options() return type
- dir2.py: annotate safe_hasattr/dir2/get_real_method with object/str/List[str]
- io.py: annotate Tee.__init__ (Union[str, StringIO]) and temp_pyfile
- path.py: annotate _writable_dir, get_home_dir, get_xdg_dir, expand_path, ensure_dir_exists
- tempdir.py: annotate NamedFileInTemporaryDirectory and TemporaryWorkingDirectory
- tokenutil.py: annotate generate_tokens readline parameter as Callable

Manual corrections applied on top of MonkeyType output:
- dir2.py: replaced test-specific `Base` import with `object` (the correct general type)
- io.py: widened Tee file_or_name from StringIO to Union[str, StringIO]
- tempdir.py: corrected __exit__ params from None to Optional[Type[BaseException]] etc.

Tests: 146 passed, 6 skipped across the modified modules and related tests.

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
The MonkeyType trace database is a generated artifact from running
`monkeytype run` during development and should not be tracked.

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
…dules

Apply MonkeyType-generated type annotations to 12 additional files,
completing coverage across all modules traced during the test suite run.

Files annotated:
- IPython/terminal/prompts.py: Prompts.__init__ shell param, out_prompt_tokens, write_format_data
- IPython/terminal/shortcuts/filters.py: has_focus, preceding_text, following_text, eval_node, filter_from_string return types
- IPython/testing/decorators.py: skipif, skip, onlyif -> MarkDecorator; module_not_available -> bool
- IPython/testing/ipunittest.py: Doc2UnitTester.__init__ verbose param
- IPython/testing/plugin/pytest_ipdoctest.py: from_parent return, get_optionflags, _get_continue_on_failure
- IPython/testing/skipdoctest.py: skip_doctest f/return -> Callable
- IPython/testing/tools.py: parse_test_output, default_argv, default_config, get_ipython_cmd, ipexec, ipexec_validate, mktmp, AssertPrints
- IPython/utils/capture.py: CapturedIO.__init__, stdout/stderr properties, capture_output context manager
- IPython/utils/ipstruct.py: __setitem__, __setattr__, __getattr__ -> Any
- IPython/utils/py3compat.py: encode, cast_unicode
- IPython/utils/strdispatch.py: add_s
- IPython/utils/terminal.py: toggle_set_term_title

Manual corrections applied:
- prompts.py: circular import with interactiveshell fixed via TYPE_CHECKING + string annotation
- pytest_ipdoctest.py: removed self-referential circular import of IPDoctestItem
- filters.py: replaced verbose private prompt_toolkit internal types with public Filter/Condition; removed unused Buffer/Container/UIControl imports
- ipstruct.py: replaced over-specific CommandChainDispatcher with Any for general dict methods
- tools.py: options: None -> Optional[List[str]], commands: Tuple[()] -> Tuple[str, ...]
- capture.py/tools.py: __exit__ None params -> proper Optional[Type[BaseException]] etc.
- py3compat.py: encoding: None -> Optional[str]

Tests: 146 passed, 6 skipped.

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
@Carreau Carreau force-pushed the claude/monkeytype-ipython-tests-Y0WBi branch from 93ad49d to 0df05ac Compare March 3, 2026 17:42
claude added 2 commits March 3, 2026 17:50
- ipstruct.py: remove duplicate `from typing import Any` (ruff F811)
- skipdoctest.py: type skip_doctest param/return as Any since Callable
  doesn't allow arbitrary attribute assignment (__skip_doctest__)
- path.py: fix get_xdg_dir return type str -> str | None (function
  returns None on non-posix or non-writable xdg dir)

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
cast_unicode was annotated as accepting str | bytes, but tbtools.py
_tokens_filename passes file: str | None to it without a None guard.
This would crash at runtime if file is None since cast_unicode passes
the value through to compress_user which calls .startswith() on it.

Fix: add `file or ""` guard at the tbtools.py call site, which is the
correct behaviour (treat a missing filename as an empty string).

The annotation cast_unicode(s: str | bytes) -> str is correct.

https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
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

0