Skip to content

[red-knot] Add callable subtyping for callable instances and bound methods #17105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 1, 2025

Conversation

MatthewMckee4
Copy link
Contributor

Summary

Trying to improve #17005
Partially fixes #16953

Test Plan

Update is_subtype_of.md

Copy link
Contributor

github-actions bot commented Mar 31, 2025

mypy_primer results

Changes were detected when running on open source projects
rich (https://github.com/Textualize/rich)
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/rich/tests/test_progress.py:292:60: Object of type `MockClock` cannot be assigned to parameter `get_time` of function `track`; expected type `(() -> int | float) | None`
- Found 578 diagnostics
+ Found 577 diagnostics

black (https://github.com/psf/black)
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/black/src/black/__init__.py:1280:48: Object of type `<bound method `readline` of `BytesIO`> | @Todo(instance attribute on class with dynamic base)` cannot be assigned to parameter 1 (`readline`) of function `detect_encoding`; expected type `() -> bytes | bytearray`
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/black/src/black/__init__.py:1280:48: Object of type `<bound method `readline` of `BytesIO`> | @Todo(instance attribute on class with dynamic base)` cannot be assigned to parameter 1 (`readline`) of function `detect_encoding`; expected type `() -> bytes | bytearray`
- error[lint:invalid-argument-type] /tmp/mypy_primer/projects/black/src/black/__init__.py:1280:48: Object of type `<bound method `readline` of `BytesIO`> | @Todo(instance attribute on class with dynamic base)` cannot be assigned to parameter 1 (`readline`) of function `detect_encoding`; expected type `() -> bytes | bytearray`
- Found 212 diagnostics
+ Found 209 diagnostics

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 31, 2025
Comment on lines 4335 to 4341
let signature = self.function(db).signature(db).clone();
let parameters = Parameters::new(signature.parameters().iter().skip(1).cloned());

Type::Callable(CallableType::General(GeneralCallableType::new(
db,
Signature::new(parameters, signature.return_ty),
)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is how we will want to implement this , but i think its right to not check the name of the variable, and since staticmethod is not supported, this should be fine, but when that is implemented i think we will have to check decorators

@MatthewMckee4 MatthewMckee4 marked this pull request as ready for review April 1, 2025 00:20
@MichaReiser
Copy link
Member

MichaReiser commented Apr 1, 2025

Not sure, but this might be a good one for @dcreager to review?

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me! The tests are good, and we can always change the implementation of BoundMethodType::into_callable_type if we decide we need to preserve the first argument.

Thank you!

Co-authored-by: Carl Meyer <[email protected]>
@carljm carljm changed the title [red-knot] Add callable subtyping for bounded methods [red-knot] Add callable subtyping for callable instances and bound methods Apr 1, 2025
@carljm carljm enabled auto-merge (squash) April 1, 2025 23:39
@carljm carljm merged commit eb3e176 into astral-sh:main Apr 1, 2025
22 checks passed
@MatthewMckee4 MatthewMckee4 deleted the bounded-method-subtype branch April 1, 2025 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[red-knot] Implement subtyping/assignability between different callable objects
5 participants