Skip to content

Fix return annotation inference for callable instances - #2128

Open
leeshengis wants to merge 1 commit into
davidhalter:masterfrom
leeshengis:fix-callable-return-annotations
Open

leeshengis wants to merge 1 commit into
davidhalter:masterfrom
leeshengis:fix-callable-return-annotations

Conversation

@leeshengis

@leeshengis leeshengis commented Nov 2, 2026 •

Copy link
Copy Markdown

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:

  • New regression cases: 13 passed. Against the original accessor, 12 failed and the descriptor-safety control passed.
  • Interpreter module: 168 passed, both normally and with --interpreter-env.
  • Full suite: 4,041 passed, 21 skipped, 5 xfailed, 1 failed.
  • The failure in test/test_inference/test_namespace_package.py::test_relative_import also reproduces with the original production code: the inferred module name includes the pytest temporary directory instead of just rel2. No unrelated fix or suppression is included.
  • CI lint scope (python -m flake8 jedi test setup.py): passed.
  • Zuban: no issues in 152 source files.

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.

Fixes davidhalter#1962. Developed with OpenAI Codex assistance.
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.

Tab complete for class __call__ not working

1 participant