Repository navigation
Fix return annotation inference for callable instances - #2128
Open
leeshengis wants to merge 1 commit into
Open
leeshengis wants to merge 1 commit into
leeshengis wants to merge 1 commit into
Conversation
Fixes davidhalter#1962. Developed with OpenAI Codex assistance.
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.
Fixes #1962.
Callable instances currently lose the return annotation on
__call__during Interpreter inference. This change reads the annotation from the instance type's Python__call__function or bound method, matching Python's special-method lookup. Instance-level annotations and__call__overrides are ignored. Custom descriptors are excluded so this new annotation lookup does not execute their code.The regression tests cover ordinary, static and class methods, inheritance, forward-reference annotations, misleading instance attributes, and descriptor safety. Existing function and class handling is unchanged.
Validation
Tested on Linux with Python 3.12.14:
--interpreter-env.test/test_inference/test_namespace_package.py::test_relative_importalso reproduces with the original production code: the inferred module name includes the pytest temporary directory instead of justrel2. No unrelated fix or suppression is included.python -m flake8 jedi test setup.py): passed.Other Python versions and operating systems have not been tested locally.
AI assistance
OpenAI Codex assisted with investigation, implementation, tests and automated review. A separate Codex reviewer reran the focused tests, lint and type checks. This is not a claim of human code review. Submitted for maintainer feedback.