Add runtime type annotations to IPython.utils via MonkeyType#15146
Open
Add runtime type annotations to IPython.utils via MonkeyType#15146
Conversation
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
93ad49d to
0df05ac
Compare
- 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
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.
Run MonkeyType against the IPython test suite to collect runtime type
information, then apply the generated annotations to 6 files in
IPython/utils/:
Manual corrections applied on top of MonkeyType output:
Baseimport withobject(the correct general type)Tests: 146 passed, 6 skipped across the modified modules and related tests.
https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp